Esempio n. 1
0
 private void checkInput()
 {
     if (!isCPU)
     {
         KeyboardState newState = Keyboard.GetState();
         oldState = newState;
         if (newState.IsKeyDown(Keys.Up))
         {
             if (!oldState.IsKeyDown(Keys.Up))
             {
                 paddle.MoveUp();
                 counter = 0;
             }
             else
             {
                 counter++;
                 if (counter > threshold)
                 {
                     paddle.MoveUp();
                 }
             }
         }
         if (newState.IsKeyDown(Keys.Down))
         {
             if (!oldState.IsKeyDown(Keys.Down))
             {
                 paddle.MoveDown();
                 counter = 0;
             }
             else
             {
                 counter++;
                 if (counter > threshold)
                 {
                     paddle.MoveDown();
                 }
             }
         }
         if (newState.IsKeyDown(Keys.Space))
         {
             if (!oldState.IsKeyDown(Keys.Space))
             {
                 paddle.autoMove();
                 counter = 0;
             }
             else
             {
                 counter++;
             }
         }
         oldState = newState;
     }
 }
Esempio n. 2
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            keyboardState = Keyboard.GetState();

            if (currentState == GameState.Playing)
            {
                if (keyboardState.IsKeyDown(Keys.W))
                {
                    leftPaddle.MoveUp();
                }
                if (keyboardState.IsKeyDown(Keys.S))
                {
                    leftPaddle.MoveDown();
                }
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    rightPaddle.MoveUp();
                }
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    rightPaddle.MoveDown();
                }
                if (IsKeyPressed(Keys.Space))
                {
                    currentState = GameState.Paused;
                }

                int ballUpdateValue = ball.Update(leftPaddle, rightPaddle);
                if (ballUpdateValue > 0)
                {
                    leftScore++;

                    if (leftScore == scoreToReach)
                    {
                        currentState = GameState.LeftWins;
                    }
                    else
                    {
                        ResetGame();
                    }
                }
                else if (ballUpdateValue < 0)
                {
                    rightScore++;

                    if (rightScore == scoreToReach)
                    {
                        currentState = GameState.RightWins;
                    }
                    else
                    {
                        ResetGame();
                    }
                }
            }
            else if (currentState == GameState.Paused)
            {
                if (IsKeyPressed(Keys.Space))
                {
                    currentState = GameState.Playing;
                }
            }
            else
            {
                if (IsKeyPressed(Keys.Space))
                {
                    currentState = GameState.Playing;
                    ResetEverything();
                }
            }

            lastKeyboardState = keyboardState;

            base.Update(gameTime);
        }