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 the apple:
private void addToScore() { score += POINTS_PER_APPLE; }
Now, we need to find a logical place to call this method. What we can do is place this method call inside the checkAppleCollision()
method call, as follows...