J2SE贪吃蛇(Demo学习总结)(2)

在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);

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjjdd.html