sta = "Paused";
break;
case Snake.GAMEOVER:
sta = "GameOver";
break;
}
statusLabel.setText(sta);
scoreLabel.setText("" + snake.score);
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
private JLabel scoreLabel;
private JLabel statusLabel;
private Snake snake;
}
// ----------蛇运动以及记录分数的线程
class SnakeRunnable implements Runnable {
public SnakeRunnable(Snake snake, Component component) {
this.snake = snake;
ponent = component;
}
public void run() {
while (true) {
try {
snake.move();
component.repaint();
Thread.sleep(snake.speed);
} catch (Exception e) {
}
}
}
private Snake snake;
private Component component;
}
class Snake {
boolean isRun; // ---------是否运动中