Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. /*
  2. * File: Breakout.java
  3. * -------------------
  4. * Name: Angel Lopez
  5. * Section Leader: Steven Vellon
  6. *
  7. * This file will eventually implement the game of Breakout.
  8. */
  9.  
  10. import acm.graphics.*;
  11. import acm.program.*;
  12. import acm.util.*;
  13.  
  14. //import java.applet.*;
  15. import java.applet.AudioClip;
  16. import java.awt.*;
  17. import java.awt.event.*;
  18.  
  19. public class Breakout extends GraphicsProgram {
  20.  
  21. public static final int APPLICATION_WIDTH = 400;
  22. public static final int APPLICATION_HEIGHT = 600;
  23. private static final int WIDTH = APPLICATION_WIDTH;
  24. private static final int HEIGHT = APPLICATION_HEIGHT;
  25. private static final int PADDLE_WIDTH = 60;
  26. private static final int PADDLE_HEIGHT = 10;
  27. private static final int PADDLE_Y_OFFSET = 30;
  28. private static final int NBRICKS_PER_ROW = 10;
  29. private static final int NBRICK_ROWS = 10;
  30. private static final int BRICK_SEP = 4;
  31. private static final double BRICK_WIDTH = (double) (WIDTH - (NBRICKS_PER_ROW - 1)
  32. * BRICK_SEP)
  33. / NBRICKS_PER_ROW; // had to be double to accurately center bricks!
  34. private static final int BRICK_HEIGHT = 8;
  35. private static final int BALL_RADIUS = 10;
  36. private static final int DIAMETER = 2 * BALL_RADIUS;
  37. private static final int BRICK_Y_OFFSET = 70;
  38. private static final int NTURNS = 3;
  39.  
  40. /*
  41. * Runs breakout program
  42. */
  43. public void run() {
  44. turnNumber = 0;
  45. createGame();
  46. addMouseListeners();
  47. playGame();
  48. }
  49.  
  50. private void createGame() {
  51. beginningSequence();
  52. setUpBricks();
  53. createPaddle();
  54. createBall();
  55. bounceClip.play();
  56. }
  57.  
  58. /*
  59. * Places bricks in appropriate locations and gives them color
  60. */
  61. private void setUpBricks() {
  62. for (int brickRow = 0; brickRow < NBRICK_ROWS; brickRow++) {
  63. for (int brickColumn = 0; brickColumn < NBRICKS_PER_ROW; brickColumn++) {
  64. double x = (BRICK_SEP + BRICK_WIDTH) * brickColumn;
  65. double y = BRICK_Y_OFFSET + (BRICK_SEP + BRICK_HEIGHT)
  66. * brickRow;
  67. brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
  68. add(brick);
  69. brick.setFilled(true);
  70. brick.setColor(colorBrick(brickRow));
  71. brick.setFillColor(colorBrick(brickRow));
  72.  
  73. }
  74. }
  75. }
  76.  
  77. /*
  78. * Determines colors of bricks
  79. */
  80. private Color colorBrick(int brickRow) {
  81. switch (brickRow) {
  82. case 0:
  83. colorOfBrick = Color.RED;
  84. break;
  85. case 1:
  86. colorOfBrick = Color.RED;
  87. break;
  88. case 2:
  89. colorOfBrick = Color.ORANGE;
  90. break;
  91. case 3:
  92. colorOfBrick = Color.ORANGE;
  93. break;
  94. case 4:
  95. colorOfBrick = Color.YELLOW;
  96. break;
  97. case 5:
  98. colorOfBrick = Color.YELLOW;
  99. break;
  100. case 6:
  101. colorOfBrick = Color.GREEN;
  102. break;
  103. case 7:
  104. colorOfBrick = Color.GREEN;
  105. break;
  106. case 8:
  107. colorOfBrick = Color.CYAN;
  108. break;
  109. case 9:
  110. colorOfBrick = Color.CYAN;
  111. break;
  112. }
  113. return colorOfBrick;
  114. }
  115.  
  116. /*
  117. * creates a movable paddle positioned at its appropriate location
  118. */
  119. private void createPaddle() {
  120. double x = (WIDTH - PADDLE_WIDTH) / 2;
  121. double y = HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
  122. paddle = new GRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
  123. paddle.setFilled(true);
  124. add(paddle);
  125. }
  126.  
  127. /*
  128. * moves paddle by following mouse
  129. */
  130. public void mouseMoved(MouseEvent e) {
  131. double x = e.getX() - PADDLE_WIDTH / 2; // centers mouse on middle of
  132. // paddle
  133. double y = HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
  134. if (e.getX() < PADDLE_WIDTH / 2) {
  135. paddle.setLocation(0, y);
  136. } else if (e.getX() > WIDTH - PADDLE_WIDTH / 2) {
  137. paddle.setLocation(WIDTH - PADDLE_WIDTH, y);
  138. } else {
  139. paddle.setLocation(x, y);
  140. }
  141. }
  142.  
  143. /*
  144. * creates ball
  145. */
  146. private void createBall() {
  147. ball = new GOval(ballInitialX, ballInitialY, 2 * BALL_RADIUS,
  148. 2 * BALL_RADIUS);
  149. ball.setFilled(true);
  150. add(ball);
  151. }
  152.  
  153. /*
  154. * Begins the gameplay
  155. */
  156. private void playGame() {
  157. waitForClick();
  158. while (ball.getY() < HEIGHT - PADDLE_Y_OFFSET) {
  159. moveBall();
  160. }
  161. if (ball.getY() >= HEIGHT - PADDLE_Y_OFFSET) {
  162. resetGame();
  163. }
  164. }
  165.  
  166. /*
  167. * sets ball's velocity with respect to x
  168. */
  169. private void setVelocity() {
  170. vx = rgen.nextDouble(1.0, 3.0);
  171. if (rgen.nextBoolean(0.5))
  172. vx = -vx;
  173. }
  174.  
  175. /*
  176. * checks for collisions and adjusts velocities appropriately.
  177. */
  178. private void checkForCollisions() {
  179. checkForWalls();
  180. getCollidingObject();
  181. if (collidingObject == paddle) {
  182. setVelocity();
  183. vy = -vy;
  184. } else if (collidingObject != null) {
  185. remove(collidingObject);
  186. bounceClip.play();
  187. setVelocity();
  188. vy = -vy;
  189. brickTotal--;
  190. }
  191. }
  192.  
  193. /*
  194. * finds out what the colliding objects with the ball are
  195. */
  196. private void getCollidingObject() {
  197. double x = ball.getX();
  198. double y = ball.getY();
  199. if (getElementAt(x, y) != null) {
  200. collidingObject = getElementAt(x, y);
  201. } else if (getElementAt(x + DIAMETER, y) != null) {
  202. collidingObject = getElementAt(x + DIAMETER, y);
  203. } else if (getElementAt(x, y + DIAMETER) != null) {
  204. collidingObject = getElementAt(x, y + DIAMETER);
  205. } else if (getElementAt(x + DIAMETER, y + DIAMETER) != null) {
  206. collidingObject = getElementAt(x + DIAMETER, y + DIAMETER);
  207. } else {
  208. collidingObject = null;
  209. }
  210.  
  211. }
  212.  
  213. /*
  214. * Bounces ball if it hits application edges (other than bottom edge)
  215. */
  216. private void checkForWalls() {
  217. if (ball.getX() <= 0 || ball.getX() + 2 * BALL_RADIUS >= WIDTH) {
  218. vx = -vx;
  219. }
  220. if (ball.getY() <= 0) {
  221. vy = -vy;
  222. }
  223. if (ball.getY() > HEIGHT - PADDLE_Y_OFFSET) {
  224. vy = -vy;
  225. }
  226. }
  227.  
  228. /*
  229. * Moves ball
  230. */
  231. private void moveBall() {
  232. ball.move(vx, vy);
  233. checkForCollisions();
  234. pause(DELAY);
  235. if (brickTotal == 0) {
  236. winGame();
  237. }
  238. }
  239.  
  240. /*
  241. * Resets game after ball falls past application width
  242. */
  243. private void resetGame() {
  244. remove(ball);
  245. turnNumber++;
  246. if (turnNumber < NTURNS) {
  247. add(ball, ballInitialX, ballInitialY);
  248. vy = -vy;
  249. vx = 0;
  250. playGame();
  251. } else if (brickTotal > 0) {
  252. loseGame();
  253. }
  254. }
  255.  
  256. /*
  257. * Ends game (Winning situation)
  258. */
  259. private void winGame() {
  260. removeAll();
  261. GLabel youWin = new GLabel("YOU WIN!");
  262. youWin.setFont("Serif-BOLD-48");
  263. youWin.setColor(Color.GREEN);
  264. double winWidth = youWin.getWidth();
  265. double winHeight = youWin.getAscent();
  266. add(youWin, (WIDTH - winWidth) / 2, (HEIGHT - winHeight) / 2);
  267. pause(10000);
  268. }
  269.  
  270. /*
  271. * Ends game (losing situation), says score
  272. */
  273. private void loseGame() {
  274. removeAll();
  275. int score = NBRICK_ROWS * NBRICKS_PER_ROW - brickTotal;
  276. GLabel youLose = new GLabel("YOU LOSE!");
  277. youLose.setFont("SERIF-BOLD-48");
  278. youLose.setColor(Color.RED);
  279. double loseWidth = youLose.getWidth();
  280. double loseHeight = youLose.getAscent();
  281. add(youLose, (WIDTH - loseWidth) / 2, (HEIGHT - loseHeight) / 2);
  282. GLabel losingScore = new GLabel("Final Score: " + score + " / 100");
  283. losingScore.setFont("Serif-24");
  284. double scoreWidth = losingScore.getWidth();
  285. double scoreHeight = losingScore.getAscent();
  286. add(losingScore, (WIDTH - scoreWidth) / 2, (HEIGHT - scoreHeight) / 2
  287. + youLose.getHeight());
  288. }
  289.  
  290. /*
  291. * Credits sequence before game begins
  292. */
  293. private void beginningSequence() {
  294. enterBreakoutAnimation();
  295. showCreator();
  296. showBeginningInstruction();
  297. }
  298.  
  299. /*
  300. * displays breakout beginning animation
  301. */
  302. private void enterBreakoutAnimation() {
  303. breakout.setFont("Serif-BOLD-48");
  304. double x = (WIDTH - breakout.getWidth()) / 2;
  305. double y = (HEIGHT - breakout.getAscent()) / 2;
  306. add(breakout, x, y);
  307. changeBeginningSequenceColor();
  308. removeAll();
  309. pause(1000);
  310. }
  311.  
  312. /*
  313. * Used for the animation of breakout logo
  314. */
  315. private void changeBeginningSequenceColor() {
  316. for (int i = 0; i < 10; i++) {
  317. breakout.setColor(Color.RED);
  318. pause(20);
  319. breakout.setColor(Color.ORANGE);
  320. pause(20);
  321. breakout.setColor(Color.YELLOW);
  322. pause(20);
  323. breakout.setColor(Color.GREEN);
  324. pause(20);
  325. breakout.setColor(Color.CYAN);
  326. pause(20);
  327. }
  328. breakout.setColor(Color.LIGHT_GRAY);
  329. pause(20);
  330. breakout.setColor(Color.WHITE);
  331. pause(20);
  332. }
  333.  
  334. /*
  335. * Displays name of creator of application (me)
  336. */
  337. private void showCreator() {
  338. GLabel createdBy = new GLabel("Created by");
  339. createdBy.setFont("Serif-24");
  340. double createdByX = (WIDTH - createdBy.getWidth()) / 2;
  341. double createdByY = (HEIGHT - createdBy.getAscent() - 3 * createdBy
  342. .getHeight()) / 2;
  343. GLabel angelLopez = new GLabel("Angel Lopez");
  344. angelLopez.setFont("Serif-48");
  345. double angelLopezX = (WIDTH - angelLopez.getWidth()) / 2;
  346. double angelLopezY = (HEIGHT - createdBy.getAscent()) / 2;
  347. add(createdBy, createdByX, createdByY);
  348. bounceClip.play();
  349. pause(500);
  350. add(angelLopez, angelLopezX, angelLopezY);
  351. bounceClip.play();
  352. pause(3000);
  353. createdBy.setColor(Color.DARK_GRAY);
  354. angelLopez.setColor(Color.DARK_GRAY);
  355. pause(75);
  356. createdBy.setColor(Color.LIGHT_GRAY);
  357. angelLopez.setColor(Color.LIGHT_GRAY);
  358. pause(75);
  359. removeAll();
  360. pause(1000);
  361.  
  362. }
  363.  
  364. private void showBeginningInstruction() {
  365. GLabel beginningInstruction = new GLabel("Click to begin!");
  366. beginningInstruction.setFont("Serif-24");
  367. double x = (WIDTH - beginningInstruction.getWidth()) / 2;
  368. double y = (HEIGHT - beginningInstruction.getAscent() + beginningInstruction
  369. .getHeight()) / 2;
  370. add(beginningInstruction, x, y);
  371. bounceClip.play();
  372. pause(2000);
  373. removeAll();
  374. }
  375.  
  376. private double vx;
  377. private double vy = 3;
  378. private double ballInitialX = (WIDTH - 2 * BALL_RADIUS) / 2;
  379. private double ballInitialY = (HEIGHT - 2 * BALL_RADIUS) / 2;
  380. private int turnNumber;
  381. private int brickTotal = NBRICKS_PER_ROW * NBRICK_ROWS;
  382. private int DELAY = 10; // smaller delay = faster game speed
  383. private GLabel breakout = new GLabel("BREAKOUT");
  384. private GOval ball;
  385. private GObject collidingObject;
  386. private GRect brick;
  387. private GRect paddle;
  388. private Color colorOfBrick;
  389. private RandomGenerator rgen = RandomGenerator.getInstance();
  390. private AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement