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

High scores

One of the final pieces is still missing from our Snake game; we don't have, or keep track of, any scoring! How is the player supposed to know how well they did when the game finishes?

The first step to solve this is to add a member to the GameScreen class called score, which we will add to the following:

private int score = 0;

But how much should we add? It is completely up to you, the game designer, how many points you want to give to the player. For the sake of simplicity, let's just say 20 points per apple collected. Feel free to change this later to whatever you like. Let's create a constant member:

private static final int POINTS_PER_APPLE = 20;

Next, let's create a method that can be called every time a player makes the snake collide ...

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