Advertisement
neonfire999

Game

Mar 12th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. import javax.swing.JComponent;
  2. import java.awt.geom.Rectangle2D;
  3. import java.awt.Rectangle;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.awt.event.KeyListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import javax.swing.Timer;
  12. import javax.swing.JFrame;
  13. import javax.swing.JOptionPane;
  14.  
  15. /*
  16. * Game Component class
  17. * used to display the game inside the window frame
  18. */
  19. public class Game extends JComponent
  20. {
  21. // Private instance fields go here
  22. public static Rectangle2D.Double brick[][]; // 2D array of bricks
  23. public static Paddle paddle; // game paddle
  24. private Ball ball; // the ball
  25.  
  26. private int score; // Score: increments with each brick removed
  27.  
  28. // Arrow key state variables
  29. public static boolean right_key; // State of right arrow key
  30. public static boolean left_key; // State of left arrow key
  31.  
  32. // Constants
  33. public static final int ROWS = 5; // #rows of bricks
  34. public static int COLS = 10; // #columns of bricks
  35. public static final double BRICK_WIDTH = 20.0; // Width of a brick
  36. public static final double BRICK_HEIGHT = 10.0; // Height of a brick
  37. private final double SPACING = 5.0; // Space between bricks
  38. private final int DELAY = 10;
  39.  
  40. /*
  41. * Constructor
  42. */
  43. public Game()
  44. {
  45. super(); // Call superclass constructor
  46.  
  47. // INSERT CODE HERE
  48.  
  49. brick = new Rectangle2D.Double[ROWS][COLS];
  50. double defaultBorder = 27.5;
  51. for(int i=0; i < ROWS; i++)
  52. {
  53. for(int j = 0; j < COLS; j++)
  54. {
  55. brick[i][j] = new Rectangle2D.Double(defaultBorder+(j*SPACING)+(j*BRICK_WIDTH),((i+1)*SPACING)+((i)*BRICK_HEIGHT),BRICK_WIDTH ,BRICK_HEIGHT);
  56.  
  57. }
  58. }
  59.  
  60. // make paddle here
  61. paddle = new Paddle();
  62.  
  63. // ball
  64. ball = new Ball();
  65.  
  66. class TimerListener implements ActionListener
  67. {
  68. public void actionPerformed(ActionEvent event)
  69. {
  70. update();
  71. }
  72. }
  73.  
  74. ActionListener timeListener = new TimerListener();
  75. Timer timer = new Timer(DELAY, timeListener);
  76. timer.start();
  77.  
  78. // Inner class to monitor keyboard movements
  79. class ArrowKeyListener implements KeyListener
  80. {
  81. /* This method is required by the KeyListener interface
  82. * but is not required for this game
  83. */
  84. public void keyTyped(KeyEvent e)
  85. { }
  86.  
  87. /* Handle the key release event
  88. * Check left or right arrow keys
  89. */
  90. public void keyReleased(KeyEvent e)
  91. {
  92. int keyCode = e.getKeyCode();
  93.  
  94. if (keyCode == KeyEvent.VK_RIGHT)
  95. right_key = false;
  96. else if (keyCode == KeyEvent.VK_LEFT)
  97. left_key = false;
  98. }
  99.  
  100. /* Handle the key pressed event
  101. * Check left or right arrow keys
  102. */
  103. public void keyPressed(KeyEvent e)
  104. {
  105. int keyCode = e.getKeyCode();
  106.  
  107. if (keyCode == KeyEvent.VK_RIGHT)
  108. {
  109. right_key = true;
  110. if (ball.vx == 0 && ball.vy == 0)
  111. {
  112. ball.vx = -1;
  113. ball.vy = -1;
  114. }
  115. }
  116. else if (keyCode == KeyEvent.VK_LEFT)
  117. {
  118. left_key = true;
  119. if (ball.vx == 0 && ball.vy == 0)
  120. {
  121. ball.vx = 1;
  122. ball.vy = -1;
  123. }
  124. }
  125. }
  126.  
  127. }
  128.  
  129. // Requests that this Component gets the input focus for the keyboard
  130. setFocusable(true);
  131.  
  132. requestFocusInWindow();
  133.  
  134. KeyListener listener = new ArrowKeyListener();
  135. addKeyListener(listener);
  136.  
  137. }
  138.  
  139. /*
  140. * This method returns a String which describes this object
  141. */
  142. public String toString()
  143. {
  144. return "";// INSERT CODE HERE
  145. }
  146.  
  147. /*
  148. * Resets the game to starting conditions
  149. */
  150. public void reset()
  151. {
  152. // INSERT CODE HERE
  153. }
  154.  
  155. /*
  156. * Override the paintComponent method
  157. * This is called whenever the component needs to be repainted
  158. */
  159. public void paintComponent(Graphics g)
  160. {
  161. // Restore the graphics context
  162. Graphics2D g2 = (Graphics2D) g;
  163.  
  164. // Draw score
  165. // INSERT CODE HERE
  166.  
  167. // Draw bricks
  168.  
  169. for(int i=0; i < ROWS; i++)
  170. {
  171. for(int j = 0; j < COLS; j++)
  172. {
  173. if (brick[i][j] != null)
  174. {
  175. g2.setColor(Color.green);
  176. g2.draw(brick[i][j]);
  177. g2.fill(brick[i][j]);
  178. }
  179. }
  180.  
  181. }
  182. // INSERT CODE HERE
  183.  
  184. // Draw paddle
  185. g2.setColor(Color.blue);
  186. g2.draw(paddle);
  187. g2.fill(paddle);
  188.  
  189. // Draw Ball
  190. g2.setColor(Color.red);
  191. g2.draw(ball);
  192. g2.fill(ball);
  193. // INSERT CODE HERE
  194. }
  195.  
  196. /*
  197. * Returns true if the game is over
  198. */
  199. public boolean isGameOver()
  200. {
  201. return true;
  202. }
  203.  
  204. /*
  205. * Get the score
  206. */
  207. public int getScore()
  208. {
  209. return score;
  210. }
  211.  
  212. /*
  213. * Updates the state of the game including:
  214. * -position of the paddle
  215. * -position of the ball
  216. * -check for collisions
  217. */
  218. public void update()
  219. {
  220. // If left or right arrow keys are pressed
  221. // then move the paddle left or right paddle
  222. // INSERT CODE HERE
  223. if (right_key == true)
  224. {
  225. paddle.movePaddleRight();
  226. }
  227. else if (left_key == true)
  228. {
  229. paddle.movePaddleLeft();
  230. }
  231.  
  232. // Update ball
  233. ball.update();
  234. repaint();
  235.  
  236. // If ball hits the paddle then bounce off paddle and return
  237. if(ball.intersects(paddle))
  238. {
  239. ball.yBounce();
  240. ball.x += Ball.vx;
  241. ball.y += Ball.vy;
  242. return;
  243. }
  244.  
  245. // If ball reaches bottom of the screen, then the game is over :(
  246. if(ball.y >= 350)
  247. {
  248. ball.vx = 0;
  249. ball.vy = 0;
  250. paddle.x = (GameFrame.FRAME_WIDTH/2) - (Paddle.PADDLE_WIDTH/2);
  251. ball.x = paddle.x + (Paddle.PADDLE_WIDTH/2) - (Ball.BALL_DIAMETER/2);
  252. ball.y = paddle.y - (2*Paddle.PADDLE_HEIGHT);
  253. JFrame frame = new JFrame();
  254. JOptionPane.showMessageDialog(frame, "Game Over!");
  255. }
  256.  
  257. // Check if ball intersects any of the bricks:
  258. // if it hits a brick, set the brick to null to remove it
  259. // and increment the score
  260. // INSERT CODE HERE
  261.  
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement