BreakOut Applet

By mountaindew on Apr 15, 2009

This is a BreakOut game made in Java. You start off by hitting "Play Game" and then click to release the ball. The more bricks you hit, the more points you get.

Screens:

You can play it right here as long as you have the java plugin installed.

Note: There are two files; brick.java (custom class that defines the properties for each brick) and BreakOut.java (the whole gameplay).

/*
 * brick.java
 * Properties for each brick
 *
 * BreakOut
 * Coded by MountainDew
 */

class brick {
    boolean visible;
    int x,y,width,height;
}

//******************** NEW FILE ********************\\

/**
 * BreakOut.java
 *
 * BreakOut
 * Coded by MountainDew
 */

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class BreakOut extends Applet implements MouseMotionListener, MouseListener {

    brick block[] = new brick[23];
    int px,py,pheight,pwidth,bx,by,bheight,bwidth,height,width,mx,my,speedx,speedy,score,balls,bricks;
    boolean started,title,hovered;
    private Timer timer1,timer2;

    public void init() {
        title = true;
        this.setSize(800,500);
        this.setBackground(Color.black);
        addMouseMotionListener(this);
        addMouseListener(this);
        height = getSize().height;
        width = getSize().width;
        pheight = 15;
        pwidth = 100;
        px = width / 2 - pwidth / 2;
        py = height - 50;
        bwidth = 20;
        bheight = 20;
        resetBall();
        balls = 3;
        timer1 = new Timer(16,new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bx += speedx;
                by += speedy;
                if (bx + bwidth >= width || bx <= 0) {
                    speedx = -speedx;
                }
                if (by + bheight >= py && bx + bwidth >= px && bx <= px + pwidth && by <= py || by <= 0) {
                    speedy = -speedy;
                }
                for (int i = 0; i < block.length; i++) {
                    if (block[i].visible && by <= block[i].y + block[i].height && bx + bwidth >= block[i].x && bx <= block[i].x + block[i].width && by + bheight >= block[i].y) {
                        speedy = -speedy;
                        block[i].visible = false;
                        score += 10;
                        ++bricks;
                        if (bricks == 23) {
                            bx = px + pwidth / 2 - bwidth / 2;
                            by = py - bheight;
                            bricks = 0;
                            speedx = 10;
                            speedy = -10;
                            drawBricks();
                            timer1.stop();
                            started = false;
                        }
                    }
                }
                if (by >= height) {
                    --balls;
                    if (balls > 0) {
                        resetBall();
                    }
                    timer1.stop();
                    started = false;
                    repaint();
                }
                repaint();
            }
        });
        timer2 = new Timer(2000,new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                title = true;
                timer2.stop();
            }
        });
    }

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.drawRect(0,0,width - 1,height - 1);
        if (title) {
            Font font1 = new Font("Arial", Font.BOLD, 70);
            g.setFont(font1);
            g.setColor(Color.green);
            g.drawString("BreakOut",width / 2 - 156,200);
            Font font2 = new Font("Arial", Font.BOLD, 25);
            g.setFont(font2);
            if (hovered) {
                g.setColor(Color.red);
            }
            g.drawString("Play Game",width / 2 - 61, 250);
        }
        else {
            for (int i = 0; i < block.length; i++) {
                if (block[i].visible) {
                    g.setColor(Color.red);
                    g.drawRect(block[i].x - 1,block[i].y - 1,block[i].width + 1,block[i].height + 1);
                    g.setColor(Color.green);
                    g.fillRect(block[i].x,block[i].y,block[i].width,block[i].height);
                }
            }
            g.setColor(Color.white);
            Font font3 = new Font("Arial", Font.BOLD, 14);
            g.setFont(font3);
            g.fillRect(px,py,pwidth,pheight);
            g.fillArc(bx,by,bwidth,bheight,0,360);
            g.drawString("Score: " + score,10,height - 10);
            g.drawString("Balls: ",100,height - 10);
            switch(balls) {
                case 0:
                    timer1.stop();
                    hovered = false;
                    started = false;
                    timer2.start();
                    repaint();
                    break;

                case 3:
                    g.fillArc(145,height - 18,8,8,0,360);
                    g.fillArc(155,height - 18,8,8,0,360);
                    g.fillArc(165,height - 18,8,8,0,360);

                case 2:
                    g.fillArc(145,height - 18,8,8,0,360);
                    g.fillArc(155,height - 18,8,8,0,360);

                case 1:
                    g.fillArc(145,height - 18,8,8,0,360);
            }
        }
    }

    public void drawBricks() {
        for (int i = 0; i < block.length; i++) {
            block[i] = new brick();
            block[i].width = 75;
            block[i].height = 15;
            block[i].visible = true;
            if (i <= 7) {
                if (i > 0) {
                    block[i].x = block[i - 1].x + 100;
                }
                else {
                    block[i].x = 10;
                }
                block[i].y = 10;
            }
            else if (i <= 14) {
                if (i > 8) {
                    block[i].x = block[i - 1].x + 100;
                }
                else {
                    block[i].x = 60;
                }
                block[i].y = 50;
            }
            else {
                if (i > 15) {
                    block[i].x = block[i - 1].x + 100;
                }
                else {
                    block[i].x = 10;
                }
                block[i].y = 90;
            }
        }
    }

    public void resetBall() {
        bx = px + pwidth / 2 - bwidth / 2;
        by = py - bheight;
        speedx = 10;
        speedy = -10;
    }

    public void mouseMoved(MouseEvent e) {
        mx = e.getX();
        my = e.getY();
        if (title) {
            if (mx >= width / 2 - 61 && mx <= width / 2 + 61 && my >= 227 && my <= 250) {
                hovered = true;
            }
            else {
                hovered = false;
            }
        }
        else {
            if (mx - pwidth / 2 <= 0) {
                mx = pwidth / 2;
            }
            if (mx + pwidth / 2 >= width) {
                mx = width - pwidth / 2;
            }
            px = mx - pwidth / 2;
            if (!started) {
                bx = px + pwidth / 2 - bwidth / 2;
            }
        }
        repaint();
    }

    public void mousePressed(MouseEvent e) {
        if (title && hovered) {
            title = false;
            drawBricks();
            balls = 3;
            score = 0;
            bricks = 0;
            px = width / 2 - pwidth / 2;
            resetBall();
            repaint();
        }
        else if (!title && !started) {
            started = true;
            timer1.start();
        }
    }

    public void mouseReleased(MouseEvent e) { }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    public void mouseClicked(MouseEvent e) { }

    public void mouseDragged(MouseEvent e) { }
}
Sign in to comment.
Joshuaxiong1   -  Nov 16, 2009

