neonfire999

Game

Mar 12th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. import javax.swing.JComponent;
  2. import java.awt.geom.Rectangle2D;
  3. import java.awt.Rectangle;
  4. import java.awt.event.KeyListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.Color;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9.  
  10. /*
  11. * Game Component class
  12. * used to display the game inside the window frame
  13. */
  14. public class Game extends JComponent
  15. {
  16. // Private instance fields go here
  17. private Rectangle2D.Double brick[][]; // 2D array of bricks
  18. private double X = 10.0;
  19. private double Y = -30.0;
  20. private Paddle paddle; // game paddle
  21. private Ball ball; // the ball
  22.  
  23. private int score; // Score: increments with each brick removed
  24.  
  25. // Arrow key state variables
  26. private boolean right_key; // State of right arrow key
  27. private boolean left_key; // State of left arrow key
  28. private boolean space_bar; // State of spacebar
  29.  
  30. // Constants
  31. private final int ROWS = 5; // #rows of bricks
  32. private final int COLS = 10; // #columns of bricks
  33. private final double BRICK_WIDTH = 20.0; // Width of a brick
  34. private final double BRICK_HEIGHT = 10.0; // Height of a brick
  35.  
  36. /*
  37. * Constructor
  38. */
  39. public Game()
  40. {
  41. super(); // Call superclass constructor
  42.  
  43. // INSERT CODE HERE
  44.  
  45. // Inner class to monitor keyboard movements
  46. class ArrowKeyListener implements KeyListener
  47. {
  48.  
  49. /* This method is required by the KeyListener interface
  50. * but is not required for this game
  51. */
  52. public void keyTyped(KeyEvent e)
  53. { }
  54.  
  55. /* Handle the key release event
  56. * Check left or right arrow keys
  57. */
  58. public void keyReleased(KeyEvent e)
  59. {
  60. int keyCode = e.getKeyCode();
  61.  
  62. if (keyCode == KeyEvent.VK_RIGHT)
  63. right_key = false;
  64. else if (keyCode == KeyEvent.VK_LEFT)
  65. left_key = false;
  66. }
  67.  
  68. /* Handle the key pressed event
  69. * Check left or right arrow keys
  70. */
  71. public void keyPressed(KeyEvent e)
  72. {
  73. int keyCode = e.getKeyCode();
  74.  
  75. if (keyCode == KeyEvent.VK_RIGHT)
  76. right_key = true;
  77. else if (keyCode == KeyEvent.VK_LEFT)
  78. left_key = true;
  79. }
  80.  
  81. }
  82.  
  83. // Requests that this Component gets the input focus for the keyboard
  84. setFocusable(true);
  85. requestFocusInWindow();
  86.  
  87. KeyListener listener = new ArrowKeyListener();
  88. addKeyListener(listener);
  89. }
  90.  
  91. /*
  92. * This method returns a String which describes this object
  93. */
  94. public String toString()
  95. {
  96. return "";// INSERT CODE HERE
  97. }
  98.  
  99. /*
  100. * Resets the game to starting conditions
  101. */
  102. public void reset()
  103. {
  104. // INSERT CODE HERE
  105. }
  106.  
  107. /*
  108. * Override the paintComponent method
  109. * This is called whenever the component needs to be repainted
  110. */
  111. public void paintComponent(Graphics g)
  112. {
  113. // Restore the graphics context
  114. Graphics2D g2 = (Graphics2D) g;
  115.  
  116. // Draw score
  117. // INSERT CODE HERE
  118.  
  119. // Draw bricks
  120. g2.setColor(Color.green);
  121. for(int i=0; i < ROWS; i++)
  122. {
  123. X = 18;
  124. for(int j = 0; j < COLS; j++)
  125. {
  126. brick[i][j].setRect(X, Y, BRICK_WIDTH, BRICK_HEIGHT);
  127. g2.draw(brick[i][j]);
  128. g2.fill(brick[i][j]);
  129. X += BRICK_WIDTH + 5;
  130. }
  131. Y += BRICK_HEIGHT + 5;
  132. }
  133. // INSERT CODE HERE
  134.  
  135. // Draw paddle
  136. // INSERT CODE HERE
  137.  
  138. // Draw Ball
  139. // INSERT CODE HERE
  140. }
  141.  
  142. /*
  143. * Returns true if the game is over
  144. */
  145. public boolean isGameOver()
  146. {
  147. return true;
  148. }
  149.  
  150. /*
  151. * Get the score
  152. */
  153. public int getScore()
  154. {
  155. return score;
  156. }
  157.  
  158. /*
  159. * Updates the state of the game including:
  160. * -position of the paddle
  161. * -position of the ball
  162. * -check for collisions
  163. */
  164. public void update()
  165. {
  166. // If left or right arrow keys are pressed
  167. // then move the paddle left or right paddle
  168. // INSERT CODE HERE
  169.  
  170. // Update ball
  171. ball.update();
  172.  
  173. // If ball hits the paddle then bounce off paddle and return
  174. // INSERT CODE HERE
  175.  
  176. // If ball reaches bottom of the screen, then the game is over :(
  177. // INSERT CODE HERE
  178.  
  179. // Check if ball intersects any of the bricks:
  180. // if it hits a brick, set the brick to null to remove it
  181. // and increment the score
  182. // INSERT CODE HERE
  183.  
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment