if (e.getSource() == fastItem) {
snake.speed = Snake.FAST;
speedLabel.setText("Fast");
}
}
};
pauseItem.addActionListener(l);
beginItem.addActionListener(l);
newItem.addActionListener(l);
aboutItem.addActionListener(l);
slowItem.addActionListener(l);
midItem.addActionListener(l);
fastItem.addActionListener(l);
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
// ------------方向键改变蛇运行方向
case KeyEvent.VK_DOWN://
snake.changeDerection(Snake.DOWN); break;
case KeyEvent.VK_UP://
snake.changeDerection(Snake.UP); break;
case KeyEvent.VK_LEFT://
snake.changeDerection(Snake.LEFT); break;
case KeyEvent.VK_RIGHT://
snake.changeDerection(Snake.RIGHT); break;
// 空格键,游戏暂停或继续
case KeyEvent.VK_SPACE://
if (snake.isRun == true) {
snake.isRun = false;
snake.status = Snake.PAUSED; break;
}
if (snake.isRun == false) {
snake.isRun = true;
snake.status = Snake.RUNNING; break;
}
}
}