飞机大战(3)

private static BufferedImage bulletImage;

private int bulletspeed; //子弹速度

//构造方法

public Bullet(int x,int y) {

x=x;

y=y;

height=bulletImage.getHeight();

width=bulletImage.getWidth();

bulletspeed = 3;

}

static {

try {

bulletImage = ImageIO.read(Bullet.class.getResourceAsStream("/picture/zd.png"));

} catch (IOException e) {

e.printStackTrace();

}

}

public void paint(Graphics g) {

g.drawImage(getImage(), x, y, null);

}

public void move() {

this.y-=bulletspeed;

}

public boolean outofbound() {

return this.y <height;

}

public BufferedImage getImage() {

return bulletImage;

}

五、回到入口类中

准备工作已经完成,现在可以回到入口类中完成整个代码了。梳理整个过程我们应该可以得知,我们还应该解决至少以下几个疑问:

如何创建子弹和敌机?

如何绘制战机、敌机和子弹?

怎样调用它们的移动方法?

在子弹碰撞敌机后,如何将它们俩从各自的ArrayList中去除?

如何让它们周而复始地自己完成运动?

得分机制如何?

因此我们需要以下方法来解决这些问题,并通过这些方法的调用,最终完成我们的飞机大战小游戏。

———————————————————————————————————

实例化sky 和 hero(战机)

创建子弹和敌机的动态数组

定义得分 和 子弹与敌机的计数器

定义战机的状态

private Hero hero = new Hero();

private Sky sky = new Sky();

public ArrayList<Bullet> bullets = new ArrayList<Bullet>();

public ArrayList<Enemy> enemies = new ArrayList<Enemy>();

//飞机的得分

private int score = 0;

//子弹和敌机的计数器,初始化设定为0

private int bulletCount = 0;

private int enemiesCount = 0;

//定义战机的状态 开始为0运行为1死亡为2 STATE为当前状态

public static int BEGIN = 0;

public static int RUNNING = 1;

public static int OVER = 2;

public static int STATE = 0;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

在这里将移动的方法都写到一个方法里,通过sky对象调用sky类里的move方法。将动态数组中的每个子弹和敌机都取出来,分别调用它们各自的移动方法。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。