Guest User

Untitled

a guest
Jul 28th, 2012
24
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public class Ball
  2. {
  3.     ....
  4.         private Rectangle ballRect;
  5.     ....
  6.  
  7.   public Ball(args)
  8.     {  
  9.    
  10.     ....
  11.     ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
  12.     ....   
  13.     }
  14.  
  15.    /// <summary>
  16.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  17.         /// </summary>
  18.         public void UpdatePosition(GameTime gameTime)
  19.         {
  20.         ballRect.X = (int)ballPosition.X;
  21.             ballRect.Y = (int)ballPosition.Y;
  22.             oldBallPos.X = ballPosition.X;
  23.             oldBallPos.Y = ballPosition.Y;
  24.        
  25.             ballPosition.X += speed * ((float)Math.Cos(direction));
  26.  
  27.             ballPosition.Y += speed * ((float)Math.Sin(direction));
  28.             bool collided = CheckWallHit();
  29.  
  30.             // Stops the issue where ball was oscillating on the ceiling or floor
  31.             if (collided)
  32.             {
  33.                 ballPosition.X = oldBallPos.X + speed * (float)Math.Cos(direction);
  34.                 ballPosition.Y = oldBallPos.Y + speed * (float)Math.Sin(direction);
  35.             }      
  36.         }
  37.  
  38.   /// <summary>
  39.         /// Checks for collision with the ceiling or floor. 2*Math.pi = 360 degrees
  40.         /// </summary>
  41.         private bool CheckWallHit()
  42.         {
  43.             while (direction > 2 * Math.PI)
  44.             {
  45.                 direction -= 2 * Math.PI;
  46.                 return true;
  47.             }
  48.  
  49.             while (direction < 0)
  50.             {
  51.                 direction += 2 * Math.PI;
  52.                 return true;
  53.             }
  54.  
  55.             if (ballPosition.Y <= 0 || (ballPosition.Y > resetBallPos.Y * 2 - ballRect.Height))
  56.             {
  57.                 direction = 2 * Math.PI - direction;
  58.                 return true;
  59.             }
  60.             return true;
  61.         }
  62.  
  63. }
  64.  
  65. //////////////////////////////////////////////////////////////////////////////
  66.  
  67. public class bat
  68. {
  69.     ....
  70.         public Rectangle batRect;
  71.     ....
  72.  
  73.     public bat(args)
  74.     {
  75.     ....
  76.         batRect = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  77.     ....
  78.     }
  79.  
  80.        /// <summary>
  81.         /// Updates the position of the AI bat, in order to track the ball
  82.         /// </summary>
  83.         public virtual void UpdatePosition(Ball ball, GameTime gameTime)
  84.         {
  85.             ...
  86.         batRect.X = (int)BatPosition.X;
  87.             batRect.Y = (int)BatPosition.Y;
  88.         ....
  89.     }
  90. }
RAW Paste Data