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 game board (usually the same) */
- private static final int WIDTH = APPLICATION_WIDTH;
- private static final int HEIGHT = APPLICATION_HEIGHT;
- /** 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;
- /** Width of a brick */
- private static final int BRICK_WIDTH = (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 int NTURNS = 4;
- private static int LifeCounter = NTURNS;
- /* Method: run() */
- /** Runs the Breakout program. */
- public void run() {
- this.setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
- setup();
- play();
- }
- private void secondChallange() {
- GPoint k = new GPoint(ball.getX(), ball.getY());
- remove(ball);
- ball = new GOval(BALL_RADIUS, BALL_RADIUS);
- ball.setFilled(true);
- add(ball, k);
- secondChallangeb = true;
- }
- private void firstChallange() {
- GPoint k = new GPoint(paddle.getX(), paddle.getY());
- remove(paddle);
- paddle = new GRect(PADDLE_WIDTH * 0.7, PADDLE_HEIGHT);
- paddle.setFilled(true);
- add(paddle, k);
- }
- private void setup() {
- addMouseListeners();
- drawBricks();
- drawPaddle();
- }
- private void play() {
- GLabel press = new GLabel("Click the mouse to start");
- add(press, (WIDTH - press.getWidth()) / 1.9, (HEIGHT - press.getAscent()) / 1.5);
- addBall();
- waitForClick();
- remove(press);
- while (true) {
- moveBall();
- checkForCollisions();
- if (NUMBEROFBRICKS == firstChallange) {
- firstChallange();
- }
- if (NUMBEROFBRICKS == secondChallange) {
- secondChallange();
- }
- if (ball == null && LifeCounter != 0) {
- pause(100 * DELAY);
- addBall();
- }
- if (GameOver()) {
- if (NUMBEROFBRICKS == 0) {
- removeAll();
- GLabel winner = new GLabel("You Win");
- winner.setFont("SansSerif-bold-50");
- add(winner, (WIDTH - winner.getWidth()) / 2, (HEIGHT - winner.getAscent()) / 2);
- GLabel res = new GLabel("Click the mouse to restart");
- add(res, (WIDTH - winner.getWidth()) / 1.3, (HEIGHT - winner.getAscent()) / 1.5);
- }
- if (LifeCounter == 0) {
- removeAll();
- GLabel loser = new GLabel("You Lost");
- loser.setFont("SansSerif-bold-50");
- add(loser, (WIDTH - loser.getWidth()) / 2, (HEIGHT - loser.getAscent()) / 2);
- GLabel res = new GLabel("Click the mouse to restart");
- add(res, (WIDTH - loser.getWidth()) / 1.3, (HEIGHT - loser.getAscent()) / 1.5);
- }
- waitForClick();
- restart();
- }
- pause(DELAY);
- }
- }
- private void restart() {
- removeAll();
- LifeCounter = NTURNS;
- BrickCounter = NUMBEROFBRICKS;
- run();
- }
- public void mousePressed(MouseEvent e) {
- last = new GPoint(e.getPoint());
- }
- public void mouseMoved(MouseEvent e) {
- int x = Math.min(WIDTH - PADDLE_WIDTH, Math.max(0, e.getX() - PADDLE_WIDTH / 2));
- paddle.setLocation(x, HEIGHT - PADDLE_Y_OFFSET);
- }
- private void moveBall() {
- ball.move(xMove, yMove);
- }
- // Gravity
- private void checkForCollisions() {
- GObject UpBallLeft = getElementAt(ball.getX(), ball.getY());
- GObject UpBallRight = getElementAt(ball.getX() + ball.getWidth(), ball.getY());
- GObject DownBallLeft = getElementAt(ball.getX(), ball.getY() + ball.getHeight());
- GObject DownBallRight = getElementAt(ball.getX() + ball.getWidth(), ball.getY() + ball.getHeight());
- if (ball.getY() < 0) {
- if (yMove < 0) {
- yMove = yMove * -1;
- }
- } else if (DownBallRight == paddle || DownBallLeft == paddle) {
- if (yMove > 0) {
- yMove = yMove * -1;
- }
- } else if (ball.getX() + 2 * BALL_RADIUS > APPLICATION_WIDTH) {
- if (xMove > 0) {
- xMove = xMove * -1;
- }
- } else if (ball.getX() < 0) {
- if (xMove < 0) {
- xMove = xMove * -1;
- }
- } else if (ball.getY() + 2 * BALL_RADIUS > HEIGHT) {
- remove(ball);
- ball = null;
- LifeCounter--;
- println("Yeak");
- }
- else if (UpBallLeft != null | UpBallRight != null) {
- if (yMove < 0) {
- yMove = yMove * -1;
- }
- if (UpBallLeft != null) {
- remove(UpBallLeft);
- UpBallLeft = null;
- NUMBEROFBRICKS--;
- } else if (UpBallRight != null) {
- remove(UpBallRight);
- UpBallRight = null;
- NUMBEROFBRICKS--;
- }
- } else if (DownBallLeft != null | DownBallRight != null) {
- if (yMove > 0) {
- yMove = yMove * -1;
- }
- if (DownBallLeft != null) {
- remove(DownBallLeft);
- DownBallLeft = null;
- NUMBEROFBRICKS--;
- } else if (DownBallRight != null) {
- remove(DownBallRight);
- DownBallRight = null;
- NUMBEROFBRICKS--;
- }
- }
- }
- // Check if The Game is over
- private boolean GameOver() {
- if (LifeCounter == 0)
- return true;
- if (NUMBEROFBRICKS == 0)
- return true;
- return false;
- }
- private void addBall() {
- if (!secondChallangeb) {
- ball = new GOval(APPLICATION_WIDTH / 2 - BALL_RADIUS / 2, APPLICATION_HEIGHT / 2 - BALL_RADIUS / 2,
- 2 * BALL_RADIUS, 2 * BALL_RADIUS);
- } else
- ball = new GOval(APPLICATION_WIDTH / 2 - BALL_RADIUS / 2, APPLICATION_HEIGHT / 2 - BALL_RADIUS / 2,
- BALL_RADIUS, BALL_RADIUS);
- ball.setFilled(true);
- add(ball);
- }
- private void drawPaddle() {
- paddle = new GRect(APPLICATION_WIDTH / 2 - PADDLE_WIDTH / 2, HEIGHT - PADDLE_Y_OFFSET, PADDLE_WIDTH,
- PADDLE_HEIGHT);
- paddle.setFilled(true);
- add(paddle);
- }
- private void drawBricks() {
- for (int row = 0; row < NBRICK_ROWS; row++) {
- for (int brick = 0; brick < NBRICKS_PER_ROW; brick++) {
- GRect rect = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
- rect.setFilled(true);
- if (row == 0 || row == 1) {
- rect.setFillColor(Color.RED);
- rect.setColor(Color.RED);
- } else if (row == 2 || row == 3) {
- rect.setFillColor(Color.ORANGE);
- rect.setColor(Color.ORANGE);
- } else if (row == 4 || row == 5) {
- rect.setFillColor(Color.YELLOW);
- rect.setColor(Color.YELLOW);
- } else if (row == 6 || row == 7) {
- rect.setFillColor(Color.GREEN);
- rect.setColor(Color.GREEN);
- } else if (row == 8 || row == 9) {
- rect.setFillColor(Color.CYAN);
- rect.setColor(Color.CYAN);
- }
- add(rect, brick * (BRICK_WIDTH + BRICK_SEP), row * (BRICK_HEIGHT + BRICK_SEP) + BRICK_Y_OFFSET);
- }
- }
- }
- private static double yMove = 2;
- private static double xMove = yMove * Math.cos(1.03);
- private static int NUMBEROFBRICKS = NBRICK_ROWS * NBRICKS_PER_ROW;
- private static int BrickCounter = NUMBEROFBRICKS;
- private GOval ball;
- private GRect paddle;
- private GPoint last;
- private int DELAY = 10;
- private int firstChallange = NBRICK_ROWS * NBRICKS_PER_ROW - 2;
- private int secondChallange = NBRICK_ROWS * NBRICKS_PER_ROW - 5;
- private boolean secondChallangeb = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement