【问题标题】:Breakout brick collision in javajava中的突破砖碰撞
【发布时间】:2016-06-12 06:07:21
【问题描述】:

我一直在开发一个 Breakout 游戏,除了砖块碰撞之外,我几乎完成了所有工作。球在墙壁、桨和砖上反弹。然而,当球反弹砖块时,砖块并没有消失。我真的坚持这一点。任何想法都非常感谢。 我对砖、球和主课有什么:

砖类:

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

    }   
}

感谢您的帮助。

【问题讨论】:

    标签: java applet collision-detection breakout


    【解决方案1】:

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

    最好的方法是创建布尔 isDestroyed 作为私有变量,并使用 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;
    }
    

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

    【讨论】:

    • 如何停止画砖?
    • 如何停止与该特定积木的交互以及如何使积木 = null?我没有任何数组。
    • 你会一一添加每块砖吗?
    • 如果我要制作一个积木数组,那么该数组的数据类型应该是什么?
    • ArrayList 砖块 = 新的 ArrayList(); docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
    【解决方案2】:

    以与处理任何其他对象集合相同的方式处理一组砖块。你可以使用List&lt;brick&gt; = new ArrayList&lt;brick&gt;();

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多