I am making a Breakout game. I have two class: a brick class which displays an array of brick images and a ball class which moves an ball image around the window. I'm trying to figure out how to make the brick disappear when the ball hits one of the brick. Any help would be greatly appreciated.
Brick class:
public class Brick {
private URL url;
private Image brick;
Image [][] bricks = new Image[50][3];
public Brick (Breakout bR){
url = bR.getDocumentBase();
brick = bR.getImage(bR.getDocumentBase(),"brick.png");
for(int i =0; i < bricks.length; i++)
for(int j = 0; j < bricks[0].length; j++)
bricks[i][j] = brick;
}
public void update(Breakout bR){}
public void paint(Graphics g, Breakout bR){
brick = bR.getImage(bR.getDocumentBase(),"brick.png");
int imageWidth = imageWidth = bricks[0][0].getWidth(bR);
int imageHeight = imageHeight = bricks[0][0].getHeight(bR);
for (int i = 0; i < bricks.length; i++)
for ( int j =0; j < bricks[0].length; j++)
g.drawImage(brick, i * imageWidth + 5, j* imageHeight + 5, bR);
}
}
Ball Class:
public class Ball {
private int x=355 ;
private int y=200;
private int speed = 8;
private int xVel = -speed;
private int yVel = speed;
private boolean gameOver = false;
private Image ball;
public Ball (Breakout bR){
ball = bR.getImage(bR.getDocumentBase(),"ball.png");
}
public void update(Breakout bR, Paddle p){
x += xVel;
y += yVel;
if (x < 0){
xVel = speed;
}
else if (x > bR.getWidth()){
xVel = -speed;
}
if(y > bR.getHeight()){
gameOver = true;
}
else if (y < 0){
yVel = speed;
}
collision(p);
}
public void collision(Paddle p){
int pX = p.getX();
int pY = p.getY();
int pHeight = p.getImageHeight();
int pWidth = p.getImageWidth();
if (pX<=x && pX+pWidth>=x && pY-pHeight<=y && pY+pHeight>=y){
yVel = -speed;
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void paint (Graphics g, Breakout bR){
g.drawImage(ball,x,y,bR);
if (gameOver){
g.setColor(Color.WHITE);
g.drawString("Game Over", 100,300);
}
}
}
Thanks for your help :)