处理中的类无法到达另一个类

发布于 2025-01-11 20:30:40 字数 17232 浏览 2 评论 0原文

我们正在编写一个基于java的跳跃和奔跑游戏。我们有 12 个课程和一个主要草图。在类游戏中,我们描述游戏的状态以及当该游戏状态发生时应该发生什么。我们的玩家有一个问题,在我们定义的玩家类中,当玩家死亡时,游戏状态“Lose”应该发生。但似乎,班级玩家无法在班级游戏中达到该游戏状态的定义。

我们还收到错误 NullPointerException。

-----------------------class Game----------------------

class Game
{
  // Eigenschaften
  private Level1 level1;
  private Player p;
  private Level2 level2;
  private Level3 level3;

  PImage start;
  public String gameState;

  public boolean left, right, up, down, space;
  public FrameObject camera, gameWorld;

  public boolean intersectHaus;

  PImage win;
  PImage gameover;
  public Enemy [] enemies;
  public Blume [] blumen;

  PImage spring;

  public int lives;

  // Konstruktor
  Game()
  {
    size(1400, 750);
    this.p = new Player();


    this.start = loadImage("start.png");
    this.win = loadImage("win.png");
    this.gameover = loadImage("gameover.png");

    this.spring = loadImage ("level2.png");



    this.gameState = "START";
    this.level1 = new Level1(this);
    this.level2 = new Level2(this);
    this.level3 = new Level3(this);

    this.lives = 3;
  }


  // Methoden

  void display()
  {
    this.p.leben();

    switch(this.gameState)
    {
    case "START":
    println("start funktioniert");
      startGame();
      break;
    case "PLAY1":
    println("play1 funktioniert");
      playGame();
      break;
    case "WIN":
    println("win funktioniert");  
      winGame();
      break;
    case "WIN2":
      winGame2();
      break;
    case "WIN3":
      winGame3();
      break;
    case "LOSE":
    println("lose hat funktioniert");
      loseGame();
      break;
    case "PLAY2":
      playGame2();
      break;
    case "PLAY3":
      playGame3();
      break;
    }
  }


  void startGame()
  {
    image(this.start, 0, 0);
    if (mousePressed == true)
    {
      klick.play();
      gameState = "PLAY1";
    }
  }

  void playGame()
  {
    this.level1.display();
  }

  void playGame2()
  {
    this.level2.display();
  }

  void playGame3()
  {
    this.level3.display();
  }

  void winGame2()
  {
    image(this.win, 0, 0);
    if (mousePressed == true)
    {
      klick.play();

      if (mousePressed == true)
      {
        klick.play();
        gameState = "PLAY3";
      }
    }
  }
  void winGame3()
  {
    image(this.win, 0, 0);
    if (mousePressed == true)
    {
      klick.play();
      gameState = "START";
      this.level1.reset();
    }
  }

  void winGame()
  {
    image(this.win, 0, 0);
    if (mousePressed == true)
    {
      klick.play();

      if (mousePressed == true)
      {
        klick.play();
        gameState = "PLAY2";
        this.level2.reset();
      }
    }
  }

  void loseGame()
  {
    //noLoop();
    
    image(this.gameover, 0, 0);
    if (mousePressed == true)
    {
      klick.play();
      gameState = "START";
      this.level1.reset();
    }
  }
  
  public void setGameState(String state)
  {
    this.gameState = state;
  }
  
  public Player getPlayer()
  {
    return this.p;
  }
}


---------------------class Level3---------------------
class Level3
{
  private Player p;
  private Blume [] blumen;
  private Wabe [] waben;
  private GiftBlume [] giftBlumen;
  private Haus h;
  private FrameObject camera, gameWorld;
  public ImageObject backImage;
  public ImageObject backImage1;
  public ImageObject backImage2;
  PImage hintergrund;
  PImage background;
  private Game parent;

  PImage hintergrund1;
  private Enemy [] enemies;
  PImage haus;

  private int score;

  //Konstruktor

