// If a Keyboard Key is pressed public override EGames KeyDown(KeyEventArgs e) { // Game to load EGames eGameToPlay = meGame; // If the player is switching the selection if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) { // Switch selection to previous game meSelectedGame--; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Wrap around if necessary if (meSelectedGame == EGames.MainMenu) { meSelectedGame = EGames.HighScores; } // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } else if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Down) { // Switch selection to next game meSelectedGame++; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Wrap around if necessary if (meSelectedGame > EGames.HighScores) { meSelectedGame = EGames.Spong; } // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } // If the player has chosen the Game to play if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space) { eGameToPlay = meSelectedGame; } // If the player is toggling the Sound on/off if (e.KeyCode == Keys.S) { // Toggle the Sound on/off CFMOD.mbSoundOn = !CFMOD.mbSoundOn; } // Return which Game should be played now return(eGameToPlay); }
// Function to Fire the Arrow private void FireArrow() { // If the Player is currently Aiming if (meTargetPracticeState == ETargetPracticeGameStates.Aiming) { // Switch to Shooting the Arrow meTargetPracticeState = ETargetPracticeGameStates.FiredArrow; // Set the Arrows Velocity based on the current Pitch and Amplitude of the Players voice // Find the Middle Point of the Bow And Arrow to Rotate around PointF sMiddlePoint = new Point(); sMiddlePoint.X = mrBOW_AND_ARROW_POSITION.X + (mrBOW_AND_ARROW_POSITION.Width / 2.0f); sMiddlePoint.Y = mrBOW_AND_ARROW_POSITION.Y + (mrBOW_AND_ARROW_POSITION.Height / 2.0f); // Rotate the Bow And Arrow by the specified Bow And Arrow Rotation Amount System.Drawing.Drawing2D.Matrix sRotationMatrix = new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0); sRotationMatrix.RotateAt(miBowAndArrowRotation, sMiddlePoint); PointF[] sPoint = new PointF[1]; sPoint[0].X = sMiddlePoint.X + 1; sPoint[0].Y = sMiddlePoint.Y; sRotationMatrix.TransformPoints(sPoint); // Set the Arrows initial Direction to travel mcArrowVelocity.X = sPoint[0].X - sMiddlePoint.X; mcArrowVelocity.Y = sPoint[0].Y - sMiddlePoint.Y; mcArrowVelocity.Normalize(); // Set the Arrows initial speed mcArrowVelocity *= (mfUnitBowAndArrowPower * 300.0f); mcArrowVelocity.X += 100.0f; // Set the Arrows Rotation to be the same as the Bow And Arrow initially miArrowRotation = miBowAndArrowRotation; // Play the Fire Arrow sound CFMOD.PlaySound(msSoundFire, false); } }
// If the Mouse is moved public override void MouseMove(MouseEventArgs e) { // Get Mouses Position and store it as a rectangle Rectangle rMouse = new Rectangle(e.X, e.Y, 1, 1); // If the Mouse is over the Pong game icon if (mrPongIcon.IntersectsWith(rMouse) && meSelectedGame != EGames.Spong) { // Set Pong as the Selected game meSelectedGame = EGames.Spong; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } // Else if the Mouse is over the Shooting Gallery game icon else if (mrShootingGalleryIcon.IntersectsWith(rMouse) && meSelectedGame != EGames.ShootingGallery) { // Set Shooting Gallery as the Selected game meSelectedGame = EGames.ShootingGallery; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } // Else if the Mouse is over the Pitch Matcher game icon else if (mrPitchMatcherIcon.IntersectsWith(rMouse) && meSelectedGame != EGames.PitchMatcher) { // Set Pitch Matcher as the Selected game meSelectedGame = EGames.PitchMatcher; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } // Else if the Mouse is over the Target Practice game icon else if (mrTargetPracticeIcon.IntersectsWith(rMouse) && meSelectedGame != EGames.TargetPractice) { // Set Target Practice as the Selected game meSelectedGame = EGames.TargetPractice; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } // Else if the Mouse is over the Profile Settings icon else if (mrProfileSettingsIcon.IntersectsWith(rMouse) && meSelectedGame != EGames.ProfileSettings) { // Set the Profile Settings as the Selected icon meSelectedGame = EGames.ProfileSettings; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } // Else if the Mouse is over the High Scores icon else if (mrHighScoresIcon.IntersectsWith(rMouse) && meSelectedGame != EGames.HighScores) { // Set High Scores as the Selected game meSelectedGame = EGames.HighScores; // Reset the Game Scale mfSelectedGameScale = 0.0f; // Play Menu Selection Changed sound CFMOD.PlaySound(msSoundSelectionChanged, false); } }
// Update the Bow And Arrow and the Arrow private void UpdateGameObjects(float fTimeSinceLastUpdateInSeconds, Graphics cBackBuffer) { // If the Player is Aiming if (meTargetPracticeState == ETargetPracticeGameStates.Aiming) { // Use the Players current Pitch as the Bow And Arrow Rotation Amount mcPitchMeter.AutoDetectPitchAndUpdateMeter(); miBowAndArrowRotation = (int)(mcPitchMeter.mfPitchIndicatorUnitPosition * -45.0f); // Save the amount of Power being used mfUnitBowAndArrowPower = ((CSoundInput.Instance().fAmplitude - 90.0f) / 20.0f); // Make sure the Power is in a valid range of Zero to One if (mfUnitBowAndArrowPower < 0.0f) { mfUnitBowAndArrowPower = 0.0f; } else if (mfUnitBowAndArrowPower > 1.0f) { mfUnitBowAndArrowPower = 1.0f; } } // Else the Arrow has been fired else { // If the Arrow is not "dead" yet if (mfResetArrowWaitTime <= 0.0f) { // Update the Arrows Velocity by Gravity mcArrowVelocity += (mcGRAVITY_ACCELERATION * fTimeSinceLastUpdateInSeconds); // Calculate the Arrows new Position mrArrow.X += (mcArrowVelocity.X * fTimeSinceLastUpdateInSeconds); mrArrow.Y += (mcArrowVelocity.Y * fTimeSinceLastUpdateInSeconds); // Get the Rotation angle of the Arrow so that it faces the direction it is travelling float fArrowRotationInRadians = 0.0f; bool bChangeRotation = true; if (mcArrowVelocity.Y < 0.0f) { fArrowRotationInRadians = (float)(Math.PI + Math.Atan(mcArrowVelocity.X / mcArrowVelocity.Y)); } else if (mcArrowVelocity.Y > 0.0f) { fArrowRotationInRadians = (float)(Math.Atan(mcArrowVelocity.X / mcArrowVelocity.Y)); } // Else the Arrow has no Y Velocity else { if (mcArrowVelocity.X > 0.0f) { fArrowRotationInRadians = (float)(Math.PI / 2.0f); } else if (mcArrowVelocity.X < 0.0f) { fArrowRotationInRadians = (float)((3.0f * Math.PI) / 2.0f); } // Else the Arrow has no X Velocity else { // Rotation is undefined, so leave rotationa as is bChangeRotation = false; } } // If the Rotation should be updated if (bChangeRotation) { // Convert the Radians to Degrees and store the result miArrowRotation = 90 - (int)(fArrowRotationInRadians * (180.0f / Math.PI)); } } // If the Arrow should be reset if (mrArrow.IntersectsWith(mrTarget) || mrArrow.X > 800 || mrArrow.Y > 485) { // If the Arrow just "died" if (mfResetArrowWaitTime == 0.0f) { // If the Arrow hit the Target if (mrArrow.IntersectsWith(mrTarget)) { // Record it as a hit mbArrowHitTarget = true; // Update the Players Score miScore++; // If the Players Score is more than their current High Score if (miScore > CProfiles.Instance().mcCurrentProfile.iScoreTargetPractice) { // Update the Players High Score and Save CProfiles.Instance().mcCurrentProfile.iScoreTargetPractice = miScore; CProfiles.Instance().SaveProfilesToFile(); } // Play the Hit sound CFMOD.PlaySound(msSoundHit, false); } // Else if the Arrow did not hit anything else if (mrArrow.X > 800 || mrArrow.Y > 485) { // Record that it was not a hit mbArrowHitTarget = false; // Reset the Players Score miScore = 0; // Play the Miss sound CFMOD.PlaySound(msSoundMiss, false); } // Reset the Arrows Velocity to zero mcArrowVelocity *= 0.0f; } // Update how long we have waited for mfResetArrowWaitTime += fTimeSinceLastUpdateInSeconds; // If we have waited long enough since the Arrow "died" if (mfResetArrowWaitTime > 1.0f) { // Reset the Arrows Position MoveArrowToBow(); // If the Arrow hit the Target if (mbArrowHitTarget) { // Create a new Target to hit MoveTargetToNewRandomLocation(); } // Reset the Arrow Wait Time mfResetArrowWaitTime = 0.0f; // Change the Game State to Aiming meTargetPracticeState = ETargetPracticeGameStates.Aiming; } } } }
// If the Pitch Transition Example button was pressed private void buttonPitchTransitionExample_Click(object sender, EventArgs e) { // Play the Pitch Transition Example sound CFMOD.PlaySound(msSoundPitchTransitionExample, true); }
// 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); }