Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Breakout.java
- * -------------------
- * Name: Angel Lopez
- * Section Leader: Steven Vellon
- *
- * This file will eventually implement the game of Breakout.
- */
- import acm.graphics.*;
- import acm.program.*;
- import acm.util.*;
- //import java.applet.*;
- import java.applet.AudioClip;
- import java.awt.*;
- import java.awt.event.*;
- public class Breakout extends GraphicsProgram {
- public static final int APPLICATION_WIDTH = 400;
- public static final int APPLICATION_HEIGHT = 600;
- private static final int WIDTH = APPLICATION_WIDTH;
- private static final int HEIGHT = APPLICATION_HEIGHT;
- private static final int PADDLE_WIDTH = 60;
- private static final int PADDLE_HEIGHT = 10;
- private static final int PADDLE_Y_OFFSET = 30;
- private static final int NBRICKS_PER_ROW = 10;
- private static final int NBRICK_ROWS = 10;
- private static final int BRICK_SEP = 4;
- private static final double BRICK_WIDTH = (double) (WIDTH - (NBRICKS_PER_ROW - 1)
- * BRICK_SEP)
- / NBRICKS_PER_ROW; // had to be double to accurately center bricks!
- private static final int BRICK_HEIGHT = 8;
- private static final int BALL_RADIUS = 10;
- private static final int DIAMETER = 2 * BALL_RADIUS;
- private static final int BRICK_Y_OFFSET = 70;
- private static final int NTURNS = 3;
- /*
- * Runs breakout program
- */
- public void run() {
- turnNumber = 0;
- createGame();
- addMouseListeners();
- playGame();
- }
- private void createGame() {
- beginningSequence();
- setUpBricks();
- createPaddle();
- createBall();
- bounceClip.play();
- }
- /*
- * Places bricks in appropriate locations and gives them color
- */
- private void setUpBricks() {
- for (int brickRow = 0; brickRow < NBRICK_ROWS; brickRow++) {
- for (int brickColumn = 0; brickColumn < NBRICKS_PER_ROW; brickColumn++) {
- double x = (BRICK_SEP + BRICK_WIDTH) * brickColumn;
- double y = BRICK_Y_OFFSET + (BRICK_SEP + BRICK_HEIGHT)
- * brickRow;
- brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
- add(brick);
- brick.setFilled(true);
- brick.setColor(colorBrick(brickRow));
- brick.setFillColor(colorBrick(brickRow));
- }
- }
- }
- /*
- * Determines colors of bricks
- */
- private Color colorBrick(int brickRow) {
- switch (brickRow) {
- case 0:
- colorOfBrick = Color.RED;
- break;
- case 1:
- colorOfBrick = Color.RED;
- break;
- case 2:
- colorOfBrick = Color.ORANGE;
- break;
- case 3:
- colorOfBrick = Color.ORANGE;
- break;
- case 4:
- colorOfBrick = Color.YELLOW;
- break;
- case 5:
- colorOfBrick = Color.YELLOW;
- break;
- case 6:
- colorOfBrick = Color.GREEN;
- break;
- case 7:
- colorOfBrick = Color.GREEN;
- break;
- case 8:
- colorOfBrick = Color.CYAN;
- break;
- case 9:
- colorOfBrick = Color.CYAN;
- break;
- }
- return colorOfBrick;
- }
- /*
- * creates a movable paddle positioned at its appropriate location
- */
- private void createPaddle() {
- double x = (WIDTH - PADDLE_WIDTH) / 2;
- double y = HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
- paddle = new GRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
- paddle.setFilled(true);
- add(paddle);
- }
- /*
- * moves paddle by following mouse
- */
- public void mouseMoved(MouseEvent e) {
- double x = e.getX() - PADDLE_WIDTH / 2; // centers mouse on middle of
- // paddle
- double y = HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
- if (e.getX() < PADDLE_WIDTH / 2) {
- paddle.setLocation(0, y);
- } else if (e.getX() > WIDTH - PADDLE_WIDTH / 2) {
- paddle.setLocation(WIDTH - PADDLE_WIDTH, y);
- } else {
- paddle.setLocation(x, y);
- }
- }
- /*
- * creates ball
- */
- private void createBall() {
- ball = new GOval(ballInitialX, ballInitialY, 2 * BALL_RADIUS,
- 2 * BALL_RADIUS);
- ball.setFilled(true);
- add(ball);
- }
- /*
- * Begins the gameplay
- */
- private void playGame() {
- waitForClick();
- while (ball.getY() < HEIGHT - PADDLE_Y_OFFSET) {
- moveBall();
- }
- if (ball.getY() >= HEIGHT - PADDLE_Y_OFFSET) {
- resetGame();
- }
- }
- /*
- * sets ball's velocity with respect to x
- */
- private void setVelocity() {
- vx = rgen.nextDouble(1.0, 3.0);
- if (rgen.nextBoolean(0.5))
- vx = -vx;
- }
- /*
- * checks for collisions and adjusts velocities appropriately.
- */
- private void checkForCollisions() {
- checkForWalls();
- getCollidingObject();
- if (collidingObject == paddle) {
- setVelocity();
- vy = -vy;
- } else if (collidingObject != null) {
- remove(collidingObject);
- bounceClip.play();
- setVelocity();
- vy = -vy;
- brickTotal--;
- }
- }
- /*
- * finds out what the colliding objects with the ball are
- */
- private void getCollidingObject() {
- double x = ball.getX();
- double y = ball.getY();
- if (getElementAt(x, y) != null) {
- collidingObject = getElementAt(x, y);
- } else if (getElementAt(x + DIAMETER, y) != null) {
- collidingObject = getElementAt(x + DIAMETER, y);
- } else if (getElementAt(x, y + DIAMETER) != null) {
- collidingObject = getElementAt(x, y + DIAMETER);
- } else if (getElementAt(x + DIAMETER, y + DIAMETER) != null) {
- collidingObject = getElementAt(x + DIAMETER, y + DIAMETER);
- } else {
- collidingObject = null;
- }
- }
- /*
- * Bounces ball if it hits application edges (other than bottom edge)
- */
- private void checkForWalls() {
- if (ball.getX() <= 0 || ball.getX() + 2 * BALL_RADIUS >= WIDTH) {
- vx = -vx;
- }
- if (ball.getY() <= 0) {
- vy = -vy;
- }
- if (ball.getY() > HEIGHT - PADDLE_Y_OFFSET) {
- vy = -vy;
- }
- }
- /*
- * Moves ball
- */
- private void moveBall() {
- ball.move(vx, vy);
- checkForCollisions();
- pause(DELAY);
- if (brickTotal == 0) {
- winGame();
- }
- }
- /*
- * Resets game after ball falls past application width
- */
- private void resetGame() {
- remove(ball);
- turnNumber++;
- if (turnNumber < NTURNS) {
- add(ball, ballInitialX, ballInitialY);
- vy = -vy;
- vx = 0;
- playGame();
- } else if (brickTotal > 0) {
- loseGame();
- }
- }
- /*
- * Ends game (Winning situation)
- */
- private void winGame() {
- removeAll();
- GLabel youWin = new GLabel("YOU WIN!");
- youWin.setFont("Serif-BOLD-48");
- youWin.setColor(Color.GREEN);
- double winWidth = youWin.getWidth();
- double winHeight = youWin.getAscent();
- add(youWin, (WIDTH - winWidth) / 2, (HEIGHT - winHeight) / 2);
- pause(10000);
- }
- /*
- * Ends game (losing situation), says score
- */
- private void loseGame() {
- removeAll();
- int score = NBRICK_ROWS * NBRICKS_PER_ROW - brickTotal;
- GLabel youLose = new GLabel("YOU LOSE!");
- youLose.setFont("SERIF-BOLD-48");
- youLose.setColor(Color.RED);
- double loseWidth = youLose.getWidth();
- double loseHeight = youLose.getAscent();
- add(youLose, (WIDTH - loseWidth) / 2, (HEIGHT - loseHeight) / 2);
- GLabel losingScore = new GLabel("Final Score: " + score + " / 100");
- losingScore.setFont("Serif-24");
- double scoreWidth = losingScore.getWidth();
- double scoreHeight = losingScore.getAscent();
- add(losingScore, (WIDTH - scoreWidth) / 2, (HEIGHT - scoreHeight) / 2
- + youLose.getHeight());
- }
- /*
- * Credits sequence before game begins
- */
- private void beginningSequence() {
- enterBreakoutAnimation();
- showCreator();
- showBeginningInstruction();
- }
- /*
- * displays breakout beginning animation
- */
- private void enterBreakoutAnimation() {
- breakout.setFont("Serif-BOLD-48");
- double x = (WIDTH - breakout.getWidth()) / 2;
- double y = (HEIGHT - breakout.getAscent()) / 2;
- add(breakout, x, y);
- changeBeginningSequenceColor();
- removeAll();
- pause(1000);
- }
- /*
- * Used for the animation of breakout logo
- */
- private void changeBeginningSequenceColor() {
- for (int i = 0; i < 10; i++) {
- breakout.setColor(Color.RED);
- pause(20);
- breakout.setColor(Color.ORANGE);
- pause(20);
- breakout.setColor(Color.YELLOW);
- pause(20);
- breakout.setColor(Color.GREEN);
- pause(20);
- breakout.setColor(Color.CYAN);
- pause(20);
- }
- breakout.setColor(Color.LIGHT_GRAY);
- pause(20);
- breakout.setColor(Color.WHITE);
- pause(20);
- }
- /*
- * Displays name of creator of application (me)
- */
- private void showCreator() {
- GLabel createdBy = new GLabel("Created by");
- createdBy.setFont("Serif-24");
- double createdByX = (WIDTH - createdBy.getWidth()) / 2;
- double createdByY = (HEIGHT - createdBy.getAscent() - 3 * createdBy
- .getHeight()) / 2;
- GLabel angelLopez = new GLabel("Angel Lopez");
- angelLopez.setFont("Serif-48");
- double angelLopezX = (WIDTH - angelLopez.getWidth()) / 2;
- double angelLopezY = (HEIGHT - createdBy.getAscent()) / 2;
- add(createdBy, createdByX, createdByY);
- bounceClip.play();
- pause(500);
- add(angelLopez, angelLopezX, angelLopezY);
- bounceClip.play();
- pause(3000);
- createdBy.setColor(Color.DARK_GRAY);
- angelLopez.setColor(Color.DARK_GRAY);
- pause(75);
- createdBy.setColor(Color.LIGHT_GRAY);
- angelLopez.setColor(Color.LIGHT_GRAY);
- pause(75);
- removeAll();
- pause(1000);
- }
- private void showBeginningInstruction() {
- GLabel beginningInstruction = new GLabel("Click to begin!");
- beginningInstruction.setFont("Serif-24");
- double x = (WIDTH - beginningInstruction.getWidth()) / 2;
- double y = (HEIGHT - beginningInstruction.getAscent() + beginningInstruction
- .getHeight()) / 2;
- add(beginningInstruction, x, y);
- bounceClip.play();
- pause(2000);
- removeAll();
- }
- private double vx;
- private double vy = 3;
- private double ballInitialX = (WIDTH - 2 * BALL_RADIUS) / 2;
- private double ballInitialY = (HEIGHT - 2 * BALL_RADIUS) / 2;
- private int turnNumber;
- private int brickTotal = NBRICKS_PER_ROW * NBRICK_ROWS;
- private int DELAY = 10; // smaller delay = faster game speed
- private GLabel breakout = new GLabel("BREAKOUT");
- private GOval ball;
- private GObject collidingObject;
- private GRect brick;
- private GRect paddle;
- private Color colorOfBrick;
- private RandomGenerator rgen = RandomGenerator.getInstance();
- private AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement