• Log in or sign up for an account here: https://www.sythe.org/login

[source] Breakout(non-applet) in Java! 100% coded by me.

slashshot007

Active Member
Joined
May 6, 2006
Sythe Gold
0
Messages
164
i decided to do something with my no life, so I made this, please, if you use this, give credit, I spent a lot of time on this, and am being very nice to show:

for the .jar file, which would be alot easier to just run, download it here:http://www.megaupload.com/?d=6GZ2HMD2

anyways there are alot of files, so i'll go first with the main one:

named File.java, this one simply creates a window, and adds the breakoutpanel to it, the breakoutpanel will be the next file.
Code:
import javax.swing.*;
import java.awt.*;

public class File{
	public static void main(String[] args){
		JFrame frame = new JFrame("Breakout, All Code 100% Designed by Slashshot");
		frame.setSize(500,500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		BreakoutPanel bPanel = new BreakoutPanel(500,500);
		Container pane = frame.getContentPane();
		pane.add(bPanel);
		frame.setVisible(true);
	}
}


Name: BreakoutPanel.java, this one is all of the coding and stuff, you can try and figure it out, its pretty organized so won't be too hard.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Thread;
import sun.audio.*;
import java.io.*;
import java.util.ArrayList;

public class BreakoutPanel extends JPanel implements Runnable{
	
	Paddle p;
	Ball b;
	Brick[][] brick = new Brick[10][5];
	int balls = 3, bricksLeft = 50;
	Thread myThread;
	int xSpeed;
	ArrayList<Pill> pills = new ArrayList<Pill>();
	int waitTime;
	
	
	public BreakoutPanel(int width, int height){
		super.setSize(width, height);
		
		int y2 = 0;
		
		createBricks();
		
		p = new Paddle(width/2,height-(height/10),width/7,height/50,Color.BLACK);
		addMouseMotionListener(new PanelMotionListener());
		b = new Ball(getWidth()/2,getHeight()/2,getWidth()/100,1,1,Color.BLUE);
		
		
		waitTime = Integer.parseInt(JOptionPane.showInputDialog(null,
			"Enter a speed (for faster computers, 3 or 4, for slower ones, 1 or 2)",
			"How Fast?",
			JOptionPane.QUESTION_MESSAGE));
  			
		myThread = new Thread(this);
		myThread.start();
		myThread.suspend();
		addMouseListener(new PanelListener());

	}
	
	public void createBricks(){
		int widthDistance = getWidth()/10;
		int heightDistance = getHeight()/20;
		boolean metal = true;
		for(int X = 0; X < 10; X++){
			for(int Y = 0; Y < 5; Y++){
				int random = (int)(Math.random()*13) + 1;
				if(random > 5 && random <= 13)
					random = 3;
				if(random == 4 || random == 5)
					random = 2;
				metal = (Y < 2);
				brick[X][Y] = new Brick(X*widthDistance,(heightDistance/2)+(Y*heightDistance),
					getWidth()/11,getHeight()/23,random,metal);
			}
		}
	}
	
	public void start(){
		myThread.resume();
	}

  	public void stop(){
		myThread.suspend();
	}

  	public void destroy(){
		myThread.resume();
		myThread.stop();
	}

  	public void run(){
  		xSpeed = 1;
		while(true){
			int x1 = b.getX();
			int y1 = b.getY();
			
			//Moderates the speed, so it wont go too fast
			if (Math.abs(xSpeed) > 2){
				if(xSpeed > 2)
					xSpeed--;
				if(xSpeed < 2)
					xSpeed++;
			}

			checkPaddle(x1,y1);
			
			checkWall(x1,y1);
				
			checkBricks(x1,y1);
				
			checkLives();
		
			checkIfOut(y1);
			
			b.move();
			
			dropPills();
			
			checkPillList();
			
			repaint();
			try {myThread.sleep(waitTime);}
			catch (InterruptedException e ) {}
		}
	}
	


	public void placePill(Pill p){
		pills.add(p);
	}
	
	public void dropPills(){
		for(int i = 0; i < pills.size(); i++){
			Pill tempPill = pills.get(i);
			tempPill.drop();
			pills.set(i,tempPill);
		}
	}
	
	public void checkLives(){
		if(bricksLeft == 0){
			repaint();
			balls = 0;
			stop();
		}
	}
	
	public void checkPillList(){
		for(int i = 0; i < pills.size(); i++){
			Pill tempPill = pills.get(i);
			if(p.absorbPill(tempPill)){
				pills.remove(i);
			}else if(tempPill.getY() > getHeight())
				pills.remove(i);
		}
	}
	
	public void checkPaddle(int x1, int y1){
			if(p.hitLeft(x1,y1)){
				playSound("ding.au");
				b.setYDir(-2);
				xSpeed += -1;
				b.setXDir(xSpeed);
			}else if(p.hitRight(x1,y1)){
				playSound("ding.au");
				b.setYDir(-2);
				xSpeed += 1;
				b.setXDir(xSpeed);
			}
	}
	
	public void checkWall(int x1, int y1){
			if(x1 >= getWidth())
				xSpeed = -Math.abs(xSpeed);
				b.setXDir(xSpeed);
			if(x1 <= 0)
				xSpeed = Math.abs(xSpeed);
				b.setXDir(xSpeed);
			if(y1 <= 0)
				b.setYDir(2);
	}
	
    public void checkBricks(int x1, int y1){
    		for(int X = 0; X < 10; X++)
				for(int Y = 0; Y < 5; Y++){
					if(brick[X][Y].hitBottom(x1,y1)){
						playSound("beep_3.au");
						b.setYDir(2);
						if(brick[X][Y].destroyed()){
							bricksLeft--;
							placePill(brick[X][Y].pill);
						}
					}
					if(brick[X][Y].hitLeft(x1,y1)){
						playSound("beep_3.au");
						xSpeed = -xSpeed;
						b.setXDir(xSpeed);
						if(brick[X][Y].destroyed()){
							bricksLeft--;
							placePill(brick[X][Y].pill);
						}
					}
					if(brick[X][Y].hitRight(x1,y1)){
						playSound("beep_3.au");
						xSpeed = -xSpeed;
						b.setXDir(xSpeed);
						if(brick[X][Y].destroyed()){
							bricksLeft--;
							placePill(brick[X][Y].pill);
						}
					}
					if(brick[X][Y].hitTop(x1,y1)){
						playSound("beep_3.au");
						b.setYDir(-2);
						if(brick[X][Y].destroyed()){
							bricksLeft--;
							placePill(brick[X][Y].pill);
						}
					}
				}
    }
    
    public void checkIfOut(int y1){
    	if(y1 > p.getY() + 10){
			balls --;
			b.setY(getHeight()/2); 
			repaint();
			stop();
		}
    }
    
    
    private void playSound(String file){
    	try{
			InputStream in = new FileInputStream(file);
			AudioStream as = new AudioStream(in);  
			AudioPlayer.player.start(as);
		}catch(FileNotFoundException exc){
			System.out.println(exc);
		}catch(IOException exc){
			System.out.println(exc);
		}
    }

	public void paintComponent(Graphics g){
		super.paintComponent(g);
		p.draw(g);
		b.draw(g);
		for(int X = 0; X < 10; X++)
			for(int Y = 0; Y < 5; Y++)
				brick[X][Y].draw(g);
		g.setColor(Color.BLACK);
		g.drawString("Balls left: " + Integer.toString(balls), getWidth() - (getWidth()/7), getHeight() - (getHeight()/10));
		
		for(Pill p: pills){
			p.draw(g);
		}
		
		if(balls == 0){
			int heightBorder = getHeight()/10;
			int widthBorder = getWidth()/10;
			g.setColor(Color.BLACK);
			g.fillRect(widthBorder, heightBorder, getWidth() - (2 * widthBorder), getHeight() - (2 * heightBorder ));
			g.setColor(Color.WHITE);
			g.drawString("Game Over, Click the mouse to start over", getWidth()/5,getHeight()/2);
		}
		
		if(bricksLeft == 0){
			int heightBorder = getHeight()/10;
			int widthBorder = getWidth()/10;
			g.setColor(Color.BLACK);
			g.fillRect(widthBorder, heightBorder, getWidth() - (2 * widthBorder), getHeight() - (2 * heightBorder ));
			g.setColor(Color.WHITE);
			g.drawString("Congratulations! You Win!", getWidth()/5,getHeight()/2);
		}
		
	}
	
	private class PanelMotionListener extends MouseMotionAdapter{
		public void mouseMoved(MouseEvent e){
			int newX = e.getX()-getWidth()/17;
			p.setX(newX);
			repaint();
		}
	}
	private class PanelListener extends MouseAdapter{
		public void mousePressed(MouseEvent e){
			if (balls>0){
				start();
			}else{
				balls = 3;
				createBricks();
				for(int X = 0; X < 10; X++){
					for(int Y = 0; Y < 5; Y++){
						brick[X][Y].setVisible(true);
					}
				}
				b.setY(getHeight()/2);
				bricksLeft = 50;
				repaint();
				start();
			}
		}
	}
}

next one is called Ball.java. it is the ball object(duh)
Code:
import javax.swing.*;
import java.awt.*;

public class Ball{
	private int xCoord, yCoord, radius;
	private Color color;
	private int xDir = 1, yDir = 2 ;
	
	public Ball(int x, int y , int r, int xD, int yD, Color c){
		xCoord = x;
		yCoord = y;
		setRadius(r);
		setColor(c);
	}
	public int getX(){
		return xCoord;
	}
	public int getY(){
		return yCoord;
	}
	public void setY(int y){
		yCoord = y;
	}
	public int getRadius(){
		return radius;
	}
	public Color getColor(){
		return color;
	}
	public void setXDir(int x){
		xDir = x;
	}
	public void setYDir(int y){
		yDir = y;
	}
	
	public int getXDir(){
		return xDir;
	}
	public int getYDir(){
		return yDir;
	}
	public void setRadius(int r){
		radius = r;
	}
	public void setColor(Color c){
		color = c;
	}
	public void move(){
		xCoord = xCoord + xDir;
		yCoord  = yCoord + yDir;
	}
	public void draw(Graphics g){
		Color oldColor = g.getColor();
		g.setColor(color);
		g.fillOval(xCoord - radius, yCoord - radius, radius * 2, radius * 2 );
		g.setColor(oldColor);
	}
	public boolean containsPoint(int x, int y){
		int xSquared = (x-xCoord)*(x - xCoord);
		int ySquared = (y - yCoord)*(y - yCoord);
		int radiusSquared = radius *radius;
		return xSquared + ySquared - radiusSquared <= 0;
	}
}


Paddle.java (the paddle):
Code:
import java.awt.*;

public class Paddle{
	public Color color;
	private int x, y, width, height;
	private int maxWidth;
	private int minWidth;
	
	public Paddle(int X, int Y, int Width, int Height, Color c){
		x = X;
		y = Y;
		width = Width;
		height = Height;
		color = c;
		maxWidth = width*2;
		minWidth = width/2;
	}
	public void setX(int X){
		x = X;
	}
	
	public int getY(){
		return y;
	}
	
	public int getWidth(){
		return width;
	}
	
	public void setWidth(int Width){
		if(Width > maxWidth || Width < minWidth){
			return;
		}else{
			width = Width;
		}
	}
	
	public boolean hitLeft(int X, int Y){
		if((X >= x) && (X <= x+(width/2)) && ((y == Y)||(y == Y - 1))){
			return true;
		}
		return false;
	}
	public boolean hitRight(int X, int Y){
		if(((X >= x+(width/2)) && (X <= x+width)) && ((y == Y)||(y == Y - 1))){
			return true;
		}
		return false;
	}
	
	public boolean absorbPill(Pill p){
		if((p.getX() < x + width) && (p.getX()+ p.getWidth() > x) && (y == p.getY()||y == p.getY() -1)){
			p.absorbed(this);
			return true;
		}
		return false;
	}
	
	public void draw(Graphics g){
		Color oldColor = g.getColor();
		g.setColor(color);
		g.fillRect(x,y,width,height);
		g.setColor(oldColor);
	}
}

brick.java (the bricks)
Code:
import java.awt.*;

public class Brick{
	public Color color;
	private int x, y, width, height, numHits, maxHits;
	private boolean visible;
	public Pill pill;
	
	
	public Brick(int X, int Y, int Width, int Height, int pillType, boolean Metal){
		x = X;
		y = Y;
		width = Width;
		height = Height;

		pill = new Pill(x+(width/4),y+(height/4),width/2,height/2,pillType);
		if(Metal){
			maxHits = 2;
			color = Color.GREEN;
		}else{
			maxHits = 1;
			color = Color.RED;
		}
		numHits = 0;
		visible = true;
	}
	public void setX(int X){
		x = X;
	}

	public void addHits(){
		numHits++;
		color = Color.RED;
		if(numHits == maxHits){
			visible = false;
		}
	}
	public boolean hitBottom(int X, int Y){
		if((X>=x) && (X <= x+width) && ((y+height == Y)||(y+height == Y + 1)) && visible == true){
			addHits();
			return true;
		}
		return false;
	}
	
	public void setVisible(boolean b){
		visible = b;
	}
	
	public boolean hitTop(int X, int Y){
		if((X>=x) && (X <= x+width) && ((y == Y)||(y == Y - 1)) && visible == true){
			addHits();
			return true;
		}
		return false;
	}
	
	public boolean hitLeft(int X, int Y){
		if((Y>=y) && (Y <= y+height) && ((x == X)||(x == X - 1)) && visible == true){
			addHits();
			return true;
		}
		return false;
	}
	
	public boolean hitRight(int X, int Y){
		if((Y>=y) && (Y <= y+height) && ((x+width == X)||(x+width == X - 1)) && visible == true){
			addHits();
			return true;
		}
		return false;
	}
	
	public boolean destroyed(){
		return !visible;
	}
	
	public void draw(Graphics g){
		if(visible){
			Color oldColor = g.getColor();
			g.setColor(color);
			g.fillRect(x,y,width,height);
			g.setColor(oldColor);
		}
	}
	
}

Pill.java: (this one is the pills that make the paddle either small, or large)
Code:
import java.awt.*;

public class Pill{
	public static final int BIG = 1;
	public static final int SMALL = 2;
	public static final int NOTHING = 3;
	
	int x, y,width,height,type;
	
	public Pill(int X, int Y,int Width, int Height, int Type){
		x = X;
		y = Y;
		width = Width;
		height = Height;
		type = Type;
	}
	
	public void drop(){
		y += 1;
	}
	public int getY(){
		return y;
	}
	public int getX(){
		return x;
	}
	public int getWidth(){
		return width;
	}
	public int getType(){
		return type;
	}
	
	public void absorbed(Paddle p){
		if(getType() == BIG){
			p.setWidth(p.getWidth() * 2);
		}else if(getType() == SMALL){
			p.setWidth(p.getWidth()/2);
		}
	}
	public void draw(Graphics g){
		Color oldColor = g.getColor();
		if(type == NOTHING){
			return;
		}else if(type == BIG){
			g.setColor(Color.BLUE);
		}else if(type == SMALL){
			g.setColor(Color.MAGENTA);
		}
		g.fillRect(x,y,width,height);
		g.setColor(oldColor);
	}
}


anyways put them all into a workspace, name them the instructed name, compile and run and you should be playing breakout!

I will probably continue to work on this, and it is not done, but as of now, constructive criticism is appreciated.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
TOO MANY FILES!!!!! I will tester out laterz... :)
 
suf, i created a jar file, so you dont even need to touch the files, if you want to run the application. just run the jar.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
suf, i created a jar file, so you dont even need to touch the files, if you want to run the application. just run the jar.

oooo, why thank you. :)


EDIT: SHit nice. :D


I am going to make a bot to win for me. :P

The last block took me about 20 minutes to get. It kept going 1 px away from the damned thing i think. I hit the same place RIGHT NET TO IT like 100 times... make it so when you get a bigger block that the mouse is in the center and make it so that you can not move the thing off the edge of the map... would be nice.... :/




EDIT 2: btw the last 2 files you posted r the same. /fail :D
 
Member of the Month Winner
Sythe's 10th Anniversary
Good job. Would be easier in flash though. :(
Java is far better and has a lot more potential than flash to be honest - Flash is just a convenient way of animating, and to be honest I don't see the point in using it.
 
I know, I think Java is far better too, AS cannot compete with it. Which is why it's a shame that flash is so popular among developers of 2d games on the internet. :(
 
really nice man! :)
not very hard to win tho ;)
maybe should try to make a similar game in C# will post if i bother/succeed :p
 
hampe did you grow up with asbestos in your room?
clearly he was implying the code.
 
hampe did you grow up with asbestos in your room?
clearly he was implying the code.
asbetos, hmm, no I don't think so.. what makes you believe that?:)
I'm just getting so irritated on people that think it's the same thing...
and I'm not stupid, I understand what he meant...
 
Awesome job)
Could you tell me where I can change the speed of ball, in which file and stroke?
 
duelarena.gg
Back
Top