Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.79 KB | None | 0 0
  1. /*
  2.  * File: Breakout.java
  3.  * -------------------
  4.  * Name:
  5.  * Section Leader:
  6.  *
  7.  * This file will eventually implement the game of Breakout.
  8.  */
  9.  
  10. import acm.graphics.*;
  11. import acm.program.*;
  12. import acm.util.*;
  13.  
  14. import java.applet.*;
  15. import java.awt.*;
  16. import java.awt.event.*;
  17.  
  18. public class Breakout extends GraphicsProgram {
  19.  
  20.     /** Width and height of application window in pixels */
  21.     public static final int APPLICATION_WIDTH = 400;
  22.     public static final int APPLICATION_HEIGHT = 600;
  23.  
  24.     /** Dimensions of game board (usually the same) */
  25.     private static final int WIDTH = APPLICATION_WIDTH;
  26.     private static final int HEIGHT = APPLICATION_HEIGHT;
  27.  
  28.     /** Dimensions of the paddle */
  29.     private static final int PADDLE_WIDTH = 60;
  30.     private static final int PADDLE_HEIGHT = 10;
  31.  
  32.     /** Offset of the paddle up from the bottom */
  33.     private static final int PADDLE_Y_OFFSET = 30;
  34.  
  35.     /** Number of bricks per row */
  36.     private static final int NBRICKS_PER_ROW = 10;
  37.  
  38.     /** Number of rows of bricks */
  39.     private static final int NBRICK_ROWS = 10;
  40.  
  41.     /** Separation between bricks */
  42.     private static final int BRICK_SEP = 4;
  43.  
  44.     /** Width of a brick */
  45.     private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
  46.  
  47.     /** Height of a brick */
  48.     private static final int BRICK_HEIGHT = 8;
  49.  
  50.     /** Radius of the ball in pixels */
  51.     private static final int BALL_RADIUS = 10;
  52.  
  53.     /** Offset of the top brick row from the top */
  54.     private static final int BRICK_Y_OFFSET = 70;
  55.  
  56.     /** Number of turns */
  57.     private static int NTURNS = 4;
  58.  
  59.     private static int LifeCounter = NTURNS;
  60.  
  61.     /* Method: run() */
  62.     /** Runs the Breakout program. */
  63.     public void run() {
  64.         this.setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
  65.         setup();
  66.         play();
  67.  
  68.     }
  69.  
  70.     private void secondChallange() {
  71.         GPoint k = new GPoint(ball.getX(), ball.getY());
  72.         remove(ball);
  73.         ball = new GOval(BALL_RADIUS, BALL_RADIUS);
  74.         ball.setFilled(true);
  75.         add(ball, k);
  76.         secondChallangeb = true;
  77.  
  78.     }
  79.  
  80.     private void firstChallange() {
  81.         GPoint k = new GPoint(paddle.getX(), paddle.getY());
  82.         remove(paddle);
  83.         paddle = new GRect(PADDLE_WIDTH * 0.7, PADDLE_HEIGHT);
  84.         paddle.setFilled(true);
  85.         add(paddle, k);
  86.  
  87.     }
  88.  
  89.     private void setup() {
  90.         addMouseListeners();
  91.         drawBricks();
  92.         drawPaddle();
  93.     }
  94.  
  95.     private void play() {
  96.  
  97.         GLabel press = new GLabel("Click the mouse to start");
  98.         add(press, (WIDTH - press.getWidth()) / 1.9, (HEIGHT - press.getAscent()) / 1.5);
  99.         addBall();
  100.  
  101.         waitForClick();
  102.  
  103.         remove(press);
  104.         while (true) {
  105.  
  106.             moveBall();
  107.             checkForCollisions();
  108.  
  109.             if (NUMBEROFBRICKS == firstChallange) {
  110.                 firstChallange();
  111.             }
  112.             if (NUMBEROFBRICKS == secondChallange) {
  113.                 secondChallange();
  114.             }
  115.  
  116.             if (ball == null && LifeCounter != 0) {
  117.                 pause(100 * DELAY);
  118.                 addBall();
  119.             }
  120.             if (GameOver()) {
  121.                 if (NUMBEROFBRICKS == 0) {
  122.                     removeAll();
  123.                     GLabel winner = new GLabel("You Win");
  124.                     winner.setFont("SansSerif-bold-50");
  125.                     add(winner, (WIDTH - winner.getWidth()) / 2, (HEIGHT - winner.getAscent()) / 2);
  126.  
  127.                     GLabel res = new GLabel("Click the mouse to restart");
  128.                     add(res, (WIDTH - winner.getWidth()) / 1.3, (HEIGHT - winner.getAscent()) / 1.5);
  129.                 }
  130.                 if (LifeCounter == 0) {
  131.                     removeAll();
  132.                     GLabel loser = new GLabel("You Lost");
  133.                     loser.setFont("SansSerif-bold-50");
  134.                     add(loser, (WIDTH - loser.getWidth()) / 2, (HEIGHT - loser.getAscent()) / 2);
  135.  
  136.                     GLabel res = new GLabel("Click the mouse to restart");
  137.                     add(res, (WIDTH - loser.getWidth()) / 1.3, (HEIGHT - loser.getAscent()) / 1.5);
  138.  
  139.                 }
  140.                 waitForClick();
  141.                 restart();
  142.             }
  143.             pause(DELAY);
  144.         }
  145.  
  146.     }
  147.  
  148.     private void restart() {
  149.         removeAll();
  150.         LifeCounter = NTURNS;
  151.         BrickCounter = NUMBEROFBRICKS;
  152.         run();
  153.     }
  154.  
  155.     public void mousePressed(MouseEvent e) {
  156.         last = new GPoint(e.getPoint());
  157.     }
  158.  
  159.     public void mouseMoved(MouseEvent e) {
  160.         int x = Math.min(WIDTH - PADDLE_WIDTH, Math.max(0, e.getX() - PADDLE_WIDTH / 2));
  161.         paddle.setLocation(x, HEIGHT - PADDLE_Y_OFFSET);
  162.     }
  163.    
  164.     private void moveBall() {
  165.         ball.move(xMove, yMove);
  166.     }
  167.  
  168.     // Gravity
  169.     private void checkForCollisions() {
  170.         GObject UpBallLeft = getElementAt(ball.getX(), ball.getY());
  171.         GObject UpBallRight = getElementAt(ball.getX() + ball.getWidth(), ball.getY());
  172.         GObject DownBallLeft = getElementAt(ball.getX(), ball.getY() + ball.getHeight());
  173.         GObject DownBallRight = getElementAt(ball.getX() + ball.getWidth(), ball.getY() + ball.getHeight());
  174.         if (ball.getY() < 0) {
  175.             if (yMove < 0) {
  176.                 yMove = yMove * -1;
  177.             }
  178.         } else if (DownBallRight == paddle || DownBallLeft == paddle) {
  179.             if (yMove > 0) {
  180.                 yMove = yMove * -1;
  181.             }
  182.         } else if (ball.getX() + 2 * BALL_RADIUS > APPLICATION_WIDTH) {
  183.             if (xMove > 0) {
  184.                 xMove = xMove * -1;
  185.             }
  186.         } else if (ball.getX() < 0) {
  187.             if (xMove < 0) {
  188.                 xMove = xMove * -1;
  189.             }
  190.         } else if (ball.getY() + 2 * BALL_RADIUS > HEIGHT) {
  191.             remove(ball);
  192.             ball = null;
  193.             LifeCounter--;
  194.             println("Yeak");
  195.         }
  196.  
  197.         else if (UpBallLeft != null | UpBallRight != null) {
  198.             if (yMove < 0) {
  199.                 yMove = yMove * -1;
  200.             }
  201.  
  202.             if (UpBallLeft != null) {
  203.                 remove(UpBallLeft);
  204.                 UpBallLeft = null;
  205.                 NUMBEROFBRICKS--;
  206.             } else if (UpBallRight != null) {
  207.                 remove(UpBallRight);
  208.                 UpBallRight = null;
  209.                 NUMBEROFBRICKS--;
  210.             }
  211.         } else if (DownBallLeft != null | DownBallRight != null) {
  212.             if (yMove > 0) {
  213.                 yMove = yMove * -1;
  214.             }
  215.             if (DownBallLeft != null) {
  216.                 remove(DownBallLeft);
  217.                 DownBallLeft = null;
  218.                 NUMBEROFBRICKS--;
  219.             } else if (DownBallRight != null) {
  220.                 remove(DownBallRight);
  221.                 DownBallRight = null;
  222.                 NUMBEROFBRICKS--;
  223.             }
  224.         }
  225.  
  226.     }
  227.  
  228.     // Check if The Game is over
  229.     private boolean GameOver() {
  230.         if (LifeCounter == 0)
  231.             return true;
  232.         if (NUMBEROFBRICKS == 0)
  233.             return true;
  234.         return false;
  235.     }
  236.  
  237.     private void addBall() {
  238.         if (!secondChallangeb) {
  239.             ball = new GOval(APPLICATION_WIDTH / 2 - BALL_RADIUS / 2, APPLICATION_HEIGHT / 2 - BALL_RADIUS / 2,
  240.                     2 * BALL_RADIUS, 2 * BALL_RADIUS);
  241.         } else
  242.             ball = new GOval(APPLICATION_WIDTH / 2 - BALL_RADIUS / 2, APPLICATION_HEIGHT / 2 - BALL_RADIUS / 2,
  243.                     BALL_RADIUS, BALL_RADIUS);
  244.         ball.setFilled(true);
  245.         add(ball);
  246.     }
  247.        
  248.     private void drawPaddle() {
  249.         paddle = new GRect(APPLICATION_WIDTH / 2 - PADDLE_WIDTH / 2, HEIGHT - PADDLE_Y_OFFSET, PADDLE_WIDTH,
  250.                 PADDLE_HEIGHT);
  251.         paddle.setFilled(true);
  252.         add(paddle);
  253.  
  254.     }
  255.  
  256.     private void drawBricks() {
  257.         for (int row = 0; row < NBRICK_ROWS; row++) {
  258.             for (int brick = 0; brick < NBRICKS_PER_ROW; brick++) {
  259.                 GRect rect = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
  260.                 rect.setFilled(true);
  261.                 if (row == 0 || row == 1) {
  262.                     rect.setFillColor(Color.RED);
  263.                     rect.setColor(Color.RED);
  264.                 } else if (row == 2 || row == 3) {
  265.                     rect.setFillColor(Color.ORANGE);
  266.                     rect.setColor(Color.ORANGE);
  267.                 } else if (row == 4 || row == 5) {
  268.                     rect.setFillColor(Color.YELLOW);
  269.                     rect.setColor(Color.YELLOW);
  270.                 } else if (row == 6 || row == 7) {
  271.                     rect.setFillColor(Color.GREEN);
  272.                     rect.setColor(Color.GREEN);
  273.                 } else if (row == 8 || row == 9) {
  274.                     rect.setFillColor(Color.CYAN);
  275.                     rect.setColor(Color.CYAN);
  276.                 }
  277.  
  278.                 add(rect, brick * (BRICK_WIDTH + BRICK_SEP), row * (BRICK_HEIGHT + BRICK_SEP) + BRICK_Y_OFFSET);
  279.             }
  280.         }
  281.     }
  282.  
  283.     private static double yMove = 2;
  284.     private static double xMove = yMove * Math.cos(1.03);
  285.     private static int NUMBEROFBRICKS = NBRICK_ROWS * NBRICKS_PER_ROW;
  286.  
  287.     private static int BrickCounter = NUMBEROFBRICKS;
  288.  
  289.     private GOval ball;
  290.     private GRect paddle;
  291.  
  292.     private GPoint last;
  293.     private int DELAY = 10;
  294.  
  295.     private int firstChallange = NBRICK_ROWS * NBRICKS_PER_ROW - 2;
  296.     private int secondChallange = NBRICK_ROWS * NBRICKS_PER_ROW - 5;
  297.  
  298.     private boolean secondChallangeb = false;
  299.  
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement