我创建了一个太空射击游戏,完成后我显示一个菜单和再次播放的选项。当我按下它时,它从我离开的地方开始(如果游戏输了)。我没有游戏循环,我认为问题出在游戏结束后解散敌人和玩家。
private static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
//constructor
public GameFrame(){
setFocusable(true);
addMouseListener(new MouseInput());
mainTimer = new Timer(12,this);
mainTimer.start();
setupScene();
private void setupScene()
{
player = new Player(300,520);
enemyCount = 10;
addKeyListener(new KeyAdapt(player));
startGame();
}
private void clearScene(){
score=0;
level=1;
player = null;
for(int j=0; j<enemies.size(); j++)
{
removeEnemy(enemies.get(j));
}
}
public void startGame() {
for (int i = 0; i < enemyCount; i++) {
addEnemy(new Enemy(rand.nextInt(600), -rand.nextInt(600)));
}
}
public void actionPerformed(ActionEvent e)
{
if(State == STATE.GAME) {
player.update();
repaint();
for (int i = 0; i < enemies.size(); i++) {
Enemy tempEnemy = enemies.get(i);
tempEnemy.update();
}
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.update();
repaint();
}
}
checkGameOver();
这是如果游戏输了我可以更改游戏状态的方法
private void checkGameOver() {
for (int i = 0; i < enemies.size(); i++) {
if (enemies.get(i).getY() >= 570)
{
State = STATE.GAME_OVER;
clearScene();
setupScene();
}
}
}