第一步,界面上画出英雄生命值
FlyPanel 类中
/*
画出生命值和分数
* @param g
*/
_private void paintLifeAndScore(Graphics g){
g.setColor(new Color(0xFF00000));
g.setFont(new Font(Font._SANS_SERIF,Font.BOLD,20));
g.drawString(“LIFE:”+hero.getLife(),20,45);
}
//当前游戏状态
public static GameState gameState = GameState.START;
@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 (FlySprite flySprite : enemys) {
flySprite.draw(g);
ActionSprite actionSprite = (ActionSprite) flySprite;
actionSprite.down();
}
paintLifeAndScore(g);
}
Hero类当中
@Override
public void destroy() {
//表示游戏结束
FlyPanel.gameState = GameState.GAME_OVER;
}
FlyPanel 类中
/*
根据当前的游戏状态进行绘制图片
* @param g
*/
_private void paintState(Graphics g) {
switch (_gameState) {
case START:
g.drawImage(start, 0, 0, null);
break;
case PAUSE:
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER:
//清理现场 游戏内容的重置
hero = new Hero();
enemys.clear();
bulletList.clear();
pauseGameTask();
g.drawImage(gameover, 0, 0, null);
//gameState = GameState.START;
break;
}
}
//添加事件,改变游戏状态
super.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch (gameState) {
//当游戏状态是START 点击 变成RUNNING
case START:
case PAUSE:
gameState = GameState.RUNNING;
startGameTask();
break;
case RUNNING:
gameState = GameState.PAUSE;
pauseGameTask();
repaint();
break;
case GAME_OVER:
gameState = GameState.START;
repaint();
break;
}
}
});