예제 #1
0
파일: GameMain.cs 프로젝트: mayur70/Pong
        protected override void Update(GameTime gameTime)
        {
            if (input.GamePadState.Buttons.Back == ButtonState.Pressed || input.KeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // start management
            if (input.KeyboardState.IsKeyDown(Keys.Enter))
            {
                if (gameState == GameState.Start)
                {
                    gameState = GameState.Serve;
                }
                else if (gameState == GameState.Serve)
                {
                    gameState = GameState.Play;
                }
                else if (gameState == GameState.Done)
                {
                    gameState = GameState.Serve;

                    ball.Reset();

                    player1Score = 0;
                    player2Score = 0;

                    if (winningPlayer == 1)
                    {
                        servingPlayer = 2;
                    }
                    else
                    {
                        servingPlayer = 1;
                    }
                }
            }


            if (gameState == GameState.Serve)
            {
                ball.dy = random.Next(-50, 50);
                if (servingPlayer == 1)
                {
                    ball.dx = random.Next(140, 200);
                }
                else
                {
                    ball.dx = -random.Next(140, 200);
                }
            }
            else if (gameState == GameState.Play)
            {
                if (ball.Collides(player1))
                {
                    ball.dx = -ball.dx * 1.03f;
                    ball.x  = player1.x + player1.transform.Width;

                    paddleHitSound.Play();

                    if (ball.dy < 0)
                    {
                        ball.dy = -random.Next(10, 150);
                    }
                    else
                    {
                        ball.dy = random.Next(10, 150);
                    }
                }

                if (ball.Collides(player2))
                {
                    ball.dx = -ball.dx * 1.03f;
                    ball.x  = player2.x - ball.transform.Width;

                    paddleHitSound.Play();

                    if (ball.dy < 0)
                    {
                        ball.dy = -random.Next(10, 150);
                    }
                    else
                    {
                        ball.dy = random.Next(10, 150);
                    }
                }

                if (ball.y < 0)
                {
                    ball.y  = 0;
                    ball.dy = -ball.dy;

                    wallHitSound.Play();
                }
                if (ball.y > VIRTUAL_HEIGHT - ball.transform.Height)
                {
                    ball.y  = VIRTUAL_HEIGHT - ball.transform.Height;
                    ball.dy = -ball.dy;

                    wallHitSound.Play();
                }

                if (ball.x < 0)
                {
                    servingPlayer = 1;
                    player2Score++;

                    scoreSound.Play();

                    if (player2Score == 10)
                    {
                        winningPlayer = 2;
                        gameState     = GameState.Done;
                    }
                    else
                    {
                        ball.Reset();
                        gameState = GameState.Serve;
                    }
                }
                if (ball.x > VIRTUAL_WIDTH)
                {
                    servingPlayer = 2;
                    player1Score++;

                    scoreSound.Play();

                    if (player1Score == 10)
                    {
                        winningPlayer = 1;
                        gameState     = GameState.Done;
                    }
                    else
                    {
                        ball.Reset();
                        gameState = GameState.Serve;
                    }
                }
            }

            if (input.KeyboardState.IsKeyDown(Keys.W))
            {
                player1.dy = -PADDLE_SPEED;
            }
            else if (input.KeyboardState.IsKeyDown(Keys.S))
            {
                player1.dy = PADDLE_SPEED;
            }
            else
            {
                player1.dy = 0;
            }

            if (input.KeyboardState.IsKeyDown(Keys.Up))
            {
                player2.dy = -PADDLE_SPEED;
            }
            else if (input.KeyboardState.IsKeyDown(Keys.Down))
            {
                player2.dy = PADDLE_SPEED;
            }
            else
            {
                player2.dy = 0;
            }

            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (gameState == GameState.Play)
            {
                ball.Update(delta);
            }


            player1.Update(delta);
            player2.Update(delta);

            base.Update(gameTime);
        }