// controlling the ball bounch direction after collide with the bar public void ballBounceDirection(ref BallSprite ball, ref UserControlSprite player, Game game ) { // if the ball hit the left side of the screen if (ball.position.X < 0) { //If the X == 0 && Y == 0 if (ball.getSignOfBallX() == 0 && ball.getSignOfBallY() == 0) { // Set ball X && Y to 0 ball.resetSignOfBall(); // Set X == 1 && Y == -1 ball.setSignOfBall(1, -1); } else { ball.resetSignOfBall(); ball.setSignOfBall(1,1); } // reverse the X direction of the ball ball.speed.X *= -1; } // if the ball hit the right side of the screen else if (ball.position.X > game.Window.ClientBounds.Width - ball.frameSize.X) { //If the X == 0 && Y == 0 if ( ball.getSignOfBallX() == 0 && ball.getSignOfBallY() == 0) { ball.resetSignOfBall(); ball.setSignOfBall(1,-1); } else { ball.resetSignOfBall(); ball.setSignOfBall(-1,1); } // reverse the X direction of the ball ball.speed.X *= -1; } // if the y position of the ball hit the top of the screen else if (ball.position.Y < 0) { //If the X == -1 && Y == -1 if ( ball.getSignOfBallX() < 0 && ball.getSignOfBallY() < 0) { ball.resetSignOfBall(); ball.setSignOfBall(-1,1); } //If the X == 1 && Y == -1 else if (ball.getSignOfBallX() > 0 && ball.getSignOfBallY() < 0) { ball.resetSignOfBall(); ball.setSignOfBall(1,1); } // reverse the Y direction of the ball ball.speed.Y *= -1; } // if the ball collide with bar if (ball.collisionRect.Intersects(player.collisionRect)) { // if the bar is stationary // reverse the Y direction of the ball if (player.getSignOfBarSpeed() == 0) ball.speed.Y *= -1; // if the bar is comning to the right else if (player.getSignOfBarSpeed() > 0) { // If the X == 1 && Y == 1 if (ball.getSignOfBallX() > 0 && ball.getSignOfBallY() > 0) { // reverse the Y direction of the ball ball.speed.Y *= -1; } // if the sign of the ball, x and y, are both 0 else if (ball.getSignOfBallX() == 0 && ball.getSignOfBallY() == 0) { // reverse the Y direction of the ball ball.speed.Y *= -1; } else { // reverse the direction of the ball ball.speed *= -1; } ball.resetSignOfBall(); } // if the bar is comning to the from the left, bounce the ball back to the left else if (player.getSignOfBarSpeed() < 0) { //If the X == -1 && Y == 1 if (ball.getSignOfBallX() < 0 && ball.getSignOfBallY() > 0) // reverse the Y direction of the ball ball.speed.Y *= -1; else if (ball.getSignOfBallX() == 0 && ball.getSignOfBallY() == 0) // reverse the Y direction of the ball ball.speed.Y *= -1; else // reverse the direction of the ball ball.speed *= -1; } ball.resetSignOfBall(); } }
protected override void LoadContent() { spriteBatch = new SpriteBatch(Game.GraphicsDevice); // instantiate Util class util = new Util(); front = Game.Content.Load<SpriteFont>("Front"); explosionImage = Game.Content.Load<Texture2D>(@"Images/explosion"); // Set Bar at middile of the screen putBarAtMiddleX = (float)(( Game.Window.ClientBounds.Width - playerFrameSizeX ) >> 1); // Set Bar at the Bottom putBarAtBottomY = (float)( Game.Window.ClientBounds.Height - (playerFrameSizeY << 1)); // Set ball at the middle of the screen putBallAtMiddleX = (float)(( Game.Window.ClientBounds.Width - ballFrameSizeX ) >> 1); // Set ball on top of the bar putBallOnTopBarY = putBarAtBottomY - (float)ballFramSizeY; // Create the fireball ball = new BallSprite( Game.Content.Load<Texture2D>( @"Images/FlameBall" ), new Vector2( putBallAtMiddleX, putBallOnTopBarY ), new Point( ballFrameSizeX, ballFramSizeY ), 0, new Point(0,0), new Point(6,8), Vector2.Zero); // Create the bar player = new UserControlSprite( Game.Content.Load<Texture2D>( @"Images/bar" ), new Vector2( putBarAtMiddleX, putBarAtBottomY ), new Point( playerFrameSizeX, playerFrameSizeY ), 10, new Point(0, 0), new Point(1,1), new Vector2(7,0)); // load the bricks util.LoadBricks(Game, ref spriteBricksList, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height, brickLength,brickWidth, ref numOfBricks); base.LoadContent(); }
// Reset the ball to the middle of the screen public void resetBallPositionMiddle(ref BallSprite ball, float putBallAtMiddleX, float putBallOnTopBarY) { // the previous speed is not 0, set the speed to 0 if (ball.speed.X != 0 || ball.speed.Y != 0) { // set ball speed to 0 ball.speed = Vector2.Zero; // put the ball on top of the bar in the middle of the screen ball.position = new Vector2(putBallAtMiddleX, putBallOnTopBarY); } }