// Do game processing and update the display public override void Update(float fTimeSinceLastUpdateInSeconds, Graphics cBackBuffer) { // Do actions based on which state the Pong game is in switch (mePongState) { default: case EPongGameStates.Play: UpdateGameObjects(fTimeSinceLastUpdateInSeconds, cBackBuffer); DrawGame(fTimeSinceLastUpdateInSeconds, cBackBuffer); break; case EPongGameStates.Pause: DrawGame(fTimeSinceLastUpdateInSeconds, cBackBuffer); DisplayPause(fTimeSinceLastUpdateInSeconds, cBackBuffer); break; case EPongGameStates.WaitForNextBall: // Calculate how long we have waited so far mfNextBallWaitTime += fTimeSinceLastUpdateInSeconds; // If we have waited one second if (mfNextBallWaitTime > 1.0f) { // Start Playing the game again mePongState = EPongGameStates.Play; } break; } }
// Reset class variables to their default values public void Reset() { // Record that we are playing Pong meGame = EGames.Spong; // Set the games initial State mePongState = EPongGameStates.Pause; // Record that the game hasn't started yet mbGameStarted = false; // Set the Paddles initial position mrPaddle = new Rectangle(80, 250, 20, 100); // Set the Balls initial position, direction, and speed mrBall = new Rectangle(150, 250, 20, 20); mcBallDirection = new Vector2(1.0f, 0.0f); mcBallDirection.Normalize(); mcBallDirection.Scale(miBALL_START_SPEED); // Initial speed ResetBall(); // Start the player with no Score and 3 Lives miScore = 0; miLives = 3; // Set the initial Pitch Meter Indicators position mcPitchMeter.UpdatePitchIndicatorsPosition(0.5f); }
// Handle key presses public override EGames KeyDown(KeyEventArgs e) { // If the Pause button was pressed if (e.KeyCode == Keys.Space) { // If the Game is currently Paused if (mePongState == EPongGameStates.Pause) { // Switch to Playing the game mePongState = EPongGameStates.Play; // If the Game hasn't been started yet if (!mbGameStarted) { // Record that the Game has been started mbGameStarted = true; // Reset the Score to zero miScore = 0; } } // Else the Game is not Paused else { // So Pause the Game mePongState = EPongGameStates.Pause; } } // If Restart was pressed if (e.KeyCode == Keys.R) { Reset(); } // If the Mouse toggle button was pressed if (e.KeyCode == Keys.M) { // Toggle the Mouse use on/off mbMouseEnabled = !mbMouseEnabled; } return(meGame); }
// Update the Ball and Paddle private void UpdateGameObjects(float fTimeSinceLastUpdateInSeconds, Graphics cBackBuffer) { // Temp local variables int iBallXDistanceTravelled = 0; // How far the Ball has moved in the X direction int iBallYDistanceTravelled = 0; // How far the Ball has moved in the Y direction Rectangle rBallOldPosition = mrBall; // Rectangle used to test Ball against collisions Rectangle rBallCollisionRect; // Rectangle used to test Ball against collisions // Move the Pitch Meter according to any Input Pitch mcPitchMeter.AutoDetectPitchAndUpdateMeter(); // If some Pitch Input was received if (CSoundInput.Instance().bInputReceived) { // Calculate what Percent of force should be used to move the Paddle float fPercentToMove = mcPitchMeter.mfPitchIndicatorUnitPosition - 0.5f; fPercentToMove *= 2.0f; // Calculate how many pixels to move the Paddle float fAmountToMove = fPercentToMove * (-miPADDLE_MAX_SPEED_PER_SECOND * fTimeSinceLastUpdateInSeconds); // Move the Paddle mrPaddle.Y += (int)fAmountToMove; } // Calculate how far the Ball should move iBallXDistanceTravelled = (int)(mcBallDirection.X * fTimeSinceLastUpdateInSeconds); iBallYDistanceTravelled = (int)(mcBallDirection.Y * fTimeSinceLastUpdateInSeconds); // Move the Ball mrBall.X += iBallXDistanceTravelled; mrBall.Y += iBallYDistanceTravelled; // Test for collision between the Ball and the Walls // Rectangle to use to test collisions against the Ball rBallCollisionRect = mrBall; // If the Ball has travelled further than it's width or height in the last frame if (Math.Abs(iBallXDistanceTravelled) >= mrBall.Width || Math.Abs(iBallYDistanceTravelled) >= mrBall.Height) { // If the Ball is moving right if (iBallXDistanceTravelled > 0) { // If the Ball is moving down if (iBallYDistanceTravelled > 0) { rBallCollisionRect.Location = rBallOldPosition.Location; rBallCollisionRect.Width = mrBall.Right - rBallOldPosition.Left; rBallCollisionRect.Height = mrBall.Bottom - rBallOldPosition.Top; } // Else the Ball is moving up else { rBallCollisionRect.X = rBallOldPosition.X; rBallCollisionRect.Y = mrBall.Top; rBallCollisionRect.Width = mrBall.Right - rBallOldPosition.Left; rBallCollisionRect.Height = rBallOldPosition.Bottom - mrBall.Top; } } // Else the Ball is moving left else { // If the Ball is moving down if (iBallYDistanceTravelled > 0) { rBallCollisionRect.X = mrBall.Left; rBallCollisionRect.Y = rBallOldPosition.Top; rBallCollisionRect.Width = rBallOldPosition.Right - mrBall.Left; rBallCollisionRect.Height = mrBall.Bottom - rBallOldPosition.Top; } // Else the Ball is moving up else { rBallCollisionRect.Location = mrBall.Location; rBallCollisionRect.Width = rBallOldPosition.Right - mrBall.Left; rBallCollisionRect.Height = rBallOldPosition.Bottom - mrBall.Top; } } } // If the Ball hit the Top Wall if (rBallCollisionRect.IntersectsWith(mrTopWall)) { // Reverse it's Y direction mcBallDirection.Y = -mcBallDirection.Y; // Place it to be hitting the Wall (not in the Wall) mrBall.Y = mrTopWall.Bottom; // Play Sound CFMOD.PlaySound(msSoundBallBounce, false); } // If the Ball hit the Bottom Wall else if (rBallCollisionRect.IntersectsWith(mrBottomWall)) { // Reverse it's Y direction mcBallDirection.Y = -mcBallDirection.Y; // Place it to be hitting the Wall (not in the Wall) mrBall.Y = mrBottomWall.Top - mrBall.Height; // Play Sound CFMOD.PlaySound(msSoundBallBounce, false); } // If the Ball hit the Right wall if (rBallCollisionRect.IntersectsWith(mrRightWall)) { // Reverse it's X direction mcBallDirection.X = -mcBallDirection.X; // Place it to be hitting the Wall (not in the Wall) mrBall.X = mrRightWall.Left - mrBall.Width; // Play Sound CFMOD.PlaySound(msSoundBallBounce, false); } // Test for collision between the Ball and the Paddle // NOTE: Test for collision between Paddle before left wall incase ball is // travelling too fast and would pass through both if (rBallCollisionRect.IntersectsWith(mrPaddle)) { // Increase the Balls speed slightly float fSpeed = mcBallDirection.Length(); fSpeed += miBALL_SPEED_INCREMENT; // Make sure Ball does not go too fast if (fSpeed > miBALL_MAX_SPEED) { fSpeed = miBALL_MAX_SPEED; } // Reverse the Balls X direction mcBallDirection.X = -mcBallDirection.X; // Add some randomness to the Balls Y direction float fRandomness = mcRandom.Next(-25, 25); fRandomness /= 100.0f; mcBallDirection.Normalize(); mcBallDirection.Y += fRandomness; // Set the Balls speed mcBallDirection.Normalize(); mcBallDirection.Scale(fSpeed); // Place the Ball on the Paddle (not inside it) mrBall.X = mrPaddle.Right; // Play Sound CFMOD.PlaySound(msSoundBallBounce, false); } // Else if the Ball hit the Left wall else if (rBallCollisionRect.IntersectsWith(mrLeftWall)) { // Player loses a life miLives--; // If the Player is dead if (miLives == 0) { // Save the Players Score int iScore = miScore; // Restart the Game Reset(); // Restore the Players Score miScore = iScore; // Play Game Over sound CFMOD.PlaySound(msSoundGameOver, false); // Check if the Player has beat their High Score if (CProfiles.Instance().mcCurrentProfile.iScoreSpong < miScore) { // Save the Players new High Score CProfiles.Instance().mcCurrentProfile.iScoreSpong = miScore; CProfiles.Instance().SaveProfilesToFile(); } // Exit this function so the Game restarts now return; } // Play sound of losing a Ball CFMOD.PlaySound(msSoundBallDied, false); // Reset the Balls position and direction ResetBall(); // Reset the Next Ball Wait Timer mfNextBallWaitTime = 0.0f; // Enter the Wait For Next Ball state mePongState = EPongGameStates.WaitForNextBall; } // Update the Players Score miScore += (int)(fTimeSinceLastUpdateInSeconds * 1000.0f); }