Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hsa2.GraphicsConsole;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Rectangle;
- public class BlockBreaker {
- public static void main(String[] args) {
- new BlockBreaker();
- }
- /***** Constants *****/
- static final int SLEEPTIME = 5;
- static final int WINW = 1000;
- static final int WINH = 600;
- static final Color PADDLECOLOUR = Color.YELLOW;
- static final int NUMBLOCKS = 40; //set the number of blocks here
- /***** Global (instance) Variables ******/
- private GraphicsConsole gc = new GraphicsConsole (WINW, WINH);
- private Ball ball = new Ball();
- private Rectangle paddle = new Rectangle(0,0,100,16); //set width and height here
- private Block[] blocks = new Block[NUMBLOCKS]; //this just makes the array, it doesn't create the blocks!
- private int lives;
- //private boolean isPlaying = true;
- /****** Constructor ********/
- private BlockBreaker() {
- initialize();
- boolean win = playGame();
- //Ending the game
- //if (lives > 0)
- if (win)
- gc.drawString("GAME OVER, You win!", 40, 22);
- else
- gc.drawString("GAME OVER, You lost.", 40, 22);
- gc.sleep(3000);
- gc.close(); //close the window and end everything
- }
- /****** Methods for game *******/
- private void initialize() {
- //set up gc
- gc.setFont(new Font("Georgia", Font.PLAIN, 20));
- gc.setTitle("Brick breaker game");
- gc.setAntiAlias(true);
- gc.setLocationRelativeTo(null);
- gc.setBackgroundColor(Color.BLACK);
- gc.enableMouse();
- gc.enableMouseMotion(); //only needed for mouse (obviously)
- gc.clear();
- //set up variables
- lives = 4;
- //isPlaying = true;
- //set up objects
- paddle.x = WINW/2;
- paddle.y = WINH - 100;
- ball.putOnPaddle(paddle);
- makeBlocks();
- gc.sleep(500); // allow a bit of time for the user to move the mouse to the correct position in the game screen
- }
- void makeBlocks() {
- int cols = 8; // this should probably somehow be based on WINW / block.width
- int topMargin = 30;
- int leftMargin = 30;
- int spacing = 30;
- for (int i=0; i < NUMBLOCKS; i++) { //instead of NUMBLOCKS I could use blocks.length
- int x = (Block.w + spacing) * (i%cols) + topMargin;
- int y = (Block.h +4) * (i/cols) + leftMargin;
- blocks[i] = new Block(x,y);
- }
- //set block types
- //blocks[4].type = Block.UNBREAK;
- blocks[4].setType(Block.UNBREAK);
- blocks[9].setType(Block.BIGPADDLE);
- //... you could also set 5 random fast balls, 5 random unbreakables ...
- }
- /**
- * This contains the main game loop.
- * Two things about this:
- * 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()
- * 2. it returns TRUE if you win or FALSE if you lose or quit.
- */
- boolean playGame() {
- //MAIN GAME LOOP
- while (true ) {
- if (gc.getMouseClick() > 0 || gc.getKeyCode() == ' ') ball.onPaddle = false; //start game by clicking or pressing space
- /*** pick one of the following two lines ***/
- movePaddle_mouse();
- //movePaddle_keys();
- moveBall();
- drawGraphics();
- gc.sleep(SLEEPTIME);
- // if (lives <= 0) isPlaying = false;
- if (lives <= 0) return false;
- //check if all of the blocks invisible.
- /*
- boolean win = true;
- for (int i=0; i < NUMBLOCKS; i++) {
- if (blocks[i].isVisible) win = false;
- }
- */
- //check if all of the blocks are off the screen
- boolean win = true;
- for (int i=0; i < NUMBLOCKS; i++) {
- if (blocks[i].y > 0) win = false;
- }
- //if (win) isPlaying = false;
- if (win) return true;
- if (gc.getKeyCode() == 'Q') return false; //press q to quit
- }
- }
- /**
- * This method moves the ball and handles all collisions where the ball hits something.
- * Don't make a separate method to see if the paddle hits the ball.
- */
- private void moveBall() {
- if (ball.onPaddle) return;
- ball.x += ball.xspeed;
- ball.y += ball.yspeed;
- //hit bottom of screen
- if ((ball.y + ball.height) > gc.getDrawHeight()) { //or WINW - which is probably slightly faster
- ball.putOnPaddle(paddle);
- lives--;
- ball.colour = new Color(Color.HSBtoRGB((float)Math.random(), 1.0f, 1.0f));
- }
- //right side of screen
- if ((ball.x + ball.width) > gc.getDrawWidth()) {
- ball.xspeed *=-1;
- }
- //top of screen
- if (ball.y < 0) {
- ball.yspeed *=-1;
- ball.yspeed++; //speed up the ball each time it hits the top
- }
- //left side of screen
- if (ball.x < 0) {
- ball.xspeed *=-1;
- }
- //check if ball hits paddle
- if (ball.intersects(paddle)) {
- if (ball.yspeed > 0 ) { //the ball must be moving downwards, not upwards
- ball.yspeed *=-1;
- }
- }
- //see if ball hits block
- for (int i=0; i < blocks.length; i++) {
- if (ball.intersects(blocks[i])) {
- //blocks[i].isVisible = false; //don't bother drawing it on the screen
- blocks[i].y = -100; //move it off the screen
- ball.yspeed *= -1;
- }
- }
- }
- private void movePaddle_mouse(){
- paddle.x = gc.getMouseX() - paddle.width/2;
- if (ball.onPaddle) ball.x = paddle.x + paddle.width/2 - ball.width/2;
- }
- private void movePaddle_keys(){
- int moveAmount = 7;
- //37 and 39 are the keyboard codes for the left and right arrow keys.
- if (gc.getKeyCode() == 37) paddle.x -= moveAmount;
- if (gc.getKeyCode() == 39) paddle.x += moveAmount;
- if (ball.onPaddle) ball.x = paddle.x + paddle.width/2 - ball.width/2;
- //check to prevent moving the paddle off the screen
- if (paddle.x < 0) paddle.x = 0;
- if (paddle.x +paddle.width > WINW) paddle.x = WINW - paddle.width;
- }
- private void drawGraphics() {
- synchronized(gc) {
- gc.clear();
- gc.setColor(Color.WHITE);
- gc.drawString("LIVES = " + lives, 40, 22);
- gc.setColor(ball.colour);
- gc.fillOval(ball.x, ball.y, ball.width, ball.height);
- //draw the blocks
- for( Block b: blocks) {
- //if (b.isVisible) {
- gc.setColor(b.colour);
- gc.fillRect(b.x, b.y, b.width, b.height);
- gc.setColor(Color.WHITE);
- gc.drawRect(b.x, b.y, b.width, b.height);
- //}
- }
- gc.setColor(PADDLECOLOUR);
- gc.fillRoundRect(paddle.x, paddle.y, paddle.width, paddle.height, 10,10);
- }
- }
- }
Add Comment
Please, Sign In to add comment