在canvas类里头控制蛇行走的方向,如下所示:
/*
* 按键功能事件
*/
public void keyPressed(int keyCode) {
gameaction = this.getGameAction(keyCode);
switch (gameaction) {
case UP:
snake.changeDirection(Snake.UP);
break;
case DOWN:
snake.changeDirection(Snake.DOWN);
break;
case LEFT:
snake.changeDirection(Snake.LEFT);
break;
case RIGHT:
snake.changeDirection(Snake.RIGHT);
break;
}
this.display(snake, food, ground);
}