示例#1
0
 /*
  * When the snake eat a fruti its size must be increased by 1 piece. This piece is added in the
  * middle so the tail should shift by a position. The direction of this shift depends on the
  * current direction of the tail
  */
 public void eat() {
   // add a new piece to the snake
   SnakeTail tail = (SnakeTail) parts.get(parts.size() - 1);
   parts.add(parts.size() - 1, new SnakeBody(tail.getX(), tail.getY(), tail.direction));
   // shift the tail by 1 position with a direction that depends on its current position.
   switch (tail.direction) {
     case UP:
       tail.moveDown();
       break;
     case DOWN:
       tail.moveUp();
       break;
     case LEFT:
       tail.moveRight();
       break;
     case RIGHT:
       tail.moveLeft();
       break;
   }
 }