gpt4 book ai didi

java - java中的砖 block 碰撞突破

转载 作者:行者123 更新时间:2023-12-01 09:48:44 25 4
gpt4 key购买 nike

我一直在开发一款 Breakout 游戏,除了砖 block 碰撞之外,几乎所有的事情都完成了。球在墙壁、桨和砖 block 上弹跳。然而,当球从砖 block 上弹起时,砖 block 并没有消失。我真的很困惑。任何想法都将不胜感激。我所拥有的砖 block 、球和主类:

砖类:

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 类 中添加一个 boolean isDestroyed = false。一旦Ball接触Brick(在您的collision()方法中),设置isDestroyed = true并停止绘制/与该Brick交互:)不要忘记使该brick = null,以便稍后释放内存:)

执行此操作的最佳方法是将 boolean 值 isDestroyed 创建为私有(private)变量,并使用 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;
}

但是,如果我是你,我会创建一个 BricksArrayList 并通过 ArrayList 添加/删除它们。这样他们会更容易管理:)如果你还需要什么,你好。

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

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