Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ball
- {
- ....
- private Rectangle ballRect;
- ....
- public Ball(args)
- {
- ....
- ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
- ....
- }
- /// <summary>
- /// Updates position of the ball. Used in Update() for GameplayScreen.
- /// </summary>
- public void UpdatePosition(GameTime gameTime)
- {
- ballRect.X = (int)ballPosition.X;
- ballRect.Y = (int)ballPosition.Y;
- oldBallPos.X = ballPosition.X;
- oldBallPos.Y = ballPosition.Y;
- ballPosition.X += speed * ((float)Math.Cos(direction));
- ballPosition.Y += speed * ((float)Math.Sin(direction));
- bool collided = CheckWallHit();
- // Stops the issue where ball was oscillating on the ceiling or floor
- if (collided)
- {
- ballPosition.X = oldBallPos.X + speed * (float)Math.Cos(direction);
- ballPosition.Y = oldBallPos.Y + speed * (float)Math.Sin(direction);
- }
- }
- /// <summary>
- /// Checks for collision with the ceiling or floor. 2*Math.pi = 360 degrees
- /// </summary>
- private bool CheckWallHit()
- {
- while (direction > 2 * Math.PI)
- {
- direction -= 2 * Math.PI;
- return true;
- }
- while (direction < 0)
- {
- direction += 2 * Math.PI;
- return true;
- }
- if (ballPosition.Y <= 0 || (ballPosition.Y > resetBallPos.Y * 2 - ballRect.Height))
- {
- direction = 2 * Math.PI - direction;
- return true;
- }
- return true;
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- public class bat
- {
- ....
- public Rectangle batRect;
- ....
- public bat(args)
- {
- ....
- batRect = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
- ....
- }
- /// <summary>
- /// Updates the position of the AI bat, in order to track the ball
- /// </summary>
- public virtual void UpdatePosition(Ball ball, GameTime gameTime)
- {
- ...
- batRect.X = (int)BatPosition.X;
- batRect.Y = (int)BatPosition.Y;
- ....
- }
- }
RAW Paste Data