Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. /**
  2. * The Breakout Ball
  3. *
  4. * @author CMS_167
  5. */
  6.  
  7. public class Ball {
  8.  
  9. //*** Instance variables ***//
  10. private double x, y;
  11. private double radius;
  12. private double dx, dy;
  13.  
  14.  
  15. /**
  16. * Constructor -- intiailize a new Ball
  17. */
  18. public Ball() {
  19. this.x = .5;
  20. this.y = .10;
  21. this.radius = .01;
  22. this.dx = .008;
  23. this.dy = .008;
  24. }
  25.  
  26.  
  27. /**
  28. * Update -- add code to move the ball and reflect off of the sides
  29. *
  30. * Look at the BouncingBall and Pong examples
  31. */
  32. public void update() {
  33. if (this.x + this.radius > 1.0 || this.x + this.radius < 0.0) {
  34. this.dx = -this.dx;
  35. }
  36. if (this.y + this.radius > 1.0 || this.y + this.radius < 0.0) {
  37. this.dy = -this.dy;
  38. }
  39.  
  40. this.x += this.dx;
  41. this.y += this.dy;
  42. }
  43.  
  44.  
  45. /**
  46. * Check if this Ball collides with the Paddle
  47. *
  48. * If so, reverse the Ball's y velocity
  49. *
  50. * Look at the examples from Secret Collect and Pong
  51. *
  52. * @return nothing, this method only updates the Ball's dy data member
  53. */
  54. public void checkPaddleCollision(Paddle paddle) {
  55. double right = this.x + this.radius;
  56. double left = this.x - this.radius;
  57. double top = this.y + this.radius;
  58. double bottom = this.y - this.radius;
  59.  
  60. if (paddle.top() < bottom) {
  61. return;
  62. }
  63. if (paddle.bottom() > top) {
  64. return;
  65. }
  66. if (paddle.left() > right) {
  67. return;
  68. }
  69. if (paddle.right() < left) {
  70. return;
  71. }
  72. this.dy = -this.dy;
  73. }
  74.  
  75.  
  76. /**
  77. * Check if this Ball collides with the given Brick
  78. *
  79. * Similar to testing for Paddle collisions, but this method
  80. * returns a boolean indicating if the collision happened
  81. *
  82. * @return false if no collision, true otherwise
  83. */
  84. public boolean checkBrickCollision(Brick brick) {
  85. double right = this.x + this.radius;
  86. double left = this.x - this.radius;
  87. double top = this.y + this.radius;
  88. double bottom = this.y - this.radius;
  89.  
  90. if (brick.top() < bottom) {
  91. return false;
  92. }
  93. if (brick.bottom() > top) {
  94. return false;
  95. }
  96. if (brick.left() > right) {
  97. return false;
  98. }
  99. if (brick.right() < left) {
  100. return false;
  101. }
  102. // Starter code returns false so the program will run
  103. if (brick.right() < right || brick.left() > left) {
  104. this.dx = -this.dx;
  105. return false;
  106. }
  107. this.dy = -this.dy;
  108. return true;
  109. }
  110.  
  111.  
  112. /**
  113. * Draw
  114. */
  115. public void draw() {
  116. StdDraw.filledCircle(this.x, this.y, this.radius);
  117. }
  118.  
  119. }
  120.  
  121.  
  122.  
  123. /**
  124. * A Breakout Brick
  125. *
  126. * @author CMS_167
  127. */
  128.  
  129. public class Brick {
  130.  
  131. //*** Instance variables ***//
  132. private double x, y;
  133. private double halfWidth, halfHeight;
  134. private int red, green, blue;
  135.  
  136. /**
  137. * Constructor -- takes a position in the grid of bricks and automatically
  138. * calculates the correct x and y position for the new Brick's center
  139. *
  140. * DON'T MODIFY THIS METHOD.
  141. */
  142. public Brick(int row, int col) {
  143. // Calculate the starting position based on the row and column
  144. this.halfHeight = .30 / Breakout.NUM_ROWS / 2;
  145. this.halfWidth = 1.0 / Breakout.NUM_COLS / 2;
  146.  
  147. this.x = col * (2 * halfWidth) + halfWidth;
  148. this.y = .90 - row * (2 * halfHeight) - halfHeight;
  149.  
  150. // Random color
  151. this.red = (int) (Math.random() * 256);
  152. this.green = (int) (Math.random() * 256);
  153. this.blue = (int) (Math.random() * 256);
  154.  
  155. }
  156.  
  157.  
  158. /**
  159. * Add new methods to calculate and return the right, left, top, and bottom
  160. * of this Brick
  161. */
  162. public double right() {
  163. return this.x + this.halfWidth;
  164. }
  165.  
  166. public double left() {
  167. return this.x - this.halfWidth;
  168. }
  169.  
  170. public double top() {
  171. return this.y + this.halfHeight;
  172. }
  173.  
  174. public double bottom() {
  175. return this.y - this.halfHeight;
  176. }
  177.  
  178. /**
  179. * Draw
  180. */
  181. public void draw() {
  182. StdDraw.setPenColor(this.red, this.green, this.blue);
  183. StdDraw.filledRectangle(this.x, this.y, this.halfWidth, this.halfHeight);
  184. StdDraw.setPenColor(0, 0, 0); // Set color back to black
  185. }
  186.  
  187. }
  188.  
  189.  
  190. /**
  191. * The Breakout Paddle
  192. *
  193. * @author CMS_167
  194. */
  195.  
  196. import java.awt.event.KeyEvent;
  197.  
  198. public class Paddle {
  199.  
  200. //*** Instance variables ***//
  201. private double x, y;
  202. private double halfWidth, halfHeight;
  203. private final double STEP = .02; // Distance to move on each key press
  204.  
  205. /**
  206. * Construct a new Paddle
  207. */
  208. public Paddle() {
  209. this.x = .5;
  210. this.y = .01;
  211. this.halfWidth = .10;
  212. this.halfHeight = .01;
  213. }
  214.  
  215.  
  216. /**
  217. * Update -- move the Paddle
  218. *
  219. * Check for left and right arrow presses StdDraw.isKeyPressed
  220. */
  221. public void update() {
  222.  
  223. if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT)){
  224. this.x -= STEP;
  225. }
  226.  
  227. if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT)) {
  228. this.x += STEP;
  229. }
  230.  
  231. }
  232.  
  233.  
  234. /**
  235. * Add methods to calculate and return the right, left, top, and bottom sides
  236. * of this Paddle
  237. */
  238. public double right() {
  239. return this.x + this.halfWidth;
  240. }
  241.  
  242. public double left() {
  243. return this.x - this.halfWidth;
  244. }
  245.  
  246. public double top() {
  247. return this.y + this.halfHeight;
  248. }
  249.  
  250. public double bottom() {
  251. return this.y - this.halfHeight;
  252. }
  253.  
  254. /**
  255. * Draw
  256. */
  257. public void draw() {
  258. StdDraw.filledRectangle(this.x, this.y, this.halfWidth, this.halfHeight);
  259. }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement