我是Andoid的新手,我有一个简单的蛇游戏。 我有一个有毒的苹果(只有一个在屏幕上的随机位置产生),如果蛇吃掉它就死了。
有什么建议吗?
My PoisonedFood课程:
public abstract class PoisonedFood {
public abstract float getX();
public abstract float getY();
public abstract void eatPoison();
public abstract void render(GL10 gl);
}
我的SFood课程:
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
}
}
答案 0 :(得分:3)
嗯我猜蛇应该吃的食物,而不是食物应该吃的东西。
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()
肯定应该通过食物和蛇的碰撞事件来运行。