O'Reilly logo

LibGDX Cross-Platform Development Blueprints by Indraneel Potnis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding game logic

Here, we will add some logic to the game such as searching for doors that have goats behind them and the addition of game states for easy management of the code.

Finding doors with goats

First of all, we need to declare a Boolean variable in our Door class called isGoat so that we can determine whether a goat is behind the door:

  public boolean isGoat = false; // indicates whether a goat is behind the door
  public Vector2 position =new Vector2(); // position of the door
  //door dimensions
  public float height;

Next, we need to set this variable for each door appropriately in the initDoors() method of the GameManager class:

  doors.get(0).openSprite.setRegion(goatTexture);
  doors.get(0).isGoat= true; doors.get(1).openSprite.setRegion(carTexture); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required