//********************************************************************************************************************************************* // // Method Name: NewMatch // // Description: // Resets the ball position to be center of the paddle (directly above the paddle) and the velocity. The ball must also be relaunched. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* public void NewMatch() { // Reset the paddle to be in the centered horizontally. mHitBox.X = (BreakoutConstants.SCREEN_PLAY_AREA_WIDTH / BreakoutConstants.HALF) - (mHitBox.Width / BreakoutConstants.HALF); mHitBox.Y = BreakoutConstants.SCREEN_PLAY_AREA_HEIGHT - BreakoutConstants.PADDLE_BOUNDARY_PADDING - mHitBox.Height; // On the start of a new match the ball has no velocity since it has not been launched. Vector.SetComponentX(BreakoutConstants.BALL_INITIAL_SPEED_X); Vector.SetComponentY(BreakoutConstants.BALL_INITIAL_SPEED_Y); // On the start of a new match the ball has not been launched yet. mBallLaunched = false; }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnPaddle // // Description: // Determines if the ball has collided with the paddle. If the center of the ball is within the width of the paddle then ball will reverse // in its Y velocity. Otherwise the ball hit the side or corner of the paddle and the X and Y velocity are reversed. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void CheckBallCollisionOnPaddle(BreakoutGame theBreakoutGame) { // Check if the ball hits the paddle. if (mHitBox.IntersectsWith(theBreakoutGame.Paddle.HitBox)) { // Keep the ball above the paddle to avoid the ball getting stuck inside the paddle. mHitBox.Y = theBreakoutGame.Paddle.HitBox.Y - mHitBox.Height; mInnerHitDetection.Y = mHitBox.Y + mInnerHitPadding; // The Y (vertical) component is always reversed on a paddle hit. Vector.ReverseComponentY(); // Transfer a percentage of the paddles velocity to the ball. This helps the player to change to angle of the ball with the paddle. Vector.SetComponentX(Vector.ComponentX + (theBreakoutGame.Paddle.Vector.ComponentX * BreakoutConstants.PADDLE_VECTOR_TRANSFER_PERCENTAGE)); } }
//********************************************************************************************************************************************* // // Method Name: Update // // Description: // Update the paddle based on if the left/right movement buttons are pressed. The left movement takes precedence over the right movement. The // paddle is restricted within the bounds of the left and right borders. If the ball has not been launched yet, the ball will follow the // paddle movement. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* public override void Update() { // Check if the left button for the paddle is currently being pressed down. // If true: draw the paddle further left by the paddle speed. The ball will follow the paddle if the ball has not been launched yet. if (mMovePaddleLeft == true) { // Increase the paddles horizontal speed. Vector.SetComponentX(Vector.ComponentX - BreakoutConstants.PADDLE_SPEED_INCREASE); // Limit the horizontal speed to its maximum. if (Vector.ComponentX < -BreakoutConstants.PADDLE_MAX_SPEED) { Vector.SetComponentX(-BreakoutConstants.PADDLE_MAX_SPEED); } // Move the paddle left. mHitBox.X += (int)Math.Round(Vector.ComponentX); // The ball follows the paddle's left movement if the ball has not been launched yet. if (mBreakoutGame.Ball.BallLaunched == false) { mBreakoutGame.Ball.SetBallCoordinateX(mBreakoutGame.Ball.HitBox.X + (int)Math.Round(Vector.ComponentX)); } // Prevent the paddle and unlaunched ball from moving further left if the paddle reaches the left border by setting their positions // X-Coordinate positions to be furthest left before exiting the screen. if (mHitBox.X < BreakoutConstants.SCREEN_X_COORDINATE_LEFT) { mHitBox.X = BreakoutConstants.SCREEN_X_COORDINATE_LEFT; if (mBreakoutGame.Ball.BallLaunched == false) { // The ball i placed center horizontally from the paddle. mBreakoutGame.Ball.SetBallCoordinateX(mHitBox.X + (mHitBox.Width / BreakoutConstants.HALF) - (mBreakoutGame.Ball.HitBox.Width / BreakoutConstants.HALF)); } } } // Check if the right button for the paddle is currently being pressed down. // If true: draw the paddle further right by the paddle speed. The ball will follow the paddle if the ball has not been launched yet. else if (mMovePaddleRight == true) { // Increase the paddles horizontal speed. Vector.SetComponentX(Vector.ComponentX + BreakoutConstants.PADDLE_SPEED_INCREASE); // Limit the horizontal speed to its maximum. if (Vector.ComponentX > BreakoutConstants.PADDLE_MAX_SPEED) { Vector.SetComponentX(BreakoutConstants.PADDLE_MAX_SPEED); } // Move the paddle right. mHitBox.X += (int)Math.Round(Vector.ComponentX); // The ball follows the paddle's right movement if the ball has not been launched yet. if (mBreakoutGame.Ball.BallLaunched == false) { mBreakoutGame.Ball.SetBallCoordinateX(mBreakoutGame.Ball.HitBox.X + (int)Math.Round(Vector.ComponentX)); } // Prevent the paddle and unlaunched ball from moving further right if the paddle reaches the right border by setting their positions // X-Coordinate positions to be furthest right before exiting the screen. if (mHitBox.X > (BreakoutConstants.SCREEN_PLAY_AREA_WIDTH - mHitBox.Width)) { mHitBox.X = BreakoutConstants.SCREEN_PLAY_AREA_WIDTH - mHitBox.Width; if (mBreakoutGame.Ball.BallLaunched == false) { // The ball i placed center horizontally from the paddle. mBreakoutGame.Ball.SetBallCoordinateX(mHitBox.X + (mHitBox.Width / BreakoutConstants.HALF) - (mBreakoutGame.Ball.HitBox.Width / BreakoutConstants.HALF)); } } } else { Vector.SetComponentX(0.0F); } }