  Level3(Game parent)
  {
    size(1400, 750);
    this.parent = parent;
    this.score = 0;


    for (int i = 0; i < plattformen.length; i++)
    {
      plattformen[i] = loadImage("blume"+i+".png");
    }


    left = false;
    right = false;
    up = false;
    down = false;
    space = false;


    this.p = this.parent.getPlayer();

    this.blumen = new Blume[8];
    this.blumen[0] = new Blume(plattformen[15], 10, 550, 200, 25, "safe");
    this.blumen[1] = new Blume(plattformen[14], 700, 200, 200, 25, "safe");
    this.blumen[2] = new Blume(plattformen[13], 1100, 460, 200, 25, "safe");
    this.blumen[3] = new Blume(plattformen[12], 2000, 240, 170, 25, "safe");
    this.blumen[4] = new Blume(plattformen[17], 2500, 450, 200, 25, "safe");
    this.blumen[5] = new Blume(plattformen[15], 3700, 550, 200, 25, "safe");
    this.blumen[6] = new Blume(plattformen[14], 4000, 200, 200, 25, "safe");
    this.blumen[7] = new Blume(plattformen[13], 4100, 460, 200, 25, "safe");



    this.giftBlumen = new GiftBlume[4]; //
    this.giftBlumen [0] = new GiftBlume(1450, 420, 290, 25, "death");
    this.giftBlumen [1] = new GiftBlume(3000, 420, 290, 25, "death");
    this.giftBlumen [2] = new GiftBlume(3400, 420, 290, 25, "death");
    this.giftBlumen [3] = new GiftBlume(4800, 420, 290, 25, "death");


    this.waben = new Wabe[47];
    this.waben[0] = new Wabe(430, 100, 30, 30); //nach Blume 1
    this.waben[1] = new Wabe(430, 150, 30, 30);
    this.waben[2] = new Wabe(430, 200, 30, 30);
    this.waben[3] = new Wabe(430, 250, 30, 30);
    this.waben[4] = new Wabe(430, 300, 30, 30);

    this.waben[5] = new Wabe(960, 130, 30, 30); // nach Blume 2
    this.waben[6] = new Wabe(990, 160, 30, 30);
    this.waben[7] = new Wabe(1020, 190, 30, 30);
    this.waben[8] = new Wabe(1050, 220, 30, 30);
    this.waben[9] = new Wabe(1080, 250, 30, 30);
    this.waben[10] = new Wabe(1110, 280, 30, 30);

    this.waben[11] = new Wabe(1540, 160, 30, 30); // Blume 4
    this.waben[12] = new Wabe(1540, 100, 30, 30);
    this.waben[13] = new Wabe(1540, 220, 30, 30);

    this.waben[14] = new Wabe(2070, 160, 30, 30); // Blume 6
    this.waben[15] = new Wabe(2070, 60, 30, 30);
    this.waben[16] = new Wabe(2040, 110, 30, 30);
    this.waben[17] = new Wabe(2100, 110, 30, 30);

    this.waben[18] = new Wabe(2770, 300, 30, 30); // nach Blume 6
    this.waben[19] = new Wabe(2820, 300, 30, 30);
    this.waben[20] = new Wabe(2870, 300, 30, 30);
    this.waben[21] = new Wabe(2920, 300, 30, 30);

    this.waben[22] = new Wabe(3000, 80, 30, 30); // über Blume 7 & 8
    this.waben[23] = new Wabe(3050, 80, 30, 30);
    this.waben[24] = new Wabe(3100, 80, 30, 30);
    this.waben[25] = new Wabe(3150, 80, 30, 30);
    this.waben[26] = new Wabe(3200, 80, 30, 30);
    this.waben[27] = new Wabe(3250, 80, 30, 30);
    this.waben[28] = new Wabe(3300, 80, 30, 30);
    this.waben[29] = new Wabe(3350, 80, 30, 30);
    this.waben[30] = new Wabe(3400, 80, 30, 30);
    this.waben[31] = new Wabe(3450, 80, 30, 30);
    this.waben[32] = new Wabe(3500, 80, 30, 30);
    this.waben[33] = new Wabe(3550, 80, 30, 30);


    this.waben[34] = new Wabe(3700, 450, 30, 30); // Blume 9
    this.waben[35] = new Wabe(3750, 450, 30, 30);
    this.waben[36] = new Wabe(3800, 450, 30, 30);
    this.waben[37] = new Wabe(3850, 450, 30, 30);
    this.waben[38] = new Wabe(3900, 450, 30, 30);


    this.waben[39] = new Wabe(4400, 200, 30, 30); // nach Blume 11
    this.waben[40] = new Wabe(4450, 200, 30, 30);
    this.waben[41] = new Wabe(4500, 200, 30, 30);
    this.waben[42] = new Wabe(4550, 200, 30, 30);
    this.waben[43] = new Wabe(4600, 200, 30, 30);
    this.waben[44] = new Wabe(4650, 200, 30, 30);
    this.waben[45] = new Wabe(4600, 150, 30, 30);
    this.waben[46] = new Wabe(4600, 250, 30, 30);






    hintergrund1 = loadImage("hintergrund3.png");
    backImage2 = new ImageObject(0, 0, 1400, 750, hintergrund1);
    gameWorld = new FrameObject(0, 0, backImage2.w*4, backImage2.h);
    camera = new FrameObject(0, 0, width, height);

    this.h = new Haus();



    this.enemies = new Enemy [this.blumen.length];
    for (int i = 0; i < this.blumen.length; i++) {
      this.enemies[i] = new Enemy (this.blumen[i]);
    }

    camera.x = (gameWorld.x + gameWorld.w/2) - camera.w/2;
    camera.y = (gameWorld.y + gameWorld.h/2) - camera.h/2;
  }


  //Methoden


