BreakOut Applet
Platform: Java
Published Apr 15, 2009
Updated 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) { }
}