Пример #1
0
    public BodyParts(int col, int row) {
      this.position = new Position(col, row);
      bodyDrawing =
          new Rectangle(
              position.getCol() * Map.getCellSize(),
              position.getRow() * Map.getCellSize(),
              Map.getCellSize(),
              Map.getCellSize());

      Color color = speedUp ? Color.RED : Color.GREEN;
      bodyDrawing.setColor(color);
      bodyDrawing.fill();
    }
Пример #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);
  }
Пример #3
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);
  }