Advertisement
kevinhuang890

Untitled

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