Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Breakout.java
- * -------------------
- * Name:
- * Section Leader:
- *
- * This file will eventually implement the game of Breakout.
- */
- import acm.graphics.*;
- import acm.program.*;
- import acm.util.*;
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Breakout extends GraphicsProgram {
- /** Width and height of application window in pixels */
- public static final int APPLICATION_WIDTH = 400;
- public static final int APPLICATION_HEIGHT = 600;
- /** Dimensions of the paddle */
- private static final int PADDLE_WIDTH = 60;
- private static final int PADDLE_HEIGHT = 10;
- /** Offset of the paddle up from the bottom */
- private static final int PADDLE_Y_OFFSET = 30;
- /** Number of bricks per row */
- private static final int NBRICKS_PER_ROW = 10;
- /** Number of rows of bricks */
- private static final int NBRICK_ROWS = 10;
- /** Separation between bricks */
- private static final int BRICK_SEP = 4;
- private static final int NUMBER_BRICKS = NBRICKS_PER_ROW*NBRICK_ROWS ;
- /** Width of a brick */
- private static final int BRICK_WIDTH =
- (APPLICATION_WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
- /** Height of a brick */
- private static final int BRICK_HEIGHT = 8;
- /** Radius of the ball in pixels */
- private static final int BALL_RADIUS = 10;
- /** Offset of the top brick row from the top */
- private static final int BRICK_Y_OFFSET = 70;
- /** Number of turns */
- private static final int NTURNS = 3;
- /** Initial Y velocity */
- private static final double Y_VELOCITY = 3.0;
- /** Delay between ball updates */
- private static final int ANIMATION_PAUSE = 10;
- //Declaration of all the instance variables
- private GRect paddle;
- private GOval ball;
- private GLabel messageLabel;
- private int numBricksLeft = NUMBER_BRICKS;
- private RandomGenerator rgen = RandomGenerator.getInstance();
- AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
- public static void main(String[] args) {
- new Breakout().start(args);
- }
- public void run() {
- addMouseListeners();
- setUp();
- for(int i = 0; i < NTURNS; i++) {
- setUpTurn(i);
- playTurn();
- if(numBricksLeft == 0) break;
- }
- if(numBricksLeft == 0) {
- addMessage("You Win");
- } else {
- addMessage("You Lose");
- }
- }
- private void setUpTurn(int turnNumber) {
- removeBall();
- addMessage("Turns left: " + (NTURNS - turnNumber));
- waitForClick();
- removeMessage();
- addBall();
- }
- private void setUp() {
- addBricks();
- addPaddle();
- }
- /* Method: run() */
- /** Runs the Breakout program. */
- private void addMessage(String message) {
- messageLabel = new GLabel(message);
- messageLabel.setFont("Monospace-32");
- messageLabel.setLocation(getWidth()/2 - messageLabel.getWidth()/2, getHeight()/2 - messageLabel.getHeight()/2);
- add(messageLabel);
- }
- private void removeMessage() {
- if(messageLabel != null) {
- remove(messageLabel);
- }
- }
- public void addBricks() {
- for (int i = 0; i < NBRICK_ROWS; i++) {
- Color color = getColorForRow(i);
- addRow(BRICK_SEP/2, BRICK_Y_OFFSET + i*(BRICK_HEIGHT + BRICK_SEP), color);
- }
- }
- private Color getColorForRow(int rowIndex) {
- Color color;
- switch(rowIndex) {
- case 0: case 1: color = Color.RED; break;
- case 2: case 3: color = Color.ORANGE; break;
- case 4: case 5: color =Color.YELLOW; break;
- case 6:case 7: color= Color.green; break;
- case 8: case 9: color = Color.blue; break;
- default: color = Color.BLACK; break;
- }
- return color;
- }
- private void addRow(int rowX, int rowY, Color color) {
- for(int i = 0; i < NBRICKS_PER_ROW; i++) {
- int brickX = rowX = (i * BRICK_SEP) + (i * BRICK_WIDTH);
- GRect rect = new GRect (brickX, rowY, BRICK_WIDTH, BRICK_HEIGHT);
- rect.setFilled(true);
- rect.setColor(color);
- rect.setFillColor(color);
- add(rect);
- }
- }
- private void addPaddle() {
- paddle = new GRect(getWidth()/2-PADDLE_WIDTH/2,getHeight()-PADDLE_Y_OFFSET,
- PADDLE_WIDTH, PADDLE_HEIGHT);
- paddle.setFilled(true);
- paddle.setFillColor(Color.BLACK);
- add(paddle);
- }
- public void mouseMoved(MouseEvent me) {
- int x = me.getX();
- if(paddle != null) {
- if(x > PADDLE_WIDTH/2 && x< getWidth()-PADDLE_WIDTH/2)
- paddle.setLocation(x - PADDLE_WIDTH/2, getHeight() - PADDLE_Y_OFFSET);
- }
- }
- private void addBall() {
- ball = new GOval(getWidth()/2 - BALL_RADIUS, getHeight()/2 - BALL_RADIUS, 2*BALL_RADIUS, 2*BALL_RADIUS);
- ball.setFilled(true);
- ball.setFillColor(Color.BLACK);
- add(ball);
- }
- private void removeBall() {
- if(ball != null) {
- remove(ball);
- }
- }
- private void playTurn() {
- double vx = rgen.nextDouble(1.0, 3.0);
- if(rgen.nextBoolean(.5)) vx =- vx;
- double vy = Y_VELOCITY;
- while(true) {
- if(numBricksLeft == 0)
- break;
- if(ball.getY() > APPLICATION_HEIGHT)
- break;
- ball.move(vx,vy);
- pause(ANIMATION_PAUSE);
- vy = reflectOffColliders(vy);
- vx = reflectOffSideWalls(vx);
- vy = reflectOffTopWall(vy);
- }
- }
- private double reflectOffColliders(double vy) {
- GObject collider = getCollidingObject();
- if(collider == paddle) {
- bounceClip.play();
- vy = -Math.abs(vy);
- }
- else if (collider != null) {
- remove(collider);
- vy *= -1;
- }
- return vy;
- }
- private double reflectOffTopWall(double vy) {
- if (ball.getY() < 0) {
- vy *= -1;
- }
- return vy;
- }
- private double reflectOffSideWalls(double vx) {
- if (ball.getX() < 0 || ball.getX() > APPLICATION_WIDTH - ball.getWidth()) {
- vx *= -1;
- }
- return vx;
- }
- private GObject getCollidingObject() {
- //Finds collider, if the ball collided on the top left corner
- if (getElementAt(ball.getX(), ball.getY()) != null) {
- return getElementAt(ball.getX(), ball.getY()) ;
- }
- //Finds collider, if the ball collided on the top right corner
- else if (getElementAt(ball.getX()+2*BALL_RADIUS, ball.getY()) != null){
- return getElementAt(ball.getX()+2*BALL_RADIUS, ball.getY()) ;
- }
- //Finds collider, if the ball collided on the bottom left corner
- else if (getElementAt(ball.getX(), ball.getY() + 2*BALL_RADIUS) != null) {
- return getElementAt(ball.getX(), ball.getY() + 2* BALL_RADIUS) ;
- }
- //Finds collider, if the ball collided on the bottom right corner
- else if (getElementAt(ball.getX()+ 2*BALL_RADIUS, ball.getY() + 2*BALL_RADIUS) != null) {
- return getElementAt(ball.getX()+ 2*BALL_RADIUS, ball.getY() + 2*BALL_RADIUS);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement