0

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 :)

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Jose
  • 5
  • 2
  • 1
    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](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) See also [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example (using Java-2D shapes). – Andrew Thompson Jun 07 '16 at 05:42
  • 1
    BTW - is this using AWT or Swing components? – Andrew Thompson Jun 07 '16 at 05:42
  • 1
    Possible duplicate of [Collision detection with complex shapes](http://stackoverflow.com/questions/14574045/collision-detection-with-complex-shapes) – Martin Frank Jun 07 '16 at 10:13

1 Answers1

0

You can use Rectangle.intersects().

Create a getBounds() method for both your ball and brick classes. This will create a virtual rectangle surrounding your entities.

public Rectangle getBounds(){
    return new Rectangle(x, y, ballSizeX, ballSizeY);
}

Then detecting a collision would look like:

if(ball.getBounds().intersects(brick.getBounds())){
    doSomething();
}

More info here if you need it.

Rocky
  • 132
  • 1
  • 2
  • 9