Exemplo n.º 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            MouseState mouse = Mouse.GetState();

            switch (CurrentGameState)
            {
            case GameState.MainMenu:
                if (btnPlay.isClicked == true)
                {
                    CurrentGameState = GameState.Playing;
                }
                if (btnExit.isClicked == true)
                {
                    Exit();
                }
                btnPlay.Update(mouse);
                btnExit.Update(mouse);
                break;

            case GameState.Playing:
                //player 1 movement
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    player1.KeyUp();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    player1.KeyDown();
                }

                //player 2 movement
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                {
                    player2.KeyUp();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    player2.KeyDown();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Space) && !ball.gameRun)
                {
                    ball.gameRun = true;
                }
                if (ball.gameRun)
                {
                    ball.Update(gameTime);



                    //player collision logic
                    if (ball.dirx > 0)
                    {
                        if (ball.posy >= player2.posY && ball.posy + ballSize < player2.posY + batheigth && ball.posx + ballSize >= player2.posX)
                        {
                            ball.dirx = -ball.dirx;
                        }
                    }

                    else if (ball.dirx < 0)
                    {
                        if (ball.posy >= player1.posY && ball.posy + ballSize <= player1.posY + batheigth && ball.posx <= player1.posX + batwidth)
                        {
                            ball.dirx = -ball.dirx;
                        }
                    }
                    if (score1 == 10 || score2 == 10)
                    {
                        ball.gameRun     = false;
                        CurrentGameState = GameState.GameOver;
                        if (score1 < score2)
                        {
                            winner = "Player 2";
                        }
                        else
                        {
                            winner = "Player 1";
                        }
                    }
                }
                break;
            }

            base.Update(gameTime);
        }