Guest User

Untitled

a guest
Dec 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. private void moveBall(GOval ball, int height, int width, GRect paddle) {
  2. // Update ball position
  3. ball.setLocation(ball.getX() + vx, ball.getY() + vy);
  4.  
  5. // Make sure ball Y is within the area
  6. if (ball.getY() <= 0) {
  7. ball.setLocation(ball.getX(), 0);
  8. vx += getVelOff();
  9. vy = Math.abs(vy);
  10. } else if (ball.getY() + BALL_RADIUS * 2 >= height) {
  11. // The ball has reached the bottom of the screen
  12. turn ++;
  13. ball.setLocation(width / 2, height / 2);
  14. // TODO: Wait for the player to be ready
  15. }
  16.  
  17. // Make sure ball X is within the area
  18. if (ball.getX() <= 0) {
  19. ball.setLocation(0, ball.getY());
  20. vx = Math.abs(vx);
  21. vy += getVelOff();
  22. } else if (ball.getX() + BALL_RADIUS * 2 >= width) {
  23. ball.setLocation(width - BALL_RADIUS * 2, ball.getY());
  24. vx = -Math.abs(vx);
  25. vy += getVelOff();
  26. }
  27.  
  28. // Get nearby objects
  29. GObject yn = getElementAt(ball.getX() + BALL_RADIUS, ball.getY());
  30. GObject yp = getElementAt(ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS * 2);
  31. GObject xn = getElementAt(ball.getX(), ball.getY() + BALL_RADIUS);
  32. GObject xp = getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS);
  33. GObject center = getElementAt(ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS);
  34.  
  35. // yn collision
  36. if (
  37. // check if yn exists
  38. yn != null &&
  39. // check if the ball's bottom surface is below or at yn's top surface
  40. ball.getY() <= yn.getY() + yn.getHeight() &&
  41. // check if the ball's x location is within yn's bounds
  42. ball.getX() + BALL_RADIUS * 2 >= yn.getX() && ball.getX() <= yn.getX() + yn.getWidth()
  43. ) {
  44. // The ball has hit the top of the yn brick
  45. ball.setLocation(ball.getX(), yn.getY() + yn.getHeight());
  46. vx += getVelOff();
  47. vy = Math.abs(vy);
  48. remove(yn);
  49. }
  50.  
  51. // yp collision
  52. if (
  53. // check if yp exists
  54. yp != null &&
  55. // check if the ball's top surface is above or at yp's bottom surface
  56. ball.getY() + BALL_RADIUS * 2 >= yp.getY() &&
  57. // check if the ball's x location is within yp's bounds
  58. ball.getX() + BALL_RADIUS * 2 >= yp.getX() && ball.getX() <= yp.getX() + yp.getWidth()
  59. ) {
  60. // The ball has hit the bottom of the yp brick
  61. ball.setLocation(ball.getX(), yp.getY() - BALL_RADIUS * 2);
  62. vx += getVelOff();
  63. vy = -Math.abs(vy);
  64. remove(yp);
  65. }
  66.  
  67. // xn collision
  68. if (
  69. // check if xn exists
  70. xn != null &&
  71. // check if the ball's left surface is left of or at xn's right surface
  72. ball.getX() <= xn.getX() + xn.getWidth() &&
  73. // check if the ball's y location is within xn's bounds
  74. ball.getY() + BALL_RADIUS * 2 >= xn.getY() && ball.getY() <= xn.getY() + xn.getHeight()
  75. ) {
  76. // The ball has hit the right side of the xn brick
  77. ball.setLocation(xn.getX() + xn.getWidth(), ball.getY());
  78. vx = Math.abs(vx);
  79. vy += getVelOff();
  80. remove(xn);
  81. }
  82.  
  83. // xp collision
  84. if (
  85. //check if xp exists
  86. xp != null &&
  87. // check if the ball's right surface is right of or at xp's left surface
  88. ball.getX() + BALL_RADIUS * 2 >= xp.getX() &&
  89. // check if the ball's y location is within xn's bounds
  90. ball.getY() + BALL_RADIUS * 2 >= xp.getY() && ball.getY() <= xp.getY() + xp.getHeight()
  91. ) {
  92. // The ball has hit the left side of the xp brick
  93. ball.setLocation(xn.getX() - BALL_RADIUS * 2, ball.getY());
  94. vx = -Math.abs(vx);
  95. vy += getVelOff();
  96. remove(xp);
  97. }
  98.  
  99. // center collisions
  100. if (center != null) {
  101. // top collision
  102. if (
  103. // check if the ball's bottom surface is below or at center's top surface
  104. ball.getY() <= center.getY() + center.getHeight() &&
  105. // check if the ball's x location is within center's bounds
  106. ball.getX() + BALL_RADIUS * 2 >= center.getX() && ball.getX() <= center.getX() + center.getWidth()
  107. ) {
  108. // The ball has hit the top of the center brick
  109. ball.setLocation(ball.getX(), center.getY() + center.getHeight());
  110. vx += getVelOff();
  111. vy = Math.abs(vy);
  112. remove(center);
  113. }
  114.  
  115. // bottom collision
  116. if (
  117. // check if the ball's top surface is above or at center's bottom surface
  118. ball.getY() + BALL_RADIUS * 2 >= center.getY() &&
  119. // check if the ball's x location is within center's bounds
  120. ball.getX() + BALL_RADIUS * 2 >= center.getX() && ball.getX() <= center.getX() + center.getWidth()
  121. ) {
  122. // The ball has hit the bottom of the center brick
  123. ball.setLocation(ball.getX(), center.getY() - BALL_RADIUS * 2);
  124. vx += getVelOff();
  125. vy = -Math.abs(vy);
  126. remove(center);
  127. }
  128.  
  129. // right collision
  130. if (
  131. // check if the ball's left surface is left of or at center's right surface
  132. ball.getX() <= center.getX() + center.getWidth() &&
  133. // check if the ball's y location is within center's bounds
  134. ball.getY() + BALL_RADIUS * 2 >= center.getY() && ball.getY() <= center.getY() + center.getHeight()
  135. ) {
  136. // The ball has hit the right side of the center brick
  137. ball.setLocation(center.getX() + center.getWidth(), ball.getY());
  138. vx = Math.abs(vx);
  139. vy += getVelOff();
  140. remove(center);
  141. }
  142.  
  143. // left collision
  144. if (
  145. // check if the ball's right surface is right of or at center's left surface
  146. ball.getX() + BALL_RADIUS * 2 >= center.getX() &&
  147. // check if the ball's y location is within center's bounds
  148. ball.getY() + BALL_RADIUS * 2 >= center.getY() && ball.getY() <= center.getY() + center.getHeight()
  149. ) {
  150. // The ball has hit the left side of the center brick
  151. ball.setLocation(center.getX() - BALL_RADIUS * 2, ball.getY());
  152. vx = -Math.abs(vx);
  153. vy += getVelOff();
  154. remove(center);
  155. }
  156. }
  157. }
  158.  
  159. // generates a random velocity offset
  160. public static double getVelOff() {
  161. return RandomGenerator.getInstance().nextDouble(-1.5, 1.5);
  162. }
Add Comment
Please, Sign In to add comment