// Called every frame, passing in `dt` since the last frame. `dt` // is short for `deltaTime` and is measured in seconds.Multiplying // this by any changes we wish to make in our game will allow our // game to perform consistently across all hardware; otherwise, any // changes we make will be applied as fast as possible and will vary // across system hardware. public override async Task Update(TimeSpan dt) { switch (gameState) { case GameState.Serve: // before switching to play, initialize ball's velocity based // on player who last scored ball.Dy = Random.Next(-50, 50); if (servingPlayer == 1) { ball.Dx = Random.Next(140, 200); } else { ball.Dx = -Random.Next(140, 200); } break; case GameState.Play: // detect ball collision with paddles, reversing dx if true and // slightly increasing it, then altering the dy based on the position // at which it collided, then playing a sound effect if (ball.Collides(player1)) { ball.Dx = -ball.Dx * 1.03; ball.X = player1.X + 5; // keep velocity going in the same direction, but randomize it if (ball.Dy < 0) { ball.Dy = -Random.Next(10, 150); } else { ball.Dy = Random.Next(10, 150); } await Sounds["paddle_hit"].Play(); } if (ball.Collides(player2)) { ball.Dx = -ball.Dx * 1.03; ball.X = player2.X - 4; // keep velocity going in the same direction, but randomize it if (ball.Dy < 0) { ball.Dy = -Random.Next(10, 150); } else { ball.Dy = Random.Next(10, 150); } await Sounds["paddle_hit"].Play(); } // detect upper and lower screen boundary collision, playing a sound // effect and reversing dy if true if (ball.Y <= 0) { ball.Y = 0; ball.Dy = -ball.Dy; await Sounds["wall_hit"].Play(); } // -4 to account for the ball's size if (ball.Y >= VirtualHeight - 4) { ball.Y = VirtualHeight - 4; ball.Dy = -ball.Dy; await Sounds["wall_hit"].Play(); } // if we reach the left edge of the screen, go back to serve // and update the score and serving player if (ball.X < 0) { servingPlayer = 1; player2Score++; await Sounds["score"].Play(); // if we've reached a score of 10, the game is over; set the // state to done so we can show the victory message if (player2Score == 10) { winningPlayer = 2; gameState = GameState.Done; } else { gameState = GameState.Serve; // places the ball in the middle of the screen, no velocity ball.Reset(); } } // if we reach the right edge of the screen, go back to serve // and update the score and serving player if (ball.X > VirtualWidth) { servingPlayer = 2; player1Score++; await Sounds["score"].Play(); // if we've reached a score of 10, the game is over; set the // state to done so we can show the victory message if (player1Score == 10) { winningPlayer = 1; gameState = GameState.Done; } else { gameState = GameState.Serve; // places the ball in the middle of the screen, no velocity ball.Reset(); } } break; } // // paddles can move no matter what state we're in // // player 1 if (Keyboard.IsDown(Key.W)) { player1.Dy = -PaddleSpeed; } else if (Keyboard.IsDown(Key.S)) { player1.Dy = PaddleSpeed; } else { player1.Dy = 0; } // player 2 if (Keyboard.IsDown(Key.Up)) { player2.Dy = -PaddleSpeed; } else if (Keyboard.IsDown(Key.Down)) { player2.Dy = PaddleSpeed; } else { player2.Dy = 0; } // update our ball based on its DX and DY only if we're in play state; // scale the velocity by dt so movement is framerate - independent if (gameState == GameState.Play) { ball.Update(dt); } player1.Update(dt); player2.Update(dt); }
private void ResetGame() { leftPaddle.Reset(); rightPaddle.Reset(); ball.Reset(); }