  void display ()
  {
    this.p.update();

    for (int i = 0; i < this.blumen.length; ++ i) // für nicht sichere Plattform
    {
      this.p.collisionSide = rectangleCollisions(this.p, this.blumen[i]);
      if (this.p.collisionSide != "none" && this.blumen[i].typeof == "death")
      {
        this.p.leben();
      }
      this.p.checkBlumen();
    }

    //Kamerabeewgung
    camera.x = floor(p.x + (p.halfWidth) - (camera.w/2));
    camera.y = floor(p.y + (p.halfHeight) - (camera.h/2));

    if (camera.x < gameWorld.x)
    {
      camera.x = gameWorld.x;
    }
    if (camera.y < gameWorld.y)
    {
      camera.y = gameWorld.y;
    }

    if (camera.x + camera.w > gameWorld.x + gameWorld.w)
    {
      camera.x = gameWorld.x + gameWorld.w - camera.w;
    }
    if (camera.y + camera.h > gameWorld.h)
    {
      camera.y = gameWorld.h - camera.h;
    }

    for (int i = 0; i < this.enemies.length; i++) {
      this.enemies[i].update();
      if (rectangleCollisionPvE(this.p, this.enemies[i]) && !this.enemies[i].dead) {

        this.enemies[i].deathJump();
      }
    }

    pushMatrix();
    translate(-camera.x, -camera.y);

    this.backImage2.display();
    this.p.display();

    for (int i = 0; i < this.blumen.length; ++i)
    {
      this.blumen[i].display();
    }

    for (int i = 0; i < this.giftBlumen.length; ++i) //
    {
      this.giftBlumen[i].display();
      if (intersectGiftBlume(this.p, this.giftBlumen[i]) == true)
      {
        this.p.lebenGift();
      }
    }

    for (int i = 0; i < waben.length; ++i)
    {
      this.waben[i].display();
      if (intersect(this.p, this.waben[i]) == true)
      {
        this.waben[i].caught();
        this.score ++;
      }
    }

    for (int i = 0; i < this.enemies.length; i++) {
      this.enemies[i].display();
    }

    for (int i = 0; i < this.haus.width/2; i ++)
      this.h.display();

    if (intersectHaus(this.p, this.h) == true)
    {

      image(this.hintergrund, 200, 200);


      this.parent.setGameState("WIN3");
    }



    popMatrix();

    fill(255);
    String s = "Score: " + score;
    textSize(20);
    text(s, 1270, 50);
    //textFont(Font1);
    textSize(20);
    text("Steuerung:", 60, 50);
    textSize(18);
    text("space - springen, a - links, d - rechts", 60, 70);

    this.p.leben();
  }



  boolean intersect(Player p, Wabe w)
  {
    float distance = dist(p.x, p.y, w.x, w.y);
    if (distance < (p.w/2 + w.w/2))
    {
      return true;
    } else
    {
      return false;
    }
  }

  boolean intersectGiftBlume(Player p, GiftBlume g) //
  {
    float distance = dist(p.x, p.y, g.x, g.y);
    if (distance < (p.w/2 + g.w/2))
    {
      return true;
    } else
    {
      return false;
    }
  }


  boolean intersectHaus(Player p, Haus h)
  {
    float distance = dist(p.x, p.y, h.x, h.y);
    if (distance < (p.w/2 + h.w/2))
    {
      return true;
    } else
    {
      return false;
    }
  }

  boolean rectangleCollisionPvE (Player r1, Enemy r2) {
    float dx = (r1.x + r1.w/2) - (r2.x + r2.w/2);
    float dy = (r1.y + r1.h/2) - (r2.y + r2.h/2);

    float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
    float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

    if (abs(dx)< combinedHalfWidths) {
      if (abs(dy)< combinedHalfHeights) {
        return true;
      }
    }
    return false;
  }


  String rectangleCollisions(Player r1, Blume r2)
  {
    if (r1.vy < 0) // ausklammern um nicht von unten durch die Blume springen
    {
      return "none";
    }

    float dx = (r1.x + r1.w/2) - (r2.x + r2.w/2);
    float dy = (r1.y + r1.h/2) - (r2.y + r2.h/2);

    float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
    float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

    if (abs(dx) < combinedHalfWidths)
    {
      if (abs(dy) < combinedHalfHeights)
      {
        float overlapX = combinedHalfWidths - abs(dx);
        float overlapY = combinedHalfHeights - abs(dy);

        if (overlapX >= overlapY)
        {
          if (dy > 0)
          {
            r1.y += overlapY;
            return "top";
          } else
          {
            r1.y -= overlapY;
            return "bottom";
          }
        } else
        {
          if (dx > 0)
          {
            r1.x += overlapX;
            return "left";
          } else
          {
            r1.x -= overlapX;
            return "right";
          }
        }
      } else
      {
        return "none";
      }
    } else
    {
      return "none";
    }
  }
}

