Advertisement
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.*;
- import java.awt.event.*;
- import java.awt.event.KeyListener;
- import java.awt.event.KeyEvent;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import javax.swing.Timer;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- /*
- * Game Component class
- * used to display the game inside the window frame
- */
- public class Game extends JComponent
- {
- // Private instance fields go here
- public static Rectangle2D.Double brick[][]; // 2D array of bricks
- public static Paddle paddle; // game paddle
- private Ball ball; // the ball
- private int score; // Score: increments with each brick removed
- // Arrow key state variables
- public static boolean right_key; // State of right arrow key
- public static boolean left_key; // State of left arrow key
- // Constants
- public static final int ROWS = 5; // #rows of bricks
- public static int COLS = 10; // #columns of bricks
- public static final double BRICK_WIDTH = 20.0; // Width of a brick
- public static final double BRICK_HEIGHT = 10.0; // Height of a brick
- private final double SPACING = 5.0; // Space between bricks
- private final int DELAY = 10;
- /*
- * Constructor
- */
- public Game()
- {
- super(); // Call superclass constructor
- // INSERT CODE HERE
- brick = new Rectangle2D.Double[ROWS][COLS];
- double defaultBorder = 27.5;
- for(int i=0; i < ROWS; i++)
- {
- for(int j = 0; j < COLS; j++)
- {
- brick[i][j] = new Rectangle2D.Double(defaultBorder+(j*SPACING)+(j*BRICK_WIDTH),((i+1)*SPACING)+((i)*BRICK_HEIGHT),BRICK_WIDTH ,BRICK_HEIGHT);
- }
- }
- // make paddle here
- paddle = new Paddle();
- // ball
- ball = new Ball();
- class TimerListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- update();
- }
- }
- ActionListener timeListener = new TimerListener();
- Timer timer = new Timer(DELAY, timeListener);
- timer.start();
- // 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;
- if (ball.vx == 0 && ball.vy == 0)
- {
- ball.vx = -1;
- ball.vy = -1;
- }
- }
- else if (keyCode == KeyEvent.VK_LEFT)
- {
- left_key = true;
- if (ball.vx == 0 && ball.vy == 0)
- {
- ball.vx = 1;
- ball.vy = -1;
- }
- }
- }
- }
- // 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
- for(int i=0; i < ROWS; i++)
- {
- for(int j = 0; j < COLS; j++)
- {
- if (brick[i][j] != null)
- {
- g2.setColor(Color.green);
- g2.draw(brick[i][j]);
- g2.fill(brick[i][j]);
- }
- }
- }
- // INSERT CODE HERE
- // Draw paddle
- g2.setColor(Color.blue);
- g2.draw(paddle);
- g2.fill(paddle);
- // Draw Ball
- g2.setColor(Color.red);
- g2.draw(ball);
- g2.fill(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
- if (right_key == true)
- {
- paddle.movePaddleRight();
- }
- else if (left_key == true)
- {
- paddle.movePaddleLeft();
- }
- // Update ball
- ball.update();
- repaint();
- // If ball hits the paddle then bounce off paddle and return
- if(ball.intersects(paddle))
- {
- ball.yBounce();
- ball.x += Ball.vx;
- ball.y += Ball.vy;
- return;
- }
- // If ball reaches bottom of the screen, then the game is over :(
- if(ball.y >= 350)
- {
- ball.vx = 0;
- ball.vy = 0;
- paddle.x = (GameFrame.FRAME_WIDTH/2) - (Paddle.PADDLE_WIDTH/2);
- ball.x = paddle.x + (Paddle.PADDLE_WIDTH/2) - (Ball.BALL_DIAMETER/2);
- ball.y = paddle.y - (2*Paddle.PADDLE_HEIGHT);
- JFrame frame = new JFrame();
- JOptionPane.showMessageDialog(frame, "Game Over!");
- }
- // 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
Advertisement