nice but then it's too fast! lol cheat and add more balls nice that it goes on the web also Wondering if you can make this one goes on the web also http://www.hawkee.com/snippet/6233/

 Respond  
mountaindew   -  Sep 04, 2009

I'm not sure about drjava, but it should pretty much be the same as I did in JCreator. Make an applet project and have two files: one for the main coding (BreakOut.java) and one for the brick properties (brick.java).

 Respond  
sakib   -  Sep 04, 2009

i use a java editor named drjava...i tried to run it ,but it gives the following prob. Static Error: No method in Breakout with name 'main' accepts arguments (String[])

so i also created a driver class with appropriate req.,,,,,then there were no compile errors...but it did not show any game...

what shud i do to run it in editor?

 Respond  
rakess   -  Apr 26, 2009

Thanks for the advice I was wondering if there was a way to do something like this: for (int i = 0; i < 5; i++) {

    int    block[i] = x;

} and it would create block0 = x block1 = x ..... block4 = x

I was trying to find a way to do this in the plane with no succes Thank you in advance for the help! a java noob

 Respond  
mountaindew   -  Apr 22, 2009

Well the block[ i ].y refers to that block's y position (the .y comes from the brick class).

class brick {
    boolean visible;
    int x,y,width,height;
}

And I don't think it matters how you draw the bricks, as long as they're there and you can find out the x and y, you should be good.

But for all those if's for the colors, I would just use a switch statement, that's what they're meant for:

switch (j) {
  case 0:
  case 1:
    bricks.setFillColor(Color.RED);
    break;

  case 2:
  case 3:
    bricks.setFillColor(Color.ORANGE);
    break;

  //etc
}
 Respond  
rakess   -  Apr 22, 2009

Hey, I was making my own breakout game but encoutered problems in the brick collision detection, I made a working version but the brick collision detection was really sloppy and badly written (got all bricks coordinates and constantly checked if the ball was somewhere in there). Well after looking at your source code i had some questions. on this line: block[i].visible && by <= block[i].y + block[i].height && bx + bwidth >= block[i].x && bx <= block[i].x + block[i].width && by + bheight >= block[i].y) {

when you write block[i].y, what does the dot mean?

Also when I draw my bricks I use a different technique, will this impair my chances of collision detection working? here is my code for the drawing of the bricks:

int x = 0;

    int y = BRICK_Y_OFFSET;

    for(int j = 0; j < NBRICK_ROWS; j++) {
        for(int i = 0; i < NBRICKS_PER_ROW; i++) {
            GRect bricks = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
                if( j == 0 ) {
                    bricks.setFillColor(Color.RED);
                }
                if( j == 1 ) {
                    bricks.setFillColor(Color.RED);
                }
                if(j == 2) {
                    bricks.setFillColor(Color.ORANGE);
                }
                if(j == 3) {
                    bricks.setFillColor(Color.ORANGE);
                }
                if(j == 4) {
                    bricks.setFillColor(Color.YELLOW);
                }
                if(j == 5) {
                    bricks.setFillColor(Color.YELLOW);
                }
                if(j == 6) {
                    bricks.setFillColor(Color.GREEN);
                }
                if(j == 7) {
                    bricks.setFillColor(Color.GREEN);
                }
                if(j == 8) {
                    bricks.setFillColor(Color.CYAN);
                }
                if(j == 9) {
                    bricks.setFillColor(Color.CYAN);
                }

            bricks.setFilled(true);
            add(bricks);
            x = (x + BRICK_SEP + BRICK_WIDTH);    

        }
    y = (y + BRICK_SEP + BRICK_HEIGHT);
    x = 0;

Thank you in advanced for your help!

 Respond  
mountaindew   -  Apr 18, 2009

Thanks guys :)

 Respond  
Prelude   -  Apr 17, 2009

Yeah gota say this is really good, i get some flickering, but thats about it. Pretty smooth otherwise. 7/10.

 Respond  
Hawkee   -  Apr 17, 2009

This is great, just wish there was a way to control the angle by the speed I'm moving the paddle. I'm really impressed with these Java snippets you've been posting lately. Keep it up!

 Respond  
Aucun50   -  Apr 16, 2009

With some more things you could make the ipod version, very nice script.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.