gpt4 book ai didi

java - java中的砖 block 碰撞突破

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:00 24 4
gpt4 key购买 nike

我正在开发一款突破游戏。除了砖 block 的碰撞之外,我几乎完成了所有事情。到目前为止,当球击中砖 block 时,砖 block 会弹回来,但砖 block 不会消失。如果有人可以帮助我那就太好了。

主类:

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

}

}

砖类:

public class Brick2{
private int x;
private int y;

public Brick2(){

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

}}

球类:

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

}

感谢您的帮助。

最佳答案

如果检测到碰撞,Ball 类的碰撞函数中似乎不会发生任何事情。

之后

ba.setXVel(6);

您需要一些代码才能使砖 block 不再上色。尝试在类定义中为砖 block 指定可见性属性:

private Boolean visible;

当处于碰撞函数后

ba.setXVel(6);

插入

setVisible(false);

您需要为可见属性编写一个 setter 和 getter。然后在paint函数中,将其设置为仅在visible为true时才进行绘制。

您需要拥有多个 Brick2 实例。每个 Brick2 实例将代表一个单独的砖 block 。将它们放入像 List 这样的集合中,然后逐步遍历它们以绘制它们。

创建列表的一种方法是:

List<Brick2> bricks = new List<Brick2>;
for (i = 0; i > num_bricks; i++ ) {
bricks.add(new Brick2());
}

但是您需要有一种方法来设置每个砖 block 的位置。也许基于砖 block 编号(i)并使用砖 block 构造函数根据它们的创建顺序来得出它的位置。

关于java - java中的砖 block 碰撞突破,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37780376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com