//实时检测是否碰撞任务类
class HitTask extends TimerTask{
@Override<br /> public void run() {<br /> //敌机与英雄机是否碰撞<br /> for(FlySprite enemy:_enemys_){<br /> //判断是否碰撞<br /> //if(PhysicsManager.isHit(hero,enemy)){<br /> if(_hero_.isHit(enemy)){<br />// JOptionPane.showMessageDialog(null,"碰撞到了,游戏结束");<br /> System._out_.println("碰撞到了!");<br /> }<br /> }<br /> //子弹与敌机是否碰撞<br /> for(Bullet bullet:_bulletList_){<br /> for (FlySprite enemy:_enemys_){<br /> //if(PhysicsManager.isHit(bullet,enemy)){<br /> if(bullet.isHit(enemy)){<br /> bullet.destroy();<br /> enemy.destroy();<br /> }<br /> }<br /> }<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();
hitTask = new HitTask();
timer.schedule(bgRoll, 0, 50);
timer.schedule(shootTask, 0, 200);
timer.schedule(enemyTask, 0, 1000);
timer.schedule(hitTask, 0, 50);
break;
case RUNNING:
gameState = GameState.PAUSE;
bgRoll.cancel();
shootTask.cancel();
enemyTask.cancel();
hitTask.cancel();
repaint();
break;
case PAUSE:
gameState = GameState.RUNNING;
bgRoll = new BgRoll();
shootTask = new ShootTask();
enemyTask = new EnemyTask();
hitTask = new HitTask();
timer.schedule(bgRoll, 0, 50);
timer.schedule(shootTask, 0, 200);
timer.schedule(enemyTask, 0, 1000);
timer.schedule(hitTask, 0, 50);
break;
case GAME_OVER:
gameState = GameState.START;
break;
}
}
});