boxglue

BrickBreaker game

Aug 10th, 2020 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1. import hsa2.GraphicsConsole;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Rectangle;
  5.  
  6. public class BlockBreaker {
  7.  
  8.     public static void main(String[] args) {
  9.         new BlockBreaker();
  10.     }
  11.  
  12.     /***** Constants *****/
  13.     static final int SLEEPTIME = 5;
  14.     static final int WINW = 1000;
  15.     static final int WINH = 600;
  16.     static final Color PADDLECOLOUR = Color.YELLOW;
  17.     static final int NUMBLOCKS = 40;            //set the number of blocks here
  18.  
  19.     /***** Global (instance) Variables ******/
  20.     private GraphicsConsole gc = new GraphicsConsole (WINW, WINH);
  21.     private Ball ball = new Ball();
  22.     private Rectangle paddle = new Rectangle(0,0,100,16);   //set width and height here
  23.     private Block[] blocks = new Block[NUMBLOCKS];      //this just makes the array, it doesn't create the blocks!
  24.     private int lives;
  25.     //private boolean isPlaying = true;
  26.  
  27.     /****** Constructor ********/
  28.     private BlockBreaker() {
  29.         initialize();
  30.  
  31.         boolean win = playGame();
  32.  
  33.         //Ending the game      
  34.         //if (lives > 0)
  35.         if (win)
  36.             gc.drawString("GAME OVER, You win!", 40, 22);
  37.         else
  38.             gc.drawString("GAME OVER, You lost.", 40, 22);
  39.  
  40.         gc.sleep(3000);
  41.         gc.close(); //close the window and end everything
  42.  
  43.     }
  44.  
  45.     /****** Methods for game *******/
  46.  
  47.     private void initialize() {
  48.         //set up gc
  49.         gc.setFont(new Font("Georgia", Font.PLAIN, 20));
  50.         gc.setTitle("Brick breaker game");
  51.         gc.setAntiAlias(true);
  52.         gc.setLocationRelativeTo(null);
  53.         gc.setBackgroundColor(Color.BLACK);
  54.         gc.enableMouse();
  55.         gc.enableMouseMotion(); //only needed for mouse (obviously)
  56.         gc.clear();
  57.  
  58.         //set up variables
  59.         lives = 4;
  60.         //isPlaying = true;
  61.  
  62.         //set up objects       
  63.         paddle.x = WINW/2;
  64.         paddle.y = WINH - 100;
  65.         ball.putOnPaddle(paddle);
  66.  
  67.         makeBlocks();
  68.  
  69.         gc.sleep(500); // allow a bit of time for the user to move the mouse to the correct position in the game screen
  70.     }
  71.  
  72.     void makeBlocks() {
  73.        
  74.         int cols = 8; // this should probably somehow be based on WINW / block.width
  75.         int topMargin = 30;
  76.         int leftMargin = 30;
  77.         int spacing = 30;
  78.         for (int i=0; i < NUMBLOCKS; i++) {     //instead of NUMBLOCKS I could use blocks.length                       
  79.             int x = (Block.w + spacing) * (i%cols) + topMargin;
  80.             int y = (Block.h +4) * (i/cols) + leftMargin;
  81.             blocks[i] = new Block(x,y);
  82.         }
  83.         //set block types
  84.         //blocks[4].type = Block.UNBREAK;
  85.         blocks[4].setType(Block.UNBREAK);
  86.         blocks[9].setType(Block.BIGPADDLE);
  87.         //...  you could also set 5 random fast balls, 5 random unbreakables ...
  88.     }
  89.  
  90.     /**
  91.      * This contains the main game loop.
  92.      * Two things about this:
  93.      * 1. taking it out of the constructor, makes it a lot easier to have a "play again?" option - you just run initialize() and then playGame()
  94.      * 2. it returns TRUE if you win or FALSE if you lose or quit.
  95.      */
  96.     boolean playGame() {
  97.         //MAIN GAME LOOP
  98.         while (true ) {    
  99.  
  100.             if (gc.getMouseClick() > 0 || gc.getKeyCode() == ' ') ball.onPaddle = false;    //start game by clicking or pressing space
  101.  
  102.             /*** pick one of the following two lines ***/
  103.             movePaddle_mouse();
  104.             //movePaddle_keys();
  105.  
  106.             moveBall();
  107.  
  108.             drawGraphics();
  109.  
  110.             gc.sleep(SLEEPTIME);
  111.  
  112.             //          if (lives <= 0) isPlaying = false;
  113.             if (lives <= 0) return false;
  114.  
  115.             //check if all of the blocks invisible.
  116.             /*
  117.             boolean win = true;
  118.             for (int i=0; i < NUMBLOCKS; i++) {
  119.                 if (blocks[i].isVisible) win = false;  
  120.             }
  121.             */
  122.            
  123.             //check if all of the blocks are off the screen
  124.             boolean win = true;
  125.             for (int i=0; i < NUMBLOCKS; i++) {
  126.                 if (blocks[i].y > 0) win = false;  
  127.             }
  128.             //if (win) isPlaying = false;
  129.             if (win) return true;
  130.  
  131.             if (gc.getKeyCode() == 'Q') return false;       //press q to quit
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * This method moves the ball and handles all collisions where the ball hits something.
  137.      * Don't make a separate method to see if the paddle hits the ball.
  138.      */
  139.     private void moveBall() {
  140.         if (ball.onPaddle) return;
  141.  
  142.         ball.x += ball.xspeed;
  143.         ball.y += ball.yspeed;
  144.  
  145.         //hit bottom of screen     
  146.         if ((ball.y + ball.height) > gc.getDrawHeight()) {  //or WINW - which is probably slightly faster
  147.             ball.putOnPaddle(paddle);
  148.             lives--;
  149.             ball.colour = new Color(Color.HSBtoRGB((float)Math.random(), 1.0f, 1.0f));
  150.         }
  151.  
  152.         //right side of screen
  153.         if ((ball.x + ball.width) > gc.getDrawWidth()) {
  154.             ball.xspeed *=-1;
  155.         }
  156.         //top of screen
  157.         if (ball.y < 0) {
  158.             ball.yspeed *=-1;
  159.             ball.yspeed++; //speed up the ball each time it hits the top
  160.  
  161.         }
  162.         //left side of screen
  163.         if (ball.x < 0) {
  164.             ball.xspeed *=-1;
  165.         }
  166.  
  167.         //check if ball hits paddle
  168.         if (ball.intersects(paddle)) {
  169.             if (ball.yspeed > 0 ) {         //the ball must be moving downwards, not upwards
  170.                 ball.yspeed *=-1;
  171.             }
  172.         }  
  173.  
  174.         //see if ball hits block
  175.         for (int i=0; i < blocks.length; i++) {
  176.             if (ball.intersects(blocks[i])) {
  177.                 //blocks[i].isVisible = false;  //don't bother drawing it on the screen
  178.                 blocks[i].y = -100;         //move it off the screen
  179.                 ball.yspeed *= -1;
  180.             }
  181.         }
  182.  
  183.  
  184.     }  
  185.  
  186.     private void movePaddle_mouse(){
  187.         paddle.x = gc.getMouseX() - paddle.width/2;
  188.         if (ball.onPaddle) ball.x = paddle.x + paddle.width/2 - ball.width/2;
  189.     }
  190.  
  191.     private void movePaddle_keys(){
  192.         int moveAmount = 7;
  193.         //37 and 39 are the keyboard codes for the left and right arrow keys.
  194.         if (gc.getKeyCode() == 37) paddle.x -= moveAmount;
  195.         if (gc.getKeyCode() == 39) paddle.x += moveAmount;
  196.  
  197.         if (ball.onPaddle) ball.x = paddle.x + paddle.width/2 - ball.width/2;
  198.  
  199.         //check to prevent moving the paddle off the screen
  200.         if (paddle.x < 0) paddle.x = 0;
  201.         if (paddle.x +paddle.width > WINW) paddle.x = WINW - paddle.width;
  202.  
  203.     }
  204.  
  205.     private void drawGraphics() {
  206.  
  207.         synchronized(gc) {
  208.             gc.clear();
  209.  
  210.             gc.setColor(Color.WHITE);
  211.             gc.drawString("LIVES = " + lives, 40, 22);
  212.             gc.setColor(ball.colour);
  213.             gc.fillOval(ball.x, ball.y, ball.width, ball.height);
  214.  
  215.             //draw the blocks
  216.             for( Block b: blocks) {
  217.                 //if (b.isVisible) {
  218.                     gc.setColor(b.colour);
  219.                     gc.fillRect(b.x, b.y, b.width, b.height);
  220.                     gc.setColor(Color.WHITE);
  221.                     gc.drawRect(b.x, b.y, b.width, b.height);
  222.                 //}
  223.             }
  224.  
  225.             gc.setColor(PADDLECOLOUR);
  226.             gc.fillRoundRect(paddle.x, paddle.y, paddle.width, paddle.height, 10,10);
  227.  
  228.         }
  229.     }
  230. }
Add Comment
Please, Sign In to add comment