爪哇中的突破砖碰撞



我一直在做一个突破游戏,除了砖块碰撞之外,几乎所有事情都完成了。球在墙壁、球拍和砖块上弹跳。但是,当球弹起砖块时,砖块不会消失。我真的被困在这一点上。任何想法都非常感谢。我对砖块、球类和主类有什么:

砖类:

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

主类:

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

感谢您的帮助。

您可以在

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

最好的方法是将布尔值 isDestroy 创建为私有变量,并使用 isDestroy 方法获取其值:

public class Brick2 {
    private boolean isDestroyed = false;
    public boolean isDestroyed() {
        return isDestroyed;
    }
    public void setIsDestroyed(boolean newValue) {
        isDestroyed = newValue;
    }
}
然后,在

主类中,在调用 b2.paint() 之前,检查 b2.isDestroy 是否返回 true:

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

但是,如果我是你,我会创建一个Bricks ArrayList并通过 ArrayList 添加/删除它们。这样会更容易管理:)如果你需要其他任何东西。

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

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

相关内容

  • 没有找到相关文章

最新更新