-------------------------- class Player-----------------------
class Player
{
  // Eigenschaften
  private float w, h, x, y, vx, vy, accelerationX, accelerationY, speedLimit; // acceleration = Beschleunigung
  private int lives;

  // world values
  private float friction, bounce, gravity;

  private boolean isOnGround;
  private float jumpForce;

  private float halfWidth, halfHeight;
  private String collisionSide;

  private boolean facingRight;
  public String gameState;
  private PImage player, player2;





  // Konstruktor
  Player()
  {
    this.init();
    this.lives = 3;
    this.player = loadImage("biene.png");
    this.player2 = loadImage("biene2.png");
  }

  void init()
  {
    this.w = 90; // an Biene anpassen!
    this.h = 65;
    this.x = 50; // - w/2;
    this.y = 10; //(gameWorld.y + gameWorld.h/2) - h/2;
    this.vx = 0;
    this.vy = 0;
    this.accelerationX = 0;
    this.accelerationY = 0;
    this.speedLimit = 15;
    this.isOnGround = false;
    this.jumpForce = -15; //Höhe Sprung

    // world values
    this.friction = 0.96;
    this.bounce = -0.2;
    this.gravity = 0.3;

    this.halfWidth = w/2;
    this.halfHeight = h/2;

    this.facingRight = true;


    this.collisionSide = "";
  }

  // Methoden
  void update()
  {
    //text("Leben: "+this.lives, 1000, 500);

    // start all moves off with friction at 1
    if (left && !right)
    {
      this.accelerationX = -0.2;
      this.friction = 1;
      this.facingRight = false;
    }
    if (right && !left)
    {
      this.accelerationX = 0.2;
      this.friction = 1;
      this.facingRight = true;
    }
    if (!left && !right)
    {
      this.accelerationX = 0;
    }
    if (up && !down && isOnGround) // <--- im Fall fliegen können
    {
      this.vy = this.jumpForce;
      this.isOnGround = false;
      this.friction = 1;
    }
    if (down && !up)
    {
      this.accelerationY = 0.2;
      this.friction = 1;
    }
    if (!up && !down)
    {
      this.accelerationY = 0;
    }
    // removing impulse reintroduces friction
    if (!up && !down && !left && !right)
    {
      this.friction = 0.96;
      this.gravity = 0.3; // Geschwindigkeit des Fallens
    }

    this.vx += this.accelerationX;
    this.vy += this.accelerationY;

    // friction 1 = no friction
    this.vx *= this.friction;
    this.vy *= this.friction;

    //apply gravity
    this.vy += this.gravity;

    // correct fpr max. speeds
    if (this.vx > this.speedLimit)
    {
      this.vx = this.speedLimit;
    }
    if (this.vx < -this.speedLimit)
    {
      this.vx = -this.speedLimit;
    }
    // need to let gravity ramp it up
    if (this.vy > 3* this.speedLimit) // Geschwindigkeit des Fallens
    {
      this.vy = 3 * this.speedLimit;
    }
    if (this.vy < -this.speedLimit)
    {
      this.vy = -this.speedLimit;
    }

    if (abs(vx) < 0.2)
    {
      this.vx = 0;
    }

    if (abs(vy) < 0.2)
    {
      this.vy = 0;
    }

    // move player
    this.x += this.vx;
    this.y += this.vy;

    this.checkBoundaries();
    this.checkBlumen();

    //x = Math.max(0, Math.min(x + vx, gameWorld.w - w));
    //y = Math.max(0, Math.min(y + vy, gameWorld.h - h));
  } // Update Ende

  void checkBoundaries()
  {
    // left
    if (this.x < 0)
    {
      this.vx *= this.bounce;
      this.x = 0;
    }

    if (this.y < 0)
    {
      this.vy *= this.bounce;
      this.y = 0;
    }
  }


  void checkBlumen()
  {
    if (this.collisionSide == "bottom" && this.vy >= 0)
    {
      if (this.vy < 1)
      {
        this.isOnGround = true;
        this.vy = 0;
        println("top");
      } else
      {
        this.vy *= this.bounce/2;
      }
    } else if (this.collisionSide == "top" && this.vy <= 0)
    {
      this.vy = 0;
    } else if (this.collisionSide == "right" && this.vx >= 0)
    {
      this.vx = 0;
    } else if (this.collisionSide == "left" && this.vx <= 0)
    {
      this.vx = 0;
    }
    if (this.collisionSide != "bottom" && this.vy > 0)
    {
      this.isOnGround = false;
    }
  }

  void lebenGift()
  {
    this.init();
    this.lives --;
    if (this.lives <1)
    {
      gameState = "LOSE";
    }
  }

  void leben()
  {
    text("Leben: "+this.lives, 1270, 100);
    if (this.y + this.h > height)
    {
      this.init();
      this.lives--;
      if (this.lives <1)
      {
        gameState = "LOSE";
      }
    }
  }


  void display()
  {
    fill(0);
    //rect(x, y, w, h);
    if (facingRight)
    {
      image(this.player, x, y - 40);
    } else
    {
      image(this.player2, x, y - 40);
    }
  }
}

 

