1
votes

I am working on a breakout game. i have almost everything done except for the collision for the bricks. so far, when the ball hits the brick the brick bounces back but the brick does not disappear. If someone could help me that would be great.

Main class:

public class Breakout extends Applet implements Runnable{
Thread thread = new Thread(this);
boolean running = true;
Brick2 b;
Paddle p;
Ball ba;
Image dbImage;
Graphics dbg;
public void init(){
    setSize(800,600);
    b = new Brick2();
    p = new Paddle(this);
    ba = new Ball(this);
}
public void start(){
    thread.start();
}
public void destroy(){
    running = false;
}
public void stop(){
    running = false;
}
public void run(){
    while(running){
        b.update(ba);
        p.update(this);
        ba.update(this,p);
        repaint();
        try{
            thread.sleep(20);
        }
        catch (InterruptedException e){
            System.out.println("AN ERROR HAS OCCURED");
        }
    }
}
public void update(Graphics g){
    dbImage = createImage(getWidth(),getHeight());
    dbg = dbImage.getGraphics();
    paint(dbg);
    g.drawImage(dbImage,0,0,this);
}
public void paint(Graphics g){
    g.fillRect(0,0,800,600);
    b.paint(g);
    p.paint(g,this);
    ba.paint(g,this);

}   

}

Brick Class:

public class Brick2{
private int x;
private int y;

public Brick2(){

}
public void update(Ball ba){
    collision(ba);
}
public void collision(Ball ba){
    int bX = ba.getX();
    int bY = ba.getY();
    int bHeight = ba.getImageHeight();
    int bWidth = ba.getImageWidth();
    for(int x=0; x <= 800; x+=55){
        for (int y=0; y<= 100; y+=25){
           if (bX-bWidth<=x && bX+bWidth>=x && bY-bHeight<=y && bY+bHeight>=y){
               ba.setXVel(6);

            }
        }
    }
    }
    public int getX(){
    return x;
}
public int getY(){
    return y;
}
public void paint(Graphics g){
    for(int x=0; x <= 800; x+=55){
        for (int y=0; y<= 100; y+=25){
            g.setColor(Color.RED);
            g.fillRect(x,y,50,20);
        }
    }

} }

Ball Class:

public class Ball {
private int x=355 ;
private int y=200;
private int speed = 6;
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 int getImageWidth(){
    return ball.getWidth(null);
}
public int getImageHeight(){
    return ball.getHeight(null);
}
public void setXVel(int xv){
    yVel = xv;
}
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.

1
See Detection/fix for the hanging close bracket of a code block for a problem I could no longer be bothered fixing.Andrew Thompson
1) Why code an applet? If it is due to the teacher specifying it, please refer them to Why CS teachers should stop teaching Java applets. 2) See Java Plugin support deprecated and Moving to a Plugin-Free Web. ..Andrew Thompson
.. 3) Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing.Andrew Thompson

1 Answers

0
votes

Nothing seems to happen in class Ball's collision function if a collision is detected.

After

ba.setXVel(6);

You need to have some code to make the brick no longer paint. Try giving the brick a visibility attribute in the class definition:

private Boolean visible;

When in the collision function after

ba.setXVel(6);

Insert

setVisible(false);

You'll need to write a setter and getter for the visible attribute. Then in the paint function, set it only to paint if visible is true.

You'll need to have more than one Brick2 instance. Each Brick2 instance will represent one individual brick. Put them in a collection like List and step through them to draw them.

One way to create the list would be:

List<Brick2> bricks = new List<Brick2>;
for (i = 0; i > num_bricks; i++ ) {
    bricks.add(new Brick2());
}

But you'll need to have a way to set the position of each brick. Perhaps based on the brick number(i) and use the brick constructor to derive it's position based on the order in which they're created.