Esempio n. 1
0
  private void addNewBodyPart(Directions direction) {
    BodyParts bodyPart;

    switch (direction) {
      case LEFT:
        bodyPart =
            new BodyParts(
                (Map.getCols() + headPosition().getCol() - 1) % Map.getCols(),
                headPosition().getRow());
        break;

      case RIGHT:
        bodyPart =
            new BodyParts((headPosition().getCol() + 1) % Map.getCols(), headPosition().getRow());
        break;

      case UP:
        bodyPart =
            new BodyParts(
                headPosition().getCol(),
                (Map.getRows() + headPosition().getRow() - 1) % Map.getRows());
        break;

      case DOWN:
        bodyPart =
            new BodyParts(headPosition().getCol(), (headPosition().getRow() + 1) % Map.getRows());
        break;

      default:
        bodyPart = new BodyParts(headPosition().getCol() - 1, headPosition().getRow());
        break;
    }

    // System.out.println(bodyPart.position);
    snakeBody.add(bodyPart);
  }
Esempio n. 2
0
  public Snake() {
    speedUp = false;
    wallCross = 0;
    selfCross = 0;
    snakeBody = new LinkedList<>();
    BodyParts snakeHead = new BodyParts(Map.getCols() / 2, Map.getRows() / 2);

    for (int i = 3; i > 0; i--) {
      BodyParts bodyPart =
          new BodyParts(snakeHead.position.getCol() + i, snakeHead.position.getRow());

      // System.out.println("For bodyPart " + bodyPart.position);
      snakeBody.add(bodyPart);
    }

    snakeBody.add(snakeHead);
  }