We are programming a jump and run game in processing which is java based. We have 12 classes and a main sketch. In the class game we describe the states of the game and what is supposed to happen when that game state happens. There is a problem with our player, in the class player we defined when the player dies and that the state game-state Lose is supposed to happen. But it seems, that the class player can't reach the definition of that game-state in the class game.

We are also getting the error NullPointerException.

-----------------------class Game----------------------

class Game
{
  // Eigenschaften
  private Level1 level1;
  private Player p;
  private Level2 level2;
  private Level3 level3;

  PImage start;
  public String gameState;

  public boolean left, right, up, down, space;
  public FrameObject camera, gameWorld;

  public boolean intersectHaus;

  PImage win;
  PImage gameover;
  public Enemy [] enemies;
  public Blume [] blumen;

  PImage spring;

  public int lives;

  // Konstruktor
  Game()
  {
    size(1400, 750);
    this.p = new Player();


    this.start = loadImage("start.png");
    this.win = loadImage("win.png");
    this.gameover = loadImage("gameover.png");

    this.spring = loadImage ("level2.png");



    this.gameState = "START";
    this.level1 = new Level1(this);
    this.level2 = new Level2(this);
    this.level3 = new Level3(this);

    this.lives = 3;
  }


  // Methoden

  void display()
  {
    this.p.leben();

    switch(this.gameState)
    {
    case "START":
    println("start funktioniert");
      startGame();
      break;
    case "PLAY1":
    println("play1 funktioniert");
      playGame();
      break;
    case "WIN":
    println("win funktioniert");  
      winGame();
      break;
    case "WIN2":
      winGame2();
      break;
    case "WIN3":
      winGame3();
      break;
    case "LOSE":
    println("lose hat funktioniert");
      loseGame();
      break;
    case "PLAY2":
      playGame2();
      break;
    case "PLAY3":
      playGame3();
      break;
    }
  }


  void startGame()
  {
    image(this.start, 0, 0);
    if (mousePressed == true)
    {
      klick.play();
      gameState = "PLAY1";
    }
  }

  void playGame()
  {
    this.level1.display();
  }

  void playGame2()
  {
    this.level2.display();
  }

  void playGame3()
  {
    this.level3.display();
  }

  void winGame2()
  {
    image(this.win, 0, 0);
    if (mousePressed == true)
    {
      klick.play();

      if (mousePressed == true)
      {
        klick.play();
        gameState = "PLAY3";
      }
    }
  }
  void winGame3()
  {
    image(this.win, 0, 0);
    if (mousePressed == true)
    {
      klick.play();
      gameState = "START";
      this.level1.reset();
    }
  }

  void winGame()
  {
    image(this.win, 0, 0);
    if (mousePressed == true)
    {
      klick.play();

      if (mousePressed == true)
      {
        klick.play();
        gameState = "PLAY2";
        this.level2.reset();
      }
    }
  }

  void loseGame()
  {
    //noLoop();
    
    image(this.gameover, 0, 0);
    if (mousePressed == true)
    {
      klick.play();
      gameState = "START";
      this.level1.reset();
    }
  }
  
  public void setGameState(String state)
  {
    this.gameState = state;
  }
  
  public Player getPlayer()
  {
    return this.p;
  }
}


---------------------class Level3---------------------
class Level3
{
  private Player p;
  private Blume [] blumen;
  private Wabe [] waben;
  private GiftBlume [] giftBlumen;
  private Haus h;
  private FrameObject camera, gameWorld;
  public ImageObject backImage;
  public ImageObject backImage1;
  public ImageObject backImage2;
  PImage hintergrund;
  PImage background;
  private Game parent;

  PImage hintergrund1;
  private Enemy [] enemies;
  PImage haus;

  private int score;

  //Konstruktor

