Advertisement
kevinhuang890

Untitled

Dec 12th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1.  
  2. public final int BALL_RADIUS = 8;
  3. public final int BALL_DIAMETER = BALL_RADIUS * 2;
  4. public final int PADDLE_WIDTH = 100;
  5. public final int PADDLE_HEIGHT = 10;
  6. public final int BRICK_WIDTH = 44;
  7. public final int BRICK_HEIGHT = 20;
  8. int lives = 1;
  9. int score = 0;
  10.  
  11. private Ball ball;
  12.  
  13. public Brick[] redBricks = new Brick[10];
  14. public Brick[] blueBricks = new Brick[10];
  15. public Brick[] greenBricks = new Brick[10];
  16. public Brick[] yellowBricks = new Brick[10];
  17. public Brick[] orangeBricks = new Brick[10];
  18. // TODO: put the rest of the rows here
  19.  
  20. public boolean hasGameStarted = false;
  21. public boolean isGameOver = false;
  22. public int paddleX;
  23. public int paddleY;
  24.  
  25. public void setup()
  26. {
  27. size(480, 480);
  28. pixelDensity(2);
  29.  
  30.  
  31.  
  32. // Initial ball position
  33.  
  34. ball = new Ball(width / 2, height - PADDLE_HEIGHT - BALL_RADIUS);
  35. paddleX = width / 2 - PADDLE_WIDTH / 2;
  36. paddleY = height - PADDLE_HEIGHT;
  37.  
  38. // TODO: create the red bricks
  39.  
  40.  
  41. for(int i = 0; i < redBricks.length; i++){
  42. Brick b = new Brick(48*i,0,color(255,0,0));
  43. redBricks[i] = b;
  44. }
  45. // TODO: create the oranges bricks
  46. for(int i = 0; i < orangeBricks.length; i++){
  47. Brick b = new Brick(48*i,25,color(255,165,0));
  48. orangeBricks[i] = b;
  49. }
  50. // TODO: create the yellow bricks
  51. for(int i = 0; i < yellowBricks.length; i++){
  52. Brick b = new Brick(48*i,50,color(255,255,0));
  53. yellowBricks[i] = b;
  54. }
  55. // TODO: create the green bricks
  56. for(int i = 0; i < greenBricks.length; i++){
  57. Brick b = new Brick(48*i,75,color(0,128,0));
  58. greenBricks[i] = b;
  59. }
  60. // TODO: create the blue bricks
  61. for(int i = 0; i < blueBricks.length; i++){
  62. Brick b = new Brick(48*i,100,color(0,255,255));
  63. blueBricks[i] = b;
  64. }
  65. }
  66.  
  67. public void draw()
  68. {
  69.  
  70. if (!isGameOver)
  71. {
  72. background(75);
  73. fill(133,193,233);
  74. textSize(20);
  75. text("Score: "+ score, 350,400);
  76. gameOver();
  77. text("Lives: "+ lives,350,450);
  78.  
  79. // Draw the ball.
  80. ball.drawBall();
  81. didHitSide();
  82. didHitTop();
  83. // Draw the paddle.
  84. fill(165, 42, 42);
  85. rect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
  86.  
  87. // TODO: check for wall collisions
  88.  
  89.  
  90. // TODO: draw the bricks
  91. for(int x = 0; x < redBricks.length; x++){
  92. if (redBricks[x] != null){
  93. Brick myBrick = redBricks[x]; myBrick.drawBrick();
  94. }
  95. }
  96. for(int x = 0; x < blueBricks.length; x++){
  97. if (blueBricks[x] != null){
  98. Brick myBrick = blueBricks[x]; myBrick.drawBrick();
  99. }
  100. }
  101. for(int x = 0; x < orangeBricks.length; x++){
  102. if (orangeBricks[x] != null){
  103. Brick myBrick = orangeBricks[x]; myBrick.drawBrick();
  104. }
  105. }
  106. for(int x = 0; x < yellowBricks.length; x++){
  107. if (yellowBricks[x] != null){
  108. Brick myBrick = yellowBricks[x]; myBrick.drawBrick();
  109. }
  110. }
  111. for(int x = 0; x < greenBricks.length; x++){
  112. if (greenBricks[x] != null){
  113. Brick myBrick = greenBricks[x]; myBrick.drawBrick();
  114. }
  115. }
  116. // TODO: check for brick collisions
  117. if(checkForCollisions()){
  118.  
  119.  
  120. }
  121.  
  122. // TODO: check for wall collision
  123. if(didHitSide()){
  124. ball.reverseSpeedX();
  125. }
  126. if(didHitTop()){
  127. ball.reverseSpeedY();
  128. }
  129. }
  130.  
  131. // TODO: check for paddle collision
  132. if(isBallCollidingWithPaddle()){
  133.  
  134. ball.reverseSpeedY();
  135. }
  136. if(isOutOfBounds()){
  137. lives = lives-1;
  138. restartGame();
  139.  
  140.  
  141.  
  142.  
  143. }
  144.  
  145.  
  146.  
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. public void mouseMoved(MouseEvent event)
  160. {
  161. int x = event.getX();
  162. paddleX = x - PADDLE_WIDTH / 2;
  163.  
  164. if (!hasGameStarted)
  165. {
  166. ball.setX(x);
  167. }
  168. }
  169.  
  170. public void mouseReleased(MouseEvent event)
  171. {
  172. if (!hasGameStarted)
  173. {
  174. ball.setSpeedX(8);
  175. ball.setSpeedY(-5);
  176. hasGameStarted = true;
  177.  
  178. }
  179. }
  180.  
  181. /**
  182. * Return true if the ball has hit the left or right sides.
  183. */
  184. public boolean didHitSide()
  185. {
  186.  
  187. if(ball.getX()>480||ball.getX()<0){
  188. return true;
  189. }
  190. return false;
  191.  
  192.  
  193. }
  194.  
  195. /**
  196. * Return true if the ball has hit the top side.
  197. */
  198. public boolean didHitTop()
  199. {
  200.  
  201. if(ball.getY()<0){
  202. return true;
  203. }
  204. return false;
  205. }
  206.  
  207. public boolean didHitPaddle(){
  208. if( ball.getX() >PADDLE_WIDTH+50){
  209. return true;
  210. }
  211. return false;
  212. }
  213.  
  214. /**
  215. * Return true with the ball passed through the bottom.
  216. */
  217. public boolean isOutOfBounds()
  218. {
  219. // TODO
  220. if(ball.getY()>480){
  221. ball.setX((width / 2));
  222. ball.setY(height - PADDLE_HEIGHT - BALL_RADIUS);
  223. ball.setSpeedX(0);
  224. ball.setSpeedY(0);
  225. paddleX = width / 2 - PADDLE_WIDTH / 2;
  226. paddleY = height - PADDLE_HEIGHT;
  227. return true;
  228. }
  229. return false;
  230. }
  231.  
  232. /**
  233. * Return true when the ball is colliding with the paddle.
  234. */
  235.  
  236. public boolean isBallCollidingWithPaddle()
  237. {
  238. return isBallCollidingWithRect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
  239. }
  240.  
  241. /**
  242. * Detects whether the ball is colliding with a brick.
  243. * Use a loop to check every brick for collisions.
  244. * If a brick has been hit, remove it and return true.
  245. * If not bricks are being hit, return false.
  246. */
  247. public boolean checkForCollisions()
  248. {
  249. for(int i = 0; i < redBricks.length; i++){
  250. if(redBricks[i] != null){
  251. if(redBricks[i].isCollidingWithBall()){
  252. ball.reverseSpeedY();
  253. ball.reverseSpeedX();
  254. redBricks[i] = null;
  255. score = score + 1;
  256. }
  257. }
  258. }
  259. for(int i = 0; i < orangeBricks.length; i++){
  260. if(orangeBricks[i] != null){
  261. if(orangeBricks[i].isCollidingWithBall()){
  262. ball.reverseSpeedX();
  263. ball.reverseSpeedY();
  264. orangeBricks[i] = null;
  265. score = score + 1;
  266. }
  267. }
  268. }
  269. for(int i = 0; i < yellowBricks.length; i++){
  270. if(yellowBricks[i] != null){
  271. if(yellowBricks[i].isCollidingWithBall()){
  272. ball.reverseSpeedX();
  273. ball.reverseSpeedY();
  274. yellowBricks[i] = null;
  275. score = score + 1;
  276. }
  277. }
  278. }
  279. for(int i = 0; i < greenBricks.length; i++){
  280. if(greenBricks[i] != null){
  281. if(greenBricks[i].isCollidingWithBall()){
  282. ball.reverseSpeedX();
  283. ball.reverseSpeedY();
  284. greenBricks[i] = null;
  285. score = score + 1;
  286. }
  287. }
  288. }
  289. for(int i = 0; i < blueBricks.length; i++){
  290. if(blueBricks[i] != null){
  291. if(blueBricks[i].isCollidingWithBall()){
  292. ball.reverseSpeedY();
  293. ball.reverseSpeedX();
  294. blueBricks[i] = null;
  295. score = score + 1;
  296. return true;
  297. }
  298. }
  299. }
  300.  
  301. return false;
  302. }
  303.  
  304. /**
  305. * Loops over every brick. If an unbroken brick is found, true false.
  306. * If every brick has been broken, return true.
  307. */
  308. public boolean areAllBricksBroken()
  309. {
  310.  
  311.  
  312. for(int i = 0; i < blueBricks.length; i++){
  313. if(blueBricks[i] != null){
  314.  
  315.  
  316. }
  317. }
  318. for(int i = 0; i < redBricks.length; i++){
  319. if(redBricks[i] != null){
  320.  
  321.  
  322. }
  323. }
  324. for(int i = 0; i < orangeBricks.length; i++){
  325. if(orangeBricks[i] != null){
  326.  
  327.  
  328. }
  329. }
  330. for(int i = 0; i < greenBricks.length; i++){
  331. if(greenBricks[i] != null){
  332.  
  333.  
  334. }
  335. }
  336. for(int i = 0; i < yellowBricks.length; i++){
  337. if(yellowBricks[i] != null){
  338.  
  339. return false;
  340.  
  341. }
  342. }
  343.  
  344.  
  345. return true;
  346. }
  347.  
  348.  
  349. /**
  350. * Ends the game.
  351. * If param `didWin` is true, congratulate the use, else boo them.
  352. * Prompt the user to click to restart the game.
  353. * Ensure that the text is centered on the screen.
  354. * Remember to set `isGameOver` to true.
  355. */
  356. public void gameOver()
  357. {
  358.  
  359.  
  360. if(score == 50){
  361. background(244,208,63);
  362. fill(133,193,233);
  363. textSize(30);
  364. text("YOU WON!",170,240);
  365. textSize(20);
  366. text("Press anywhere to restart the game",50,290);
  367. ball.setX((width / 2));
  368. ball.setY(height - PADDLE_HEIGHT - BALL_RADIUS);
  369. paddleX = width / 2 - PADDLE_WIDTH / 2;
  370. paddleY = height - PADDLE_HEIGHT;
  371. ball.setSpeedX(0);
  372. ball.setSpeedY(0);
  373. if(mousePressed==true){
  374. restartGame();
  375. }
  376.  
  377. }
  378. }
  379.  
  380.  
  381.  
  382.  
  383. /**
  384. * Restarts the game by reseting all of the instance variables.
  385. */
  386. public void restartGame()
  387. {
  388. // TODO
  389. size(480, 480);
  390. score=0;
  391.  
  392.  
  393. // Initial ball position
  394.  
  395. paddleX = width / 2 - PADDLE_WIDTH / 2;
  396. paddleY = height - PADDLE_HEIGHT;
  397. ball.setSpeedX(0);
  398. ball.setSpeedY(0);
  399.  
  400. hasGameStarted = false;
  401. ball.setX((width / 2));
  402. ball.setY(height - PADDLE_HEIGHT - BALL_RADIUS);
  403. paddleX = width / 2 - PADDLE_WIDTH / 2;
  404. paddleY = height - PADDLE_HEIGHT;
  405. for(int i = 0; i < redBricks.length; i++){
  406. Brick b = new Brick(48*i,0,color(255,0,0));
  407. redBricks[i] = b;
  408. }
  409. // TODO: create the oranges bricks
  410. for(int i = 0; i < orangeBricks.length; i++){
  411. Brick b = new Brick(48*i,25,color(255,165,0));
  412. orangeBricks[i] = b;
  413. }
  414. // TODO: create the yellow bricks
  415. for(int i = 0; i < yellowBricks.length; i++){
  416. Brick b = new Brick(48*i,50,color(255,255,0));
  417. yellowBricks[i] = b;
  418. }
  419. // TODO: create the green bricks
  420. for(int i = 0; i < greenBricks.length; i++){
  421. Brick b = new Brick(48*i,75,color(0,128,0));
  422. greenBricks[i] = b;
  423. }
  424. // TODO: create the blue bricks
  425. for(int i = 0; i < blueBricks.length; i++){
  426. Brick b = new Brick(48*i,100,color(0,255,255));
  427. blueBricks[i] = b;
  428. }
  429.  
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement