在java中突破砖碰撞

时间:2016-06-12 06:07:21

标签: java applet collision-detection breakout

我一直致力于Breakout游戏,并且除了砖碰撞之外几乎完成了所有事情。球在墙壁,桨和砖上反弹。然而,当球弹出砖块时,砖块不会消失。我真的很困惑。任何想法都非常感谢。 我对砖,球和主要课程有什么用:

砖类:

if let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("vc3") as? ViewController3 {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window?.rootViewController!.presentViewController(vc3, animated: true, completion: nil)
}

球类:

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);
            }
        }
   }
}

主类:

    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);
        }
    }
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以在boolean isDestroyed = false中添加Brick Class。在Ball触及Brick后(在您的collision()方法中),设置isDestroyed = true并停止绘制/与Brick :)进行交互。不要忘记制作brick = null以便以后释放内存:)

执行此操作的最佳方法是将布尔值isDestroyed创建为私有变量并获取其&#39;使用isDestroyed方法的值:

public class Brick2 {
    private boolean isDestroyed = false;

    public boolean isDestroyed() {
        return isDestroyed;
    }

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

然后,在您的Main类中,在调用b2.paint()之前,检查b2.isDestroyed是否返回true:

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

但是,如果我是你,我会创建ArrayList Bricks并通过ArrayList添加/删除它们。如果您还需要其他任何东西,他们将更容易管理.Hola。

答案 1 :(得分:0)

以与处理任何其他对象集合相同的方式处理砖块数组。您可以使用List<brick> = new ArrayList<brick>();

砖块应该有四个坐标,形状适合这个目的,int brickLife = 1一旦球破坏就会变为零。您可能希望增加该值以添加坚固的砖块。