  Level3(Game parent)
  {
    size(1400, 750);
    this.parent = parent;
    this.score = 0;


    for (int i = 0; i < plattformen.length; i++)
    {
      plattformen[i] = loadImage("blume"+i+".png");
    }


    left = false;
    right = false;
    up = false;
    down = false;
    space = false;


    this.p = this.parent.getPlayer();

    this.blumen = new Blume[8];
    this.blumen[0] = new Blume(plattformen[15], 10, 550, 200, 25, "safe");
    this.blumen[1] = new Blume(plattformen[14], 700, 200, 200, 25, "safe");
    this.blumen[2] = new Blume(plattformen[13], 1100, 460, 200, 25, "safe");
    this.blumen[3] = new Blume(plattformen[12], 2000, 240, 170, 25, "safe");
    this.blumen[4] = new Blume(plattformen[17], 2500, 450, 200, 25, "safe");
    this.blumen[5] = new Blume(plattformen[15], 3700, 550, 200, 25, "safe");
    this.blumen[6] = new Blume(plattformen[14], 4000, 200, 200, 25, "safe");
    this.blumen[7] = new Blume(plattformen[13], 4100, 460, 200, 25, "safe");



    this.giftBlumen = new GiftBlume[4]; //
    this.giftBlumen [0] = new GiftBlume(1450, 420, 290, 25, "death");
    this.giftBlumen [1] = new GiftBlume(3000, 420, 290, 25, "death");
    this.giftBlumen [2] = new GiftBlume(3400, 420, 290, 25, "death");
    this.giftBlumen [3] = new GiftBlume(4800, 420, 290, 25, "death");


    this.waben = new Wabe[47];
    this.waben[0] = new Wabe(430, 100, 30, 30); //nach Blume 1
    this.waben[1] = new Wabe(430, 150, 30, 30);
    this.waben[2] = new Wabe(430, 200, 30, 30);
    this.waben[3] = new Wabe(430, 250, 30, 30);
    this.waben[4] = new Wabe(430, 300, 30, 30);

    this.waben[5] = new Wabe(960, 130, 30, 30); // nach Blume 2
    this.waben[6] = new Wabe(990, 160, 30, 30);
    this.waben[7] = new Wabe(1020, 190, 30, 30);
    this.waben[8] = new Wabe(1050, 220, 30, 30);
    this.waben[9] = new Wabe(1080, 250, 30, 30);
    this.waben[10] = new Wabe(1110, 280, 30, 30);

    this.waben[11] = new Wabe(1540, 160, 30, 30); // Blume 4
    this.waben[12] = new Wabe(1540, 100, 30, 30);
    this.waben[13] = new Wabe(1540, 220, 30, 30);

    this.waben[14] = new Wabe(2070, 160, 30, 30); // Blume 6
    this.waben[15] = new Wabe(2070, 60, 30, 30);
    this.waben[16] = new Wabe(2040, 110, 30, 30);
    this.waben[17] = new Wabe(2100, 110, 30, 30);

    this.waben[18] = new Wabe(2770, 300, 30, 30); // nach Blume 6
    this.waben[19] = new Wabe(2820, 300, 30, 30);
    this.waben[20] = new Wabe(2870, 300, 30, 30);
    this.waben[21] = new Wabe(2920, 300, 30, 30);

    this.waben[22] = new Wabe(3000, 80, 30, 30); // über Blume 7 & 8
    this.waben[23] = new Wabe(3050, 80, 30, 30);
    this.waben[24] = new Wabe(3100, 80, 30, 30);
    this.waben[25] = new Wabe(3150, 80, 30, 30);
    this.waben[26] = new Wabe(3200, 80, 30, 30);
    this.waben[27] = new Wabe(3250, 80, 30, 30);
    this.waben[28] = new Wabe(3300, 80, 30, 30);
    this.waben[29] = new Wabe(3350, 80, 30, 30);
    this.waben[30] = new Wabe(3400, 80, 30, 30);
    this.waben[31] = new Wabe(3450, 80, 30, 30);
    this.waben[32] = new Wabe(3500, 80, 30, 30);
    this.waben[33] = new Wabe(3550, 80, 30, 30);


    this.waben[34] = new Wabe(3700, 450, 30, 30); // Blume 9
    this.waben[35] = new Wabe(3750, 450, 30, 30);
    this.waben[36] = new Wabe(3800, 450, 30, 30);
    this.waben[37] = new Wabe(3850, 450, 30, 30);
    this.waben[38] = new Wabe(3900, 450, 30, 30);


    this.waben[39] = new Wabe(4400, 200, 30, 30); // nach Blume 11
    this.waben[40] = new Wabe(4450, 200, 30, 30);
    this.waben[41] = new Wabe(4500, 200, 30, 30);
    this.waben[42] = new Wabe(4550, 200, 30, 30);
    this.waben[43] = new Wabe(4600, 200, 30, 30);
    this.waben[44] = new Wabe(4650, 200, 30, 30);
    this.waben[45] = new Wabe(4600, 150, 30, 30);
    this.waben[46] = new Wabe(4600, 250, 30, 30);






    hintergrund1 = loadImage("hintergrund3.png");
    backImage2 = new ImageObject(0, 0, 1400, 750, hintergrund1);
    gameWorld = new FrameObject(0, 0, backImage2.w*4, backImage2.h);
    camera = new FrameObject(0, 0, width, height);

    this.h = new Haus();



    this.enemies = new Enemy [this.blumen.length];
    for (int i = 0; i < this.blumen.length; i++) {
      this.enemies[i] = new Enemy (this.blumen[i]);
    }

    camera.x = (gameWorld.x + gameWorld.w/2) - camera.w/2;
    camera.y = (gameWorld.y + gameWorld.h/2) - camera.h/2;
  }


  //Methoden


