Advertisement
Guest User

Untitled

a guest
May 24th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. /*
  2. Break Out Assignment
  3. This was an assignment from my java class in college. Still looking for the other assignments and will get them up here ASAP
  4. */
  5.  
  6. import java.awt.BorderLayout;
  7. import java.awt.Color;
  8. import java.awt.Font;
  9. import java.awt.Graphics;
  10. import java.awt.Rectangle;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.KeyEvent;
  14. import java.awt.event.KeyListener;
  15.  
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JPanel;
  19.  
  20. public class BreakOut extends JPanel implements KeyListener,
  21. ActionListener, Runnable {
  22. // movement keys
  23. static boolean right = false;
  24. static boolean left = false;
  25. //ball variables
  26. int ballx = 160;
  27. int bally = 218;
  28. //paddle variables
  29. int paddlex = 160;
  30. int paddley = 245;
  31. //block variables
  32. int blockx = 70;
  33. int blocky = 50;
  34.  
  35. //initialize objects
  36. Rectangle[] Block = new Rectangle[18];
  37. Rectangle Ball = new Rectangle(ballx, bally, 5, 5);
  38. Rectangle Paddle = new Rectangle(paddlex, paddley, 40, 5);
  39. Thread t;
  40. BreakeOut() {
  41. addKeyListener(this);
  42. setFocusable(true);
  43. t = new Thread(this);
  44. t.start();
  45. }
  46.  
  47. public static void main(String[] args) {
  48. JFrame frame = new JFrame();
  49. BreakeOut game = new BreakeOut();
  50. JButton button = new JButton("restart");
  51. frame.setSize(400, 500);
  52. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53.  
  54. frame.add(game);
  55. frame.add(button, BorderLayout.SOUTH);
  56. frame.setLocationRelativeTo(null);
  57. frame.setResizable(false);
  58. frame.setVisible(true);
  59. button.addActionListener(game);
  60.  
  61. }
  62.  
  63. //configuring objects
  64. public void paint(Graphics g) {
  65. g.setColor(Color.LIGHT_GRAY);
  66. g.fillRect(0, 0, 400, 500);
  67. g.setColor(Color.cyan);
  68. g.fill3DRect(Paddle.x, Paddle.y, Paddle.width, Paddle.height, true);
  69. g.setColor(Color.magenta);
  70. g.fillOval(Ball.x, Ball.y, Ball.width, Ball.height);
  71. g.setColor(Color.gray);
  72. g.fillRect(0, 251, 500, 200);
  73. g.setColor(Color.orange);
  74. g.drawRect(0, 0, 343, 250);
  75. for (int i = 0; i < Block.length; i++) {
  76. if (Block[i] != null) {
  77. g.fill3DRect(Block[i].x, Block[i].y, Block[i].width,
  78. Block[i].height, true);
  79. }
  80. }
  81.  
  82. if (ballDrop == true || noBlocks == true) {
  83. g.drawString(status, 70, 120);
  84. ballDrop = false;
  85. noBlocks = false;
  86. }
  87.  
  88. }
  89.  
  90. //game setup
  91. boolean ballDrop = false;
  92. boolean noBlocks = false;
  93. int vx = -1;
  94. int vy = -1;
  95. int count = 0;
  96. String status;
  97. //game loop
  98. public void run() {
  99.  
  100. //creating blocks
  101. for (int i = 0; i < Block.length; i++) {
  102. Block[i] = new Rectangle(blockx, blocky, 30, 10);
  103. if (i == 5) {
  104. blockx = 30;
  105. blocky = 62;
  106. }
  107. if (i == 9) {
  108. blockx = 30;
  109. blocky = 74;
  110. }
  111. blockx += 31;
  112. }
  113.  
  114. while (ballDrop == false && noBlocks == false) {
  115.  
  116. //ball movement
  117. repaint();
  118. Ball.x += vx;
  119. Ball.y += vy;
  120.  
  121. //paddle interactivity
  122. if (left == true) {
  123.  
  124. Paddle.x -= 5;
  125. right = false;
  126. }
  127. if (right == true) {
  128. Paddle.x += 5;
  129. left = false;
  130. }
  131. if (Paddle.x <= 4) {
  132. Paddle.x = 4;
  133. } else if (Paddle.x >= 298) {
  134. Paddle.x = 298;
  135. }
  136.  
  137. //paddle collisions
  138. if (Ball.intersects(Paddle)) {
  139. vy = -vy;
  140. }
  141.  
  142. //block collisions
  143. for (int i = 0; i < Block.length; i++) {
  144. if (Block[i] != null) {
  145. if (Block[i].intersects(Ball)) {
  146. Block[i] = null;
  147. vy = -vy;
  148. count++;
  149. }
  150. }
  151. }
  152.  
  153. //ending game
  154.  
  155. if (count == Block.length) {
  156. noBlocks = true;
  157. status = "YOU WON!!";
  158. repaint();
  159. }
  160.  
  161. //boundary collisions
  162. if (Ball.x <= 0 || Ball.x + Ball.height >= 343) {
  163. vx = -vx;
  164. }
  165. if (Ball.y <= 0) {
  166. vy = -vy;
  167. }
  168. if (Ball.y >= 250) {
  169. ballDrop = true;
  170. status = "YOU LOST THE GAME";
  171. repaint();
  172. }
  173. try {
  174. Thread.sleep(8);
  175. } catch (Exception ex) {
  176. }
  177.  
  178. }
  179.  
  180. }
  181.  
  182. //handling keys
  183. @Override
  184. public void keyPressed(KeyEvent e) {
  185. int keyCode = e.getKeyCode();
  186. if (keyCode == KeyEvent.VK_LEFT) {
  187. left = true;
  188. }
  189.  
  190. if (keyCode == KeyEvent.VK_RIGHT) {
  191. right = true;
  192. }
  193. }
  194.  
  195. @Override
  196. public void keyReleased(KeyEvent e) {
  197. int keyCode = e.getKeyCode();
  198. if (keyCode == KeyEvent.VK_LEFT) {
  199. left = false;
  200. }
  201.  
  202. if (keyCode == KeyEvent.VK_RIGHT) {
  203. right = false;
  204. }
  205. }
  206.  
  207. @Override
  208. public void keyTyped(KeyEvent arg0) {
  209.  
  210. }
  211.  
  212. @Override
  213. public void actionPerformed(ActionEvent e) {
  214. String str = e.getActionCommand();
  215. if (str.equals("restart")) {
  216. System.out.print("hi");
  217. this.restart();
  218.  
  219. }
  220. }
  221.  
  222. public void restart() {
  223.  
  224. requestFocus(true);
  225.  
  226. ballx = 160;
  227. bally = 218;
  228.  
  229. paddlex = 160;
  230. paddley = 245;
  231.  
  232. blockx = 70;
  233. blocky = 50;
  234.  
  235. Ball = new Rectangle(ballx, bally, 5, 5);
  236. Paddle = new Rectangle(paddlex, paddley, 40, 5);
  237. Block = new Rectangle[18];
  238.  
  239. vx = -1;
  240. vy = -1;
  241. ballDrop = false;
  242. noBricks = false;
  243. count = 0;
  244. status = null;
  245.  
  246. for (int i = 0; i < Block.length; i++) {
  247. Block[i] = new Rectangle(blockx, blocky, 30, 10);
  248. if (i == 5) {
  249. blockx = 30;
  250. blocky = 62;
  251. }
  252. if (i == 11) {
  253. blockx = 30;
  254. blocky = 74;
  255. }
  256. blockx += 31;
  257. }
  258. repaint();
  259. }
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement