示例#1
0
  private void play() {
    if (isReload) reloadGame();
    if (isEnd || isPause) return;

    addFeed();
    doesEatFeed();
    addRegime();
    doesEatRegime();
    crossBorder();

    head = blocks.peekFirst();

    Block newHead = new Block(head.getX() + dx[inputDirection], head.getY() + dy[inputDirection]);
    Rectangle headRect = new Rectangle(head.getX(), head.getY(), 10, 10);

    if (isEnd(newHead)) {
      isEnd = true;
      //            Image image = new Image(this.getClass().getResourceAsStream("/gameOver.png"));
      //            ImageView imageView = new ImageView(image);
      //            group.getChildren().add(imageView);
      //            scene.setOnKeyPressed(null);
      saveRecord();
      showRecord();
      return;
    } else {
      blocks.push(newHead);
      rectangles.push(headRect);

      blocks.pollLast();

      updateGui();
    }
  }
示例#2
0
  private void reloadGame() {
    if (!isReload) return;

    score = 0;
    isReload = false;
    isEnd = false;
    feedCounter1 = 0;
    feedCounter2 = 0;

    List<Rectangle> rectangleList = new ArrayList<>(rectangles);
    for (Rectangle r : rectangleList) group.getChildren().remove(r);

    regimeRectangle.setX(-20);

    Deque<Block> blocksTemp = new ArrayDeque<>();
    Deque<Rectangle> rectanglesTemp = new ArrayDeque<>();

    head = new Block(100, 150);
    tale = new Block(90, 150);

    if (inputDirection != 3) {
      blocksTemp.push(tale);
      blocksTemp.push(head);

      rectanglesTemp.push(new Rectangle(tale.getX(), tale.getY(), 10, 10));
      rectanglesTemp.push(new Rectangle(head.getX(), head.getY(), 10, 10));
    } else {
      blocksTemp.push(head);
      blocksTemp.push(tale);

      rectanglesTemp.push(new Rectangle(head.getX(), head.getY(), 10, 10));
      rectanglesTemp.push(new Rectangle(tale.getX(), tale.getY(), 10, 10));
    }

    blocks = blocksTemp;
    rectangles = rectanglesTemp;

    group.getChildren().add(rectangles.getFirst());
    group.getChildren().add(rectangles.getLast());

    timer.cancel();
    timer = null;
    timer = new Timer();

    timer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            Platform.runLater(() -> play());
          }
        },
        1,
        100);
  }
示例#3
0
  private void doesEatFeed() {
    if (isEnd) return;

    if (Math.abs(blocks.peekFirst().getX() - feedRectangle.getX()) < 10
        && Math.abs(blocks.peekFirst().getY() - feedRectangle.getY()) < 10) {
      feedRectangle.setX(1200);
      isFeed = false;
      ++feedCounter2;
      ++score;
      scoreLabel.setText(String.valueOf(score));

      head = blocks.peekFirst();
      Block newHead = new Block(head.getX() + dx[inputDirection], head.getY() + dy[inputDirection]);
      Rectangle headRect = new Rectangle(head.getX(), head.getY(), 10, 10);
      blocks.push(newHead);
      rectangles.push(headRect);
    }
  }
示例#4
0
  private boolean isEnd(Block newHead) {
    List<Block> blockTemp = new ArrayList<>(blocks);

    for (int i = 0; i < blockTemp.size(); i++)
      if (blockTemp.get(i).getX() == newHead.getX() && blockTemp.get(i).getY() == newHead.getY())
        return true;

    if (newHead.getX() >= obstacle1.getX()
        && newHead.getX() <= obstacle1.getX() + 150
        && ((newHead.getY() >= obstacle1.getY() && newHead.getY() <= obstacle1.getY() + 5)
            || (newHead.getY() <= obstacle1.getY() && newHead.getY() >= obstacle1.getY() - 5)))
      return true;

    if (newHead.getY() >= obstacle2.getY()
        && newHead.getY() <= obstacle2.getY() + 150
        && ((newHead.getX() >= obstacle2.getX() && newHead.getX() <= obstacle2.getX() + 5)
            || (newHead.getX() <= obstacle2.getX() && newHead.getX() >= obstacle2.getX() - 5)))
      return true;

    return false;
  }
示例#5
0
  public Snake(String playerName, String inputDirection, Scene scene, Group group) {
    this.playerName = playerName;
    this.scene = scene;
    this.group = group;

    switch (inputDirection) {
      case "s":
        this.inputDirection = 0;
        break;

      case "d":
        this.inputDirection = 1;
        break;

      case "w":
        this.inputDirection = 2;
        break;

      case "a":
        this.inputDirection = 3;
        break;
    }

    directionsMapping = new HashMap<>();
    directionsMapping.put("S", 0);
    directionsMapping.put("D", 1);
    directionsMapping.put("W", 2);
    directionsMapping.put("A", 3);

    blocks = new ArrayDeque<>();
    rectangles = new ArrayDeque<>();

    head = new Block(100, 150);
    tale = new Block(90, 150);

    if (this.inputDirection != 3) {
      blocks.push(tale);
      blocks.push(head);

      rectangles.push(new Rectangle(tale.getX(), tale.getY(), 10, 10));
      rectangles.push(new Rectangle(head.getX(), head.getY(), 10, 10));
    } else {
      blocks.push(head);
      blocks.push(tale);

      rectangles.push(new Rectangle(head.getX(), head.getY(), 10, 10));
      rectangles.push(new Rectangle(tale.getX(), tale.getY(), 10, 10));
    }

    random = new Random(System.currentTimeMillis());

    isFeed = false;
    isRegime = false;

    scoreLabel = new Label(String.valueOf(score));
    scoreLabel.setLayoutX(240);
    scoreLabel.setLayoutY(0);

    group.getChildren().add(scoreLabel);

    feedRectangle = new Rectangle(10, 10);
    feedRectangle.setFill(Color.RED);
    feedRectangle.setX(-20);
    group.getChildren().add(feedRectangle);

    regimeRectangle = new Rectangle(10, 10);
    regimeRectangle.setFill(Color.GREEN);
    regimeRectangle.setX(-20);
    group.getChildren().add(regimeRectangle);

    obstacle1 = new Rectangle(150, 10);
    obstacle1.setFill(Color.BLUE);
    obstacle1.setY(-20);
    group.getChildren().add(obstacle1);

    obstacle2 = new Rectangle(10, 150);
    obstacle2.setFill(Color.BLUE);
    obstacle2.setX(-20);
    group.getChildren().add(obstacle2);

    setBestPlayer();
    setBoardListener();
    addFeed();

    timer = new Timer();
    timer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            Platform.runLater(() -> play());
          }
        },
        1,
        100);
  }