Advertisement
Guest User

balls

a guest
Mar 11th, 2025
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. public class Ball: Sprite
  2. {
  3. private Vector2 direction = Vector2.Zero;
  4. private Rectangle[] ballBoundaries = new Rectangle[4];
  5. private Paddle paddle;
  6. private bool isActive = false;
  7. float speed;
  8. int prevScore;
  9. private Color color = Color.DarkGray;
  10.  
  11. public Ball(Texture2D image, Vector2 position, float speed, Rectangle[] ballBoundaries, Paddle paddle) : base(image, position)
  12. {
  13. this.image = image;
  14. this.position = position;
  15. this.ballBoundaries = ballBoundaries;
  16. this.speed = speed;
  17. direction = new Vector2(0, -speed);
  18. this.paddle = paddle;
  19. }
  20.  
  21. public bool IsActive
  22. {
  23. get { return isActive; }
  24. }
  25.  
  26. public void ReverseDirectionX()
  27. {
  28. direction.X *= -1;
  29. }
  30.  
  31. public void ReverseDirectionY()
  32. {
  33. direction.Y *= -1;
  34. }
  35.  
  36. public void Launch()
  37. {
  38. isActive = true;
  39. direction.X = paddle.Velocity.X;
  40. color = Color.White;
  41. }
  42.  
  43. public void Reset()
  44. {
  45. isActive = false;
  46. direction = new Vector2(0, -speed);
  47. }
  48.  
  49. public void Update()
  50. {
  51. // no good
  52. if (collisionBox.Intersects(ballBoundaries[0]))
  53. {//right bound
  54. direction.X = Math.Abs(direction.X);
  55. }
  56.  
  57. if (collisionBox.Intersects(ballBoundaries[1]))
  58. {// left bound
  59. direction.X = -Math.Abs(direction.X);
  60. }
  61.  
  62. if (collisionBox.Intersects(ballBoundaries[2]))
  63. {// upper bound
  64. direction.Y = Math.Abs(direction.Y);
  65. }
  66. if (collisionBox.Intersects(ballBoundaries[3]))
  67. {// kill
  68. Game1.tries--;
  69. Reset();
  70. }
  71. if (collisionBox.Intersects(paddle.collisionBox))
  72. {// add back 'or equal to' if somehow nonfucntional
  73. if (collisionBox.Left >= paddle.collisionBox.Right)
  74. {// leftside collision
  75. direction.X = Math.Abs(direction.X);
  76. }
  77. if (collisionBox.Right <= paddle.collisionBox.Left)
  78. {// rightside collision
  79. direction.X = -Math.Abs(direction.X);
  80. }
  81.  
  82. if (collisionBox.Top > paddle.collisionBox.Bottom) // doesn't do shit
  83. {// if the ball somehow makes it behind the paddle
  84. direction.Y = Math.Abs(direction.Y);
  85. }
  86. if (paddle.Velocity.X != 0)
  87. {// maintain ball movement if paddle isn't moving
  88. direction.X = paddle.Velocity.X;
  89. direction.Y *= -1;
  90. }
  91. else
  92. {
  93. direction.Y *= -1;
  94. }
  95. }
  96.  
  97. if (isActive) // only move ball if active
  98. {
  99. position += direction;
  100. }
  101. else // darken ball if not active, and make it hover above paddle
  102. {
  103. position.X = paddle.position.X + 14;
  104. position.Y = paddle.position.Y - 6;
  105. color = Color.DarkGray;
  106. }
  107.  
  108. if (Game1.score % Game1.speedUpThreshold == 0 && Game1.score != 0 && Game1.score != prevScore) // speed up if score equals a certain value
  109. {
  110. speed += Game1.speedIncrement;
  111. direction.Y = speed * Math.Sign(direction.Y);
  112. }
  113. prevScore = Game1.score;
  114. }
  115. public void Draw(SpriteBatch window)
  116. {
  117. window.Draw(this.image, collisionBox, color);
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement