导弹模型设计
在飞机大战游戏中,导弹类继承自会飞的模型类。导弹类除具有会飞的模型类中的属性外,还具有移动速度这一属性。导弹的效果图如图23.11所示。
(1)定义导弹的移动速度为 3,在导弹类的构造方法中,初始化导弹类的图片、导弹类图片左上角的 x 坐标和导弹类图片左上角的 y 坐标。代码如下:
public class Ammo extends FlyModel {
private int speed = 3; // 导弹的移动速度
public Ammo(int x, int y) {
this.x = x;
this.y = y;
this.image = com.mr.view.GamePanel.ammoImage;
}
}
(2)因为导弹类继承自会飞的模型类,所以在导弹类中要重写会飞的模型类中的 move() 方法和 outOfPanel() 方法。其中:在 move() 方法中,语句 y -= speed 使得导弹类图片左上角的 y 坐标不断地改变;在 outOfPanel() 方法中,语句 y ← height
判断导弹图片是否移动到游戏面板外。代码如下:
/**
* 导弹图片的移动方法
*/
public void move() {
y -= speed;
}
/**
* 导弹图片是否移动到游戏面板外
*/
public boolean outOfPanel() {
return y <- height;
}