0

I have been working on a Breakout game and have just about everything done except for the brick collision. The ball bounces of the wall, paddle and the brick. However when the ball bounces of the brick, the brick does not disappear. I am really stuck on this. Any ideas are greatly appreciated. What I have for brick, ball and main class:

Brick Class:

import java.awt.*;
import java.applet.*;
public class Brick2{
    private int x;
    private int y;
    public Brick2(int X, int Y){
        x =X;
        y=Y;
    }
    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 <= 600; 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:

    import java.awt.*;
import java.applet.*;
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);
        }
    }
}

Main Class:

    import java.applet.*;
import java.awt.*;

public class Breakout extends Applet implements Runnable{
    Thread thread = new Thread(this);
    boolean running = true;
    //Brick b;
    Brick2 b2;
    Paddle p;
    Ball ba;
    Image dbImage;
    Graphics dbg;
   public void init(){
        setSize(800,600);
        //b = new Brick(this);
        b2 = new Brick2(0,0);
        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(this,ba);

            b2.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, Brick2 b){
        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,this);
        b2.paint(g);
        p.paint(g,this);
        ba.paint(g,this);

    }   
}

Thanks for your help.

John
  • 11
  • 1

2 Answers2

1

You could add a boolean isDestroyed = false in your Brick Class. Once the Ball touches the Brick (in your collision() method), set isDestroyed = true and stop drawing/interacting with that Brick :) Don't forget to make that brick = null so it frees up memory later :)

The best way to do this would be to create the boolean isDestroyed as a private variable and get its' value with a isDestroyed method:

public class Brick2 {
    private boolean isDestroyed = false;

    public boolean isDestroyed() {
        return isDestroyed;
    }

    public void setIsDestroyed(boolean newValue) {
        isDestroyed = newValue;
    }
}

Then, in your Main class, before calling b2.paint(), check if b2.isDestroyed returns true:

public class Breakout...{
    public void paint(...) {
        if(b2 != null && !b2.isDestroyed())
            b2.paint(g);
        else
            b2 = null;
}

BUT, if I were you, I would create an ArrayList of Bricks and add/remove them through the ArrayList. They will be easier to manage that way :) Hola if you need anything else.

Aftab Safdar
  • 653
  • 5
  • 19
  • how do I stop drawing that brick? – John Jun 12 '16 at 06:15
  • How do I stop interacting with that specific brick and how do I make brick = null? I dont have any arrays. – John Jun 12 '16 at 06:21
  • will you be adding each brick 1 by 1? – Aftab Safdar Jun 12 '16 at 06:26
  • if I am going to make an array of bricks then what should the datatype of that array be? – John Jun 12 '16 at 06:29
  • ArrayList bricks = new ArrayList(); https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html – Aftab Safdar Jun 12 '16 at 06:31
  • where do I declare that array? – John Jun 12 '16 at 06:34
  • Instead of Brick2 b2; You would have ArrayList brick; - Then in the main constructor, you would initialize it.- and you can add/remove bricks from it whenever you want – Aftab Safdar Jun 12 '16 at 06:40
  • so in the main constructor if would have: for(int x = 0; x – John Jun 12 '16 at 06:44
  • In your `while(running)` loop, you could check each brick with a for loop. If it's null, call bricks.remove(thatPosition) and if it's not null, you could call paint(bricks.get(thatPosition)) or however you have it functioning – Aftab Safdar Jun 12 '16 at 06:45
  • I found you the perfect tutorial :) This will surely guide you. But don't just copy/paste! Try to learn what he/she is doing and read the comments too :) - http://www.java-forums.org/blogs/sunde887/93-breakout-tutorial.html – Aftab Safdar Jun 12 '16 at 06:49
  • No problem bud. Holla back if there's something you don't understand. I started off Java games with BrickBreaker as well :D – Aftab Safdar Jun 12 '16 at 06:56
0

Treat an array of bricks the same way you would treat any other collection of objects. You can use List<brick> = new ArrayList<brick>();

Brick should have four coordinates and shape would fit well for this purpose, int brickLife = 1 that changes to zero once the ball breaks it. You might want to increase the value to add sturdy bricks.

sixtytrees
  • 1,156
  • 1
  • 10
  • 25