  void display ()
  {
    this.p.update();

    for (int i = 0; i < this.blumen.length; ++ i) // für nicht sichere Plattform
    {
      this.p.collisionSide = rectangleCollisions(this.p, this.blumen[i]);
      if (this.p.collisionSide != "none" && this.blumen[i].typeof == "death")
      {
        this.p.leben();
      }
      this.p.checkBlumen();
    }

    //Kamerabeewgung
    camera.x = floor(p.x + (p.halfWidth) - (camera.w/2));
    camera.y = floor(p.y + (p.halfHeight) - (camera.h/2));

    if (camera.x < gameWorld.x)
    {
      camera.x = gameWorld.x;
    }
    if (camera.y < gameWorld.y)
    {
      camera.y = gameWorld.y;
    }

    if (camera.x + camera.w > gameWorld.x + gameWorld.w)
    {
      camera.x = gameWorld.x + gameWorld.w - camera.w;
    }
    if (camera.y + camera.h > gameWorld.h)
    {
      camera.y = gameWorld.h - camera.h;
    }

    for (int i = 0; i < this.enemies.length; i++) {
      this.enemies[i].update();
      if (rectangleCollisionPvE(this.p, this.enemies[i]) && !this.enemies[i].dead) {

        this.enemies[i].deathJump();
      }
    }

    pushMatrix();
    translate(-camera.x, -camera.y);

    this.backImage2.display();
    this.p.display();

    for (int i = 0; i < this.blumen.length; ++i)
    {
      this.blumen[i].display();
    }

    for (int i = 0; i < this.giftBlumen.length; ++i) //
    {
      this.giftBlumen[i].display();
      if (intersectGiftBlume(this.p, this.giftBlumen[i]) == true)
      {
        this.p.lebenGift();
      }
    }

    for (int i = 0; i < waben.length; ++i)
    {
      this.waben[i].display();
      if (intersect(this.p, this.waben[i]) == true)
      {
        this.waben[i].caught();
        this.score ++;
      }
    }

    for (int i = 0; i < this.enemies.length; i++) {
      this.enemies[i].display();
    }

    for (int i = 0; i < this.haus.width/2; i ++)
      this.h.display();

    if (intersectHaus(this.p, this.h) == true)
    {

      image(this.hintergrund, 200, 200);


      this.parent.setGameState("WIN3");
    }



    popMatrix();

    fill(255);
    String s = "Score: " + score;
    textSize(20);
    text(s, 1270, 50);
    //textFont(Font1);
    textSize(20);
    text("Steuerung:", 60, 50);
    textSize(18);
    text("space - springen, a - links, d - rechts", 60, 70);

    this.p.leben();
  }



  boolean intersect(Player p, Wabe w)
  {
    float distance = dist(p.x, p.y, w.x, w.y);
    if (distance < (p.w/2 + w.w/2))
    {
      return true;
    } else
    {
      return false;
    }
  }

  boolean intersectGiftBlume(Player p, GiftBlume g) //
  {
    float distance = dist(p.x, p.y, g.x, g.y);
    if (distance < (p.w/2 + g.w/2))
    {
      return true;
    } else
    {
      return false;
    }
  }


  boolean intersectHaus(Player p, Haus h)
  {
    float distance = dist(p.x, p.y, h.x, h.y);
    if (distance < (p.w/2 + h.w/2))
    {
      return true;
    } else
    {
      return false;
    }
  }

  boolean rectangleCollisionPvE (Player r1, Enemy r2) {
    float dx = (r1.x + r1.w/2) - (r2.x + r2.w/2);
    float dy = (r1.y + r1.h/2) - (r2.y + r2.h/2);

    float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
    float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

    if (abs(dx)< combinedHalfWidths) {
      if (abs(dy)< combinedHalfHeights) {
        return true;
      }
    }
    return false;
  }


  String rectangleCollisions(Player r1, Blume r2)
  {
    if (r1.vy < 0) // ausklammern um nicht von unten durch die Blume springen
    {
      return "none";
    }

    float dx = (r1.x + r1.w/2) - (r2.x + r2.w/2);
    float dy = (r1.y + r1.h/2) - (r2.y + r2.h/2);

    float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
    float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

    if (abs(dx) < combinedHalfWidths)
    {
      if (abs(dy) < combinedHalfHeights)
      {
        float overlapX = combinedHalfWidths - abs(dx);
        float overlapY = combinedHalfHeights - abs(dy);

        if (overlapX >= overlapY)
        {
          if (dy > 0)
          {
            r1.y += overlapY;
            return "top";
          } else
          {
            r1.y -= overlapY;
            return "bottom";
          }
        } else
        {
          if (dx > 0)
          {
            r1.x += overlapX;
            return "left";
          } else
          {
            r1.x -= overlapX;
            return "right";
          }
        }
      } else
      {
        return "none";
      }
    } else
    {
      return "none";
    }
  }
}

-------------------------- class Player-----------------------
class Player
{
  // Eigenschaften
  private float w, h, x, y, vx, vy, accelerationX, accelerationY, speedLimit; // acceleration = Beschleunigung
  private int lives;

