Esempio n. 1
0
  // lisab Layout Groupile Panei
  public void setContent(Pane pane) {
    // Taustapildi lisamine
    Canvas backgroundImageCanvas = new Canvas(800, 600);
    GraphicsContext gc = backgroundImageCanvas.getGraphicsContext2D();

    Sprite BackgroundImageSprite = new Sprite();
    BackgroundImageSprite.setImage("images/backgroundLight.png");
    BackgroundImageSprite.setPosition(0, 0);

    this.getChildren().clear();
    BackgroundImageSprite.render(gc);
    this.backMenuButton.setFont(theFontSmall);
    this.backMenuButton.setTranslateY(0);
    this.backMenuButton.setTranslateX(0);
    this.soundOffOnButton.setTranslateX(720); // tekstiga nupu asukoht
    // this.soundOffOnButton.setTranslateX(753); //kui nupul pole teksti
    this.soundOffOnButton.setTranslateY(0);
    this.getChildren().addAll(backgroundImageCanvas, pane, backMenuButton, soundOffOnButton);
    this.showBackButton();
  }
Esempio n. 2
0
  // Konstruktor mängu objekti loomiseks
  public GameView() {
    this.setHeight(600);
    this.setWidth(800);
    this.currentLevel = startingLevel;

    // kasutajad, loome kasutades collision box muutujate modifitseerimisega konstruktorit
    user1.setImage("images/kasutaja1sinine.png");
    user1.setPosition(200, 355);
    user1.setMoveLeft("Q");
    user1.setMoveRight("W");

    user2.setImage("images/kasutaja2punane.png");
    user2.setPosition(600, 365);
    user2.setMoveLeft("LEFT");
    user2.setMoveRight("RIGHT");

    this.setOnKeyPressed(
        e -> {
          String code = e.getCode().toString();
          if (!input.contains(code)) input.add(code);
        });

    this.setOnKeyReleased(
        e -> {
          String code = e.getCode().toString();
          input.remove(code);
        });

    Canvas canvas = new Canvas(800, 600);
    GraphicsContext gc = canvas.getGraphicsContext2D();
    this.getChildren().add(canvas);

    gc.setFont(theFont);
    gc.setLineWidth(1);

    // Mänguekraanil olevad kujutised ja pildid
    Sprite skyBackgroundSprite = new Sprite();
    skyBackgroundSprite.setImage("images/taevas.png");
    skyBackgroundSprite.setPosition(0, 0);

    Sprite grassSprite = new Sprite();
    grassSprite.setImage("images/grass2.png");
    grassSprite.setPosition(0, 540);
    LongValue lastNanoTime = new LongValue(System.nanoTime());

    // See on toiduloopija, mis lisav toitu meie foodList-i
    // timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 600);
    // timer.scheduleAtFixedRate(new HealthPotionFallingTask(), 5000, 10000);

    animationTimer =
        new AnimationTimer() {
          public void handle(long currentNanoTime) {
            // calculate time since last update.
            gc.clearRect(0, 0, 800, 600);
            skyBackgroundSprite.render(gc);

            double elapsedTime = (currentNanoTime - lastNanoTime.value) / 1000000000.0;
            lastNanoTime.value = currentNanoTime;

            // anname kasutajatele kiirust vastavalt vajutatud klahvidele
            user1.setVelocity(0, 0);
            if (input.contains(user1.getMoveLeft()) && user1.getPositionX() > -80)
              user1.addVelocity(-150, 0);
            if (input.contains(user1.getMoveRight()) && user1.getPositionX() < 750)
              user1.addVelocity(150, 0);

            user2.setVelocity(0, 0);
            if (input.contains(user2.getMoveLeft()) && user2.getPositionX() > -80)
              user2.addVelocity(-150, 0);
            if (input.contains(user2.getMoveRight()) && user2.getPositionX() < 720)
              user2.addVelocity(150, 0);

            grassSprite.render(gc);
            // liigutame kasutajaid
            user2.update(elapsedTime);
            user1.update(elapsedTime);
            user1.render(gc);
            user2.render(gc);

            // LEVELITE haldus

            if (currentLevel == 0 && usersCombinedScore > level1Threshold) {
              currentLevel++;
              timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 1000);
              fallingStuffSpeed = level1Speed;
              nextLevelAt = level2Threshold;
            } else if (currentLevel == 1 && usersCombinedScore > level2Threshold) {
              currentLevel++;
              fallingStuffSpeed = level2Speed;
              timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 1000);
              nextLevelAt = level3Threshold;
            } else if (currentLevel == 2 && usersCombinedScore > level3Threshold) {
              currentLevel++;
              fallingStuffSpeed = level3Speed;
              nextLevelAt = level4Threshold;
            } else if (currentLevel == 3 && usersCombinedScore > level4Threshold) {
              currentLevel++;
              fallingStuffSpeed = level4Speed;
              timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 1000);
              nextLevelAt = level5Threshold;
            } else if (currentLevel == 4 && usersCombinedScore > level5Threshold) {
              currentLevel++;
              fallingStuffSpeed = level5Speed;
              nextLevelAt = level6Threshold;
              timer.scheduleAtFixedRate(new HealthPotionFallingTask(), 0, 10000);
            } else if (currentLevel == 5 && usersCombinedScore > level6Threshold) {
              currentLevel++;
              fallingStuffSpeed = level6Speed;
              timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 1000);
              nextLevelAt = level7Threshold;
            } else if (currentLevel == 6 && usersCombinedScore > level7Threshold) {
              currentLevel++;
              fallingStuffSpeed = level7Speed;
              nextLevelAt = level8Threshold;
            } else if (currentLevel == 7 && usersCombinedScore > level8Threshold) {
              currentLevel++;
              fallingStuffSpeed = level8Speed;
              timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 1000);
              nextLevelAt = level9Threshold;
              timer.scheduleAtFixedRate(new HealthPotionFallingTask(), 0, 10000);
            } else if (currentLevel == 8 && usersCombinedScore > level9Threshold) {
              currentLevel++;
              fallingStuffSpeed = level9Speed;
              nextLevelAt = level10Threshold;
            } else if (currentLevel == 9 && usersCombinedScore > level10Threshold) {
              currentLevel++;
              fallingStuffSpeed = level10Speed;
              timer.scheduleAtFixedRate(new FoodThrowingTask(), 0, 500);
              timer.scheduleAtFixedRate(new HealthPotionFallingTask(), 0, 5000);
              nextLevelAt = 9999;
            }

            // kokkupõrgete avastamine ja nendele vastavad tegevused
            Iterator<Potion> potionIter = potionsList.iterator();
            while (potionIter.hasNext()) {
              try {
                Potion potionSprite = potionIter.next();
                potionSprite.fallingStuffSpeed = fallingStuffSpeed;
                potionSprite.render(gc);
                if (user1.intersects(potionSprite) || user2.intersects(potionSprite)) {
                  if (healthRemaining < healthAtStart) {
                    healthRemaining++;
                    System.out.println("Yey, you got a brand new heart");
                  }
                  setHealthoMeter();
                  potionIter.remove();
                } else if (potionSprite.intersects(grassSprite)) {
                  potionIter.remove();
                }
                potionSprite.update(elapsedTime);
              } catch (ConcurrentModificationException e) {
                System.out.println("Mitu asja sai korraga otsa. Pole erilist probleemi.");
                break;
              }
            }

            // kokkupõrgete avastamine ja nendele vastavad tegevused
            Iterator<Food> foodIter = foodList.iterator();
            while (foodIter.hasNext()) {
              try {
                Food foodSprite = foodIter.next();
                foodSprite.render(gc);
                foodSprite.fallingStuffSpeed = fallingStuffSpeed;
                if (user1.intersects(foodSprite) || user2.intersects(foodSprite)) {
                  if (foodSprite.good) {
                    usersCombinedScore += 10; // kui toit oli tervislik suurendame skoori
                    PlaySound("src/sounds/213424__taira-komori__short-pickup03.mp3");
                  } else {
                    healthRemaining--; // kui toit oli paha, vähendame elusid
                    setHealthoMeter(); // uuenda elude näitamise mõõdikut
                    PlaySound("src/sounds/249615__vincentm400__confirm.mp3");
                  }
                  foodIter.remove(); // viska toit minema
                } else if (foodSprite.intersects(grassSprite)) {
                  foodIter.remove();
                  if (foodSprite.good) {
                    usersCombinedScore--;
                    PlaySound("src/sounds/149899__animationisaac__box-crash.mp3");
                  }
                }
                foodSprite.update(elapsedTime);
              } catch (ConcurrentModificationException e) {
                System.out.println("Mitu asja sai korraga otsa. Pole erilist probleemi.");
                break;
              }
            }

            // Näita elusid, mis järgi on
            gc.drawImage(healthoMeter, 140, 8);

            // Näita levelit ja skoori mis vaja järgmiseks
            gc.setFill(Color.GREEN);
            gc.setStroke(Color.BLACK);
            String pointsText = "TASE: " + currentLevel + " (järgmine: " + nextLevelAt + " punkti)";
            gc.fillText(pointsText, 260, 36);
            gc.strokeText(pointsText, 260, 36);

            // Näita hea skoori suurust
            String goodPointsText = "PUNKTE:" + (usersCombinedScore);
            gc.setFill(Color.PURPLE);
            gc.setStroke(Color.DARKBLUE);
            gc.fillText(goodPointsText, 400, 62);
            gc.strokeText(goodPointsText, 400, 62);

            /**
             * MEGA TÄHTIS!!! Lõpetab mängu kui elud otsa saavad. Muudab m2ngLbi
             * SimpleBooleanProperty variably, millele on ehitatud kuulaja Main meetodi jaoks. Sealt
             * käivitub HighScoreide kuvamine
             */
            if (healthRemaining < 1) {
              stopGame();
              gameOver.set(true);
            }
          }
        };

    // tekitab ajastaja, mis loobib iga 0,8s tagant toitu alla
  }