O'Reilly logo

LibGDX Game Development By Example by James Cook

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 textures and animations

We now have our assets. Let's add them to the assets folder in our project and start adding them into the game.

First up, let's add the background (bg.png). This is a relatively simple affair; like we did earlier, we create a Texture object that represents the background, then draw it. So, in the GameScreen class, add the following code:

private Texture background;

public void show() {
  /** Code omitted for brevity **/
  background = new Texture(Gdx.files.internal("bg.png"));
}

private void draw() {
  batch.setProjectionMatrix(camera.projection);
  batch.setTransformMatrix(camera.view);
  batch.begin();
  batch.draw(background, 0, 0);
  drawScore();
  batch.end();
}

Let's run it and see how it looks:

Excellent, we are almost there! ...

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