  // world values
  private float friction, bounce, gravity;

  private boolean isOnGround;
  private float jumpForce;

  private float halfWidth, halfHeight;
  private String collisionSide;

  private boolean facingRight;
  public String gameState;
  private PImage player, player2;





  // Konstruktor
  Player()
  {
    this.init();
    this.lives = 3;
    this.player = loadImage("biene.png");
    this.player2 = loadImage("biene2.png");
  }

  void init()
  {
    this.w = 90; // an Biene anpassen!
    this.h = 65;
    this.x = 50; // - w/2;
    this.y = 10; //(gameWorld.y + gameWorld.h/2) - h/2;
    this.vx = 0;
    this.vy = 0;
    this.accelerationX = 0;
    this.accelerationY = 0;
    this.speedLimit = 15;
    this.isOnGround = false;
    this.jumpForce = -15; //Höhe Sprung

    // world values
    this.friction = 0.96;
    this.bounce = -0.2;
    this.gravity = 0.3;

    this.halfWidth = w/2;
    this.halfHeight = h/2;

    this.facingRight = true;


    this.collisionSide = "";
  }

  // Methoden
  void update()
  {
    //text("Leben: "+this.lives, 1000, 500);

    // start all moves off with friction at 1
    if (left && !right)
    {
      this.accelerationX = -0.2;
      this.friction = 1;
      this.facingRight = false;
    }
    if (right && !left)
    {
      this.accelerationX = 0.2;
      this.friction = 1;
      this.facingRight = true;
    }
    if (!left && !right)
    {
      this.accelerationX = 0;
    }
    if (up && !down && isOnGround) // <--- im Fall fliegen können
    {
      this.vy = this.jumpForce;
      this.isOnGround = false;
      this.friction = 1;
    }
    if (down && !up)
    {
      this.accelerationY = 0.2;
      this.friction = 1;
    }
    if (!up && !down)
    {
      this.accelerationY = 0;
    }
    // removing impulse reintroduces friction
    if (!up && !down && !left && !right)
    {
      this.friction = 0.96;
      this.gravity = 0.3; // Geschwindigkeit des Fallens
    }

    this.vx += this.accelerationX;
    this.vy += this.accelerationY;

    // friction 1 = no friction
    this.vx *= this.friction;
    this.vy *= this.friction;

    //apply gravity
    this.vy += this.gravity;

    // correct fpr max. speeds
    if (this.vx > this.speedLimit)
    {
      this.vx = this.speedLimit;
    }
    if (this.vx < -this.speedLimit)
    {
      this.vx = -this.speedLimit;
    }
    // need to let gravity ramp it up
    if (this.vy > 3* this.speedLimit) // Geschwindigkeit des Fallens
    {
      this.vy = 3 * this.speedLimit;
    }
    if (this.vy < -this.speedLimit)
    {
      this.vy = -this.speedLimit;
    }

    if (abs(vx) < 0.2)
    {
      this.vx = 0;
    }

    if (abs(vy) < 0.2)
    {
      this.vy = 0;
    }

    // move player
    this.x += this.vx;
    this.y += this.vy;

    this.checkBoundaries();
    this.checkBlumen();

    //x = Math.max(0, Math.min(x + vx, gameWorld.w - w));
    //y = Math.max(0, Math.min(y + vy, gameWorld.h - h));
  } // Update Ende

  void checkBoundaries()
  {
    // left
    if (this.x < 0)
    {
      this.vx *= this.bounce;
      this.x = 0;
    }

    if (this.y < 0)
    {
      this.vy *= this.bounce;
      this.y = 0;
    }
  }


  void checkBlumen()
  {
    if (this.collisionSide == "bottom" && this.vy >= 0)
    {
      if (this.vy < 1)
      {
        this.isOnGround = true;
        this.vy = 0;
        println("top");
      } else
      {
        this.vy *= this.bounce/2;
      }
    } else if (this.collisionSide == "top" && this.vy <= 0)
    {
      this.vy = 0;
    } else if (this.collisionSide == "right" && this.vx >= 0)
    {
      this.vx = 0;
    } else if (this.collisionSide == "left" && this.vx <= 0)
    {
      this.vx = 0;
    }
    if (this.collisionSide != "bottom" && this.vy > 0)
    {
      this.isOnGround = false;
    }
  }

  void lebenGift()
  {
    this.init();
    this.lives --;
    if (this.lives <1)
    {
      gameState = "LOSE";
    }
  }

  void leben()
  {
    text("Leben: "+this.lives, 1270, 100);
    if (this.y + this.h > height)
    {
      this.init();
      this.lives--;
      if (this.lives <1)
      {
        gameState = "LOSE";
      }
    }
  }


  void display()
  {
    fill(0);
    //rect(x, y, w, h);
    if (facingRight)
    {
      image(this.player, x, y - 40);
    } else
    {
      image(this.player2, x, y - 40);
    }
  }
}

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文