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! ...

Get LibGDX Game Development By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.