1.敌机类
public class EnemyAirplane extends FlySprite implements ActionSprite {
//敌机类型<br /> private int type;<br /> //飞机下落的速度<br /> private int speed;<br /> //飞机的血量<br /> private int blood;<br /> //分值<br /> private int score;
public EnemyAirplane() {<br /> this.init();<br /> }
@Override<br /> public void init() {<br /> //创建随机对象<br /> Random random = new Random();<br /> //1-10;<br /> int i = random.nextInt(10) + 1;<br /> //1:3:6:生成不同的飞机<br /> if (i % 10 == 0) {<br /> type = 2;<br /> } else {<br /> //369<br /> if (i % 3 == 0) {<br /> type = 1;<br /> } else {<br /> type = 0;<br /> }<br /> }<br /> //根据飞机类型不通,速度也不同<br /> speed = 5 - type;<br /> if (type == 2) {<br /> score = blood = 10;<br /> } else {<br /> if (type == 1) {<br /> score = blood = 5;<br /> } else {<br /> score = blood = 1;<br /> }<br /> }<br /> //根据飞机的类型,加载相应的图片<br /> super.image = ImageLoaderUtil._load_("enemy"+type+".png");<br /> super.width = super.image.getWidth()/2;<br /> super.height = super.image.getHeight()/2;<br /> //x,y<br /> //敌机从屏幕外进入,因此y轴设定为敌机的负高度<br /> super.y = -super.height;<br /> // 随机敌机的X轴<br /> super.x =random.nextInt(FlyFrame._WIDTH_-super.width);<br /> }
@Override<br /> public void destroy() {<br /> FlyPanel._enemyAirplaneList_.remove(this);<br /> }
@Override<br /> public void down() {<br /> super.y = super.y+speed;<br /> if(super.y>FlyFrame._HEIGHT_){<br /> this.destroy();<br /> }<br /> }<br />}
1.敌机定时器
//敌机的集合,
public static CopyOnWriteArrayList
@Override
public void paint(Graphics g) {
g.drawImage(background, x, y, bgWidth, bgHeight, null);
paintState(g);
hero.draw(g);
for (Bullet bullet : bulletList) {
bullet.draw(g);
bullet.up();
}
for (EnemyAirplane enemyAirplane : enemyAirplaneList) {
enemyAirplane.draw(g);
enemyAirplane.down();
}
}
//敌机的定时任务
class EnemyTask extends TimerTask {
@Override<br /> public void run() {<br /> EnemyAirplane enemyAirplane = new EnemyAirplane();<br /> _enemyAirplaneList_.add(enemyAirplane);<br /> //重绘<br /> repaint();<br /> }<br />}
super.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch (gameState) {
//当游戏状态是START 点击 变成RUNNING
case START:
gameState = GameState.RUNNING;
bgRoll = new BgRoll();
shootTask = new ShootTask();
enemyTask = new EnemyTask();
timer.schedule(bgRoll, 0, 50);
timer.schedule(shootTask, 0, 200);
timer.schedule(enemyTask, 0, 1000);
break;
case RUNNING:
gameState = GameState.PAUSE;
bgRoll.cancel();
shootTask.cancel();
enemyTask.cancel();
repaint();
break;
case PAUSE:
gameState = GameState.RUNNING;
bgRoll = new BgRoll();
shootTask = new ShootTask();
enemyTask = new EnemyTask();
timer.schedule(bgRoll, 0, 50);
timer.schedule(shootTask, 0, 200);
timer.schedule(enemyTask, 0, 1000);
break;
case GAME_OVER:
gameState = GameState.START;
break;
}
}
});