Advertisement
Sampywise

Breakout

Jul 10th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 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.     /** Width and height of application window in pixels */
  20.     public static final int APPLICATION_WIDTH = 400;
  21.     public static final int APPLICATION_HEIGHT = 600;
  22.  
  23.     /** Dimensions of the paddle */
  24.     private static final int PADDLE_WIDTH = 60;
  25.     private static final int PADDLE_HEIGHT = 10;
  26.  
  27.     /** Offset of the paddle up from the bottom */
  28.     private static final int PADDLE_Y_OFFSET = 30;
  29.  
  30.     /** Number of bricks per row */
  31.     private static final int NBRICKS_PER_ROW = 10;
  32.  
  33.     /** Number of rows of bricks */
  34.     private static final int NBRICK_ROWS = 10;
  35.  
  36.     /** Separation between bricks */
  37.     private static final int BRICK_SEP = 4;
  38.  
  39.     private static final int NUMBER_BRICKS = NBRICKS_PER_ROW*NBRICK_ROWS ;
  40.     /** Width of a brick */
  41.     private static final int BRICK_WIDTH =
  42.         (APPLICATION_WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
  43.  
  44.     /** Height of a brick */
  45.     private static final int BRICK_HEIGHT = 8;
  46.  
  47.     /** Radius of the ball in pixels */
  48.     private static final int BALL_RADIUS = 10;
  49.  
  50.     /** Offset of the top brick row from the top */
  51.     private static final int BRICK_Y_OFFSET = 70;
  52.  
  53.     /** Number of turns */
  54.     private static final int NTURNS = 3;
  55.  
  56.     /** Initial Y velocity */
  57.     private static final double Y_VELOCITY = 3.0;
  58.    
  59.     /** Delay between ball updates */
  60.     private static final int ANIMATION_PAUSE = 10;
  61.    
  62.     //Declaration of all the instance variables
  63.     private GRect paddle;
  64.     private GOval ball;
  65.     private GLabel messageLabel;
  66.     private int numBricksLeft = NUMBER_BRICKS;
  67.     private RandomGenerator rgen = RandomGenerator.getInstance();
  68.     AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
  69.  
  70.     public static void main(String[] args) {
  71.         new Breakout().start(args);
  72.     }
  73.     public void run() {
  74.         addMouseListeners();
  75.         setUp();
  76.         for(int i = 0; i < NTURNS; i++) {
  77.             setUpTurn(i);
  78.             playTurn();
  79.             if(numBricksLeft == 0) break;
  80.         }
  81.        
  82.         if(numBricksLeft == 0) {
  83.             addMessage("You Win");
  84.         } else {
  85.             addMessage("You Lose");
  86.         }
  87.     }
  88.    
  89.     private void setUpTurn(int turnNumber) {
  90.         removeBall();
  91.         addMessage("Turns left: " + (NTURNS - turnNumber));
  92.         waitForClick();
  93.         removeMessage();
  94.         addBall();
  95.     }
  96.        
  97.     private void setUp() {
  98.         addBricks();
  99.         addPaddle();
  100.        
  101.     }
  102.     /* Method: run() */
  103.     /** Runs the Breakout program. */
  104.    
  105.     private void addMessage(String message) {
  106.         messageLabel = new GLabel(message);
  107.         messageLabel.setFont("Monospace-32");
  108.         messageLabel.setLocation(getWidth()/2 - messageLabel.getWidth()/2, getHeight()/2 - messageLabel.getHeight()/2);
  109.         add(messageLabel);
  110.     }
  111.    
  112.     private void removeMessage() {
  113.         if(messageLabel != null) {
  114.             remove(messageLabel);
  115.         }
  116.     }
  117.    
  118.     public void addBricks() {
  119.         for (int i = 0; i < NBRICK_ROWS; i++) {
  120.             Color color = getColorForRow(i);
  121.             addRow(BRICK_SEP/2, BRICK_Y_OFFSET + i*(BRICK_HEIGHT + BRICK_SEP), color);
  122.         }
  123.     }
  124.    
  125.     private Color getColorForRow(int rowIndex) {
  126.         Color color;
  127.         switch(rowIndex) {
  128.         case 0: case 1: color = Color.RED; break;
  129.         case 2: case 3: color = Color.ORANGE; break;
  130.         case 4: case 5: color =Color.YELLOW; break;
  131.         case 6:case 7: color= Color.green; break;
  132.         case 8: case 9: color = Color.blue; break;
  133.         default: color = Color.BLACK; break;
  134.         }
  135.         return color;
  136.     }
  137.    
  138.     private void addRow(int rowX, int rowY, Color color) {
  139.         for(int i = 0; i < NBRICKS_PER_ROW; i++) {
  140.             int brickX = rowX = (i * BRICK_SEP) + (i * BRICK_WIDTH);
  141.             GRect rect = new GRect (brickX, rowY, BRICK_WIDTH, BRICK_HEIGHT);
  142.             rect.setFilled(true);
  143.             rect.setColor(color);
  144.             rect.setFillColor(color);
  145.             add(rect);
  146.         }
  147.     }
  148.    
  149.     private void addPaddle() {
  150.         paddle = new GRect(getWidth()/2-PADDLE_WIDTH/2,getHeight()-PADDLE_Y_OFFSET,
  151.                 PADDLE_WIDTH, PADDLE_HEIGHT);
  152.         paddle.setFilled(true);
  153.         paddle.setFillColor(Color.BLACK);
  154.         add(paddle);
  155.     }
  156.    
  157.     public void mouseMoved(MouseEvent me) {
  158.         int x = me.getX();
  159.         if(paddle != null) {
  160.             if(x > PADDLE_WIDTH/2 && x< getWidth()-PADDLE_WIDTH/2)
  161.                 paddle.setLocation(x - PADDLE_WIDTH/2, getHeight() - PADDLE_Y_OFFSET);
  162.         }
  163.     }
  164.    
  165.  
  166.    
  167.     private void addBall() {   
  168.         ball = new GOval(getWidth()/2 - BALL_RADIUS, getHeight()/2 - BALL_RADIUS, 2*BALL_RADIUS, 2*BALL_RADIUS);
  169.         ball.setFilled(true);
  170.         ball.setFillColor(Color.BLACK);
  171.         add(ball);
  172.        
  173.     }
  174.    
  175.     private void removeBall() {
  176.         if(ball != null) {
  177.             remove(ball);
  178.         }
  179.     }
  180.  
  181.     private void playTurn() {
  182.         double vx = rgen.nextDouble(1.0, 3.0);
  183.         if(rgen.nextBoolean(.5)) vx =- vx;
  184.         double vy = Y_VELOCITY;
  185.         while(true) {
  186.             if(numBricksLeft == 0)
  187.                 break;
  188.             if(ball.getY() > APPLICATION_HEIGHT)
  189.                 break;
  190.             ball.move(vx,vy);
  191.             pause(ANIMATION_PAUSE);
  192.            
  193.             vy = reflectOffColliders(vy);
  194.             vx = reflectOffSideWalls(vx);
  195.             vy = reflectOffTopWall(vy);
  196.        
  197.         }
  198.     }
  199.    
  200.     private double reflectOffColliders(double vy) {
  201.         GObject collider = getCollidingObject();
  202.         if(collider == paddle) {
  203.             bounceClip.play();
  204.             vy = -Math.abs(vy);
  205.         }
  206.         else if (collider != null) {
  207.             remove(collider);
  208.             vy *= -1;
  209.         }
  210.         return vy;
  211.     }
  212.  
  213.     private double reflectOffTopWall(double vy) {
  214.         if (ball.getY() < 0) {
  215.             vy *= -1;
  216.         }
  217.         return vy;
  218.     }
  219.    
  220.     private double reflectOffSideWalls(double vx) {
  221.         if (ball.getX() < 0 || ball.getX() > APPLICATION_WIDTH - ball.getWidth()) {
  222.             vx *= -1;
  223.         }
  224.         return vx;
  225.     }
  226.    
  227.     private GObject getCollidingObject() {
  228.         //Finds collider, if the ball collided on the top left corner
  229.         if (getElementAt(ball.getX(), ball.getY()) != null) {
  230.             return getElementAt(ball.getX(), ball.getY()) ;
  231.         }
  232.         //Finds collider, if the ball collided on the top right corner
  233.         else if (getElementAt(ball.getX()+2*BALL_RADIUS, ball.getY()) != null){
  234.             return getElementAt(ball.getX()+2*BALL_RADIUS, ball.getY()) ;
  235.         }
  236.         //Finds collider, if the ball collided on the bottom left corner
  237.         else if (getElementAt(ball.getX(), ball.getY() + 2*BALL_RADIUS) != null) {
  238.             return getElementAt(ball.getX(), ball.getY() + 2* BALL_RADIUS) ;
  239.         }
  240.         //Finds collider, if the ball collided on the bottom right corner
  241.         else if (getElementAt(ball.getX()+ 2*BALL_RADIUS, ball.getY() + 2*BALL_RADIUS) != null) {
  242.             return getElementAt(ball.getX()+ 2*BALL_RADIUS, ball.getY() + 2*BALL_RADIUS);
  243.         }
  244.         return null;
  245.     }
  246.    
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement