gpt4 book ai didi

java - 贪吃蛇游戏中的 die() 方法用于有毒食物

转载 作者:行者123 更新时间:2023-12-01 20:52:36 25 4
gpt4 key购买 nike

我是 Android 新手,我有一个简单的贪吃蛇游戏。我有一个毒苹果(只有一个是在屏幕上随机位置生成的),如果蛇吃了它就会死。

有什么建议吗?

我的 PoisonedFood 类(class):

public abstract class PoisonedFood {
public abstract float getX();

public abstract float getY();

public abstract void eatPoison();

public abstract void render(GL10 gl);

}

我的 SFood 类(class):

public class SFood extends PoisonedFood {
public static final float RADIUS = .07f;

private float x, y, angle, speed;

public SFood(float x, float y) {
super();
this.x = x;
this.y = y;
}

public void render(GL10 gl) {
Image.setRotation((System.currentTimeMillis() / 6) % 360);
Image.setScale(RADIUS, RADIUS);
SGame.fImage.draw(gl, x, y);
}

public void update() {

}

public float getX() {
return x;
}

public float getY() {
return y;
}


public void eatPoison() {
//The method
}
}

最佳答案

嗯,我猜应该吃食物,而不是食物应该吃东西。

public class Snake() {
public void eatFood(SFood food) {
//check if food is poisoned
if (food instanceof PoisonedFood) {
this.die();
}

//or if you are hater of instanceof or you have isPoisoned() method
if (food.isPoisoned()) {
this.die();
}
}

public void die() {
//snake dies here
}
}

并且 snake.eatFood() 肯定应该通过食物和蛇的碰撞事件来运行。

关于java - 贪吃蛇游戏中的 die() 方法用于有毒食物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43000723/

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