This is a ridiculous game that I was able to build just by following Stanford’s CS106a. This is almost a complete game. The main part is done .It just needs a little polishing. I am moving on to the next assignment for now.
/* * 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 final int NTURNS = 3; /** copied from BouncingBall.java **/ private static final double INITIAL_SPEED = 3.0; /** pause time */ private static final double PAUSE_TIME = 1000 /24; /** middle space height*/ private static final double MID_SPACE = (HEIGHT +BRICK_Y_OFFSET + (NBRICK_ROWS*BRICK_HEIGHT))/2; //private static final int NBRICKS_ROWS = 0; /* Method: run() */ /** Runs the Breakout program. */ public void run() { brick_count = NBRICKS_PER_ROW*NBRICK_ROWS; // number of bricks setup(); //setup the game method call int turns =NTURNS; // how many lives we have while(turns>0){ moveBall(); //gameover(); if(ballHitsBottom(ball) || brick_count ==0){ gameover(); break; } if(turns != NTURNS){ setup(); } turns--; } gameover(); } // setup methods private void setup(){ setup_bricks(); // setup the bricks setup_paddle(); // paddle setup setup_ball(); // ball setup } // gameover if the ball hits bottom or there is no more bricks private void gameover(){ //return (ballHitsBottom() || noMoreBricks()); GLabel gameover = new GLabel("GAME OVER"); gameover.setColor(Color.RED); add(gameover,(WIDTH-gameover.getWidth())/2,(MID_SPACE - gameover.getAscent())/2); //return gameover; } private boolean ballHitsBottom(GOval ball){ return ball.getY() >= HEIGHT - ball.getHeight() ; } // create a brick private void createbrick(Color color,double x, double y){ brick = new GRect(BRICK_WIDTH,BRICK_HEIGHT); // brick is a new GRect object brick.setFilled(true); // it is filled brick.setColor(Color.WHITE); // the color is white brick.setFillColor(color); // set the fill color with color parameter add(brick,x,y); // add the brick to the x , y parameter location } // setup the bricks private void setup_bricks(){ int init_x = 0; // initial x , the left margin of the window for (int i = 1; i <= NBRICKS_PER_ROW;i++) { // loop through the bricks per row for (int j = 0;j < NBRICK_ROWS;j++){ // loop through the no. of rows // compute the x position which starts at 0 then add a brick then a brick separator int x = init_x+(j * BRICK_WIDTH)+ (j*BRICK_SEP); //compute the y by adding offset and a brick plus separator int y = BRICK_Y_OFFSET+(i * BRICK_HEIGHT)+(i*BRICK_SEP); // colorize base on the row index i if (i==1 || i==2){ createbrick(Color.RED,x,y); } else if (i==3 || i==4){ createbrick(Color.ORANGE,x,y); } else if (i==5 || i==6){ createbrick(Color.YELLOW,x,y); } else if (i==7 || i==8){ createbrick(Color.GREEN,x,y); } else if (i==9 || i==10){ createbrick(Color.CYAN,x,y); } } } } // setup the paddle private void setup_paddle(){ // put paddle in the middle paddle = new GRect(getWidth()/2-PADDLE_WIDTH/2,getHeight()-PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT); paddle.setFilled(true); paddle.setFillColor(Color.BLUE); paddle.setColor(Color.BLUE); add(paddle); addMouseListeners(); //adds mouse listeners } //track the paddle public void mouseMoved(MouseEvent e){ paddle.setLocation(e.getX(), getHeight()-PADDLE_Y_OFFSET); if ((e.getX()+PADDLE_WIDTH)>getWidth()){ paddle.setLocation(getWidth()-PADDLE_WIDTH, getHeight()-PADDLE_Y_OFFSET); } } // setup the ball in the middle of the screen private void setup_ball(){ double startX = (WIDTH - BALL_RADIUS)/2; double startY = (HEIGHT +BRICK_Y_OFFSET +(BRICK_HEIGHT*NBRICK_ROWS))/2; ball = new GOval(BALL_RADIUS, BALL_RADIUS); ball.setFilled(true); ball.setFillColor(Color.BLACK); add(ball,startX,startY); } /* * move the ball */ private void moveBall(){ double low_vel = 1.0; // low velocity of the ball double high_vel = 3.0; // high velocity of the ball double chance = 0.5; // randomize the chance by half vx = rgen.nextDouble(low_vel, high_vel); // random x velocity if (rgen.nextBoolean(chance)){ // half the chance of ball going left or right vx = -vx; // change to the opposite direction } vy = INITIAL_SPEED; //set the initial speed waitForClick(); //wait for a click to start the game //while(isBallOnScreen(ball)){ //keep moving till ball disappears while(!ballHitsBottom(ball) || brick_count!=0){ ball.move(vx, vy); // move by setting vx,vy checkForCollisions(); //check if ball hits something pause(PAUSE_TIME); //pause animation } } /** * isBallOnScreen * check for the ball location * @param ball * @return */ private boolean isBallOnScreen(GOval ball){ return ball.getX() <= WIDTH-(BALL_RADIUS*2) || ball.getX() >= 0 || ball.getY() <=HEIGHT || ball.getY() >=0; } private void checkForCollisions(){ collider = getCollidingObject(); //get the colliding object if(collider != null){ bounceClip.play(); if (collider == paddle){ // if its a paddle go the opposite direction /*if(collider.){ vy = -vy; vx = -vx; } else {*/ vy = -vy; //} } else { // if its a brick go the opposite ,remove the brick and subtract brick count vy = -vy; // bounce remove(collider); brick_count--; } } // top if (ball.getY() <= 0){ vy = -vy; } // if ball hits right side if (ball.getX() >= WIDTH-(BALL_RADIUS) ){ vx = -vx; } // if ball hits top // if ball hits left side if (ball.getX() <= 0 ){ vx = -vx; } } /** * getcollidingobject * gets the colliding object by testing all four bounding corners of the ball which is just a GRect * @return */ private GObject getCollidingObject(){ if (getElementAt(ball.getX(), ball.getY()) != null){ return (getElementAt(ball.getX(), ball.getY())); } if (getElementAt(ball.getX()+2*(BALL_RADIUS), ball.getY()) != null){ return (getElementAt(ball.getX()+2*(BALL_RADIUS), ball.getY())); } if (getElementAt(ball.getX(),ball.getY()+2*(BALL_RADIUS)) !=null ){ return (getElementAt(ball.getX(),ball.getY()+2*(BALL_RADIUS))); } if (getElementAt(ball.getX()+2*(BALL_RADIUS),ball.getY()+2*(BALL_RADIUS)) != null){ return (getElementAt(ball.getX()+2*(BALL_RADIUS),ball.getY()+2*(BALL_RADIUS))); } return null; } /* Private instance variables */ private int brick_count; // count the number of bricks private GRect brick; // the brick object private GObject collider; // the object the ball collided private GRect paddle; // the paddle private GOval ball; // the ball private double vx, vy; // the velocity in x and y private RandomGenerator rgen = RandomGenerator.getInstance(); // random generator AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au"); }