Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JComponent;
- import java.awt.geom.Rectangle2D;
- import java.awt.Rectangle;
- import java.awt.event.KeyListener;
- import java.awt.event.KeyEvent;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- /*
- * Game Component class
- * used to display the game inside the window frame
- */
- public class Game extends JComponent
- {
- // Private instance fields go here
- private Rectangle2D.Double brick[][]; // 2D array of bricks
- private double X = 10.0;
- private double Y = -30.0;
- private Paddle paddle; // game paddle
- private Ball ball; // the ball
- private int score; // Score: increments with each brick removed
- // Arrow key state variables
- private boolean right_key; // State of right arrow key
- private boolean left_key; // State of left arrow key
- private boolean space_bar; // State of spacebar
- // Constants
- private final int ROWS = 5; // #rows of bricks
- private final int COLS = 10; // #columns of bricks
- private final double BRICK_WIDTH = 20.0; // Width of a brick
- private final double BRICK_HEIGHT = 10.0; // Height of a brick
- /*
- * Constructor
- */
- public Game()
- {
- super(); // Call superclass constructor
- // INSERT CODE HERE
- // Inner class to monitor keyboard movements
- class ArrowKeyListener implements KeyListener
- {
- /* This method is required by the KeyListener interface
- * but is not required for this game
- */
- public void keyTyped(KeyEvent e)
- { }
- /* Handle the key release event
- * Check left or right arrow keys
- */
- public void keyReleased(KeyEvent e)
- {
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_RIGHT)
- right_key = false;
- else if (keyCode == KeyEvent.VK_LEFT)
- left_key = false;
- }
- /* Handle the key pressed event
- * Check left or right arrow keys
- */
- public void keyPressed(KeyEvent e)
- {
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_RIGHT)
- right_key = true;
- else if (keyCode == KeyEvent.VK_LEFT)
- left_key = true;
- }
- }
- // Requests that this Component gets the input focus for the keyboard
- setFocusable(true);
- requestFocusInWindow();
- KeyListener listener = new ArrowKeyListener();
- addKeyListener(listener);
- }
- /*
- * This method returns a String which describes this object
- */
- public String toString()
- {
- return "";// INSERT CODE HERE
- }
- /*
- * Resets the game to starting conditions
- */
- public void reset()
- {
- // INSERT CODE HERE
- }
- /*
- * Override the paintComponent method
- * This is called whenever the component needs to be repainted
- */
- public void paintComponent(Graphics g)
- {
- // Restore the graphics context
- Graphics2D g2 = (Graphics2D) g;
- // Draw score
- // INSERT CODE HERE
- // Draw bricks
- g2.setColor(Color.green);
- for(int i=0; i < ROWS; i++)
- {
- X = 18;
- for(int j = 0; j < COLS; j++)
- {
- brick[i][j].setRect(X, Y, BRICK_WIDTH, BRICK_HEIGHT);
- g2.draw(brick[i][j]);
- g2.fill(brick[i][j]);
- X += BRICK_WIDTH + 5;
- }
- Y += BRICK_HEIGHT + 5;
- }
- // INSERT CODE HERE
- // Draw paddle
- // INSERT CODE HERE
- // Draw Ball
- // INSERT CODE HERE
- }
- /*
- * Returns true if the game is over
- */
- public boolean isGameOver()
- {
- return true;
- }
- /*
- * Get the score
- */
- public int getScore()
- {
- return score;
- }
- /*
- * Updates the state of the game including:
- * -position of the paddle
- * -position of the ball
- * -check for collisions
- */
- public void update()
- {
- // If left or right arrow keys are pressed
- // then move the paddle left or right paddle
- // INSERT CODE HERE
- // Update ball
- ball.update();
- // If ball hits the paddle then bounce off paddle and return
- // INSERT CODE HERE
- // If ball reaches bottom of the screen, then the game is over :(
- // INSERT CODE HERE
- // Check if ball intersects any of the bricks:
- // if it hits a brick, set the brick to null to remove it
- // and increment the score
- // INSERT CODE HERE
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment