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:

private void checkAppleCollision() {
   if (appleAvailable && appleX == snakeX && appleY == snakeY) {
       BodyPart bodyPart = new BodyPart(snakeBody);
       bodyPart.updateBodyPosition(snakeX, snakeY);
       bodyParts.insert(0, bodyPart);
       addToScore();
       appleAvailable = false;
   }
}

Let's run the game and see what happens.

You probably have guessed, or noticed, right away that, although the code might be keeping track of the score, we have no way of feeding that back to the user.

What we can do to fix this is have the score displayed to the user as the game is being played—like a real game!

To do this, we need to update the draw() method call to draw our score out, so let's create the following method to do this:

private void drawScore() {
   if (state == STATE.PLAYING) {
      String scoreAsString = Integer.toString(score);
      BitmapFont.TextBounds scoreBounds = bitmapFont.getBounds(scoreAsString);
      bitmapFont.draw(batch, scoreAsString, (Gdx.graphics.getWidth()  - scoreBounds.width) / 2, (4 * Gdx.graphics.getHeight() / 5) - scoreBounds.height / 2);
   }
}

What we are saying here is: if the game state is playing, convert the integer score to a string object. Then, we create a TextBounds instance so we know the size of our string in terms of the font. Finally, we make the call to draw it to the screen, in the middle and slightly higher up the screen.

Next, add the method call to our original draw() method:

private void draw() {
   batch.begin();
   // Other render code ommited for brevity
   drawScore();
   batch.end();
}

If we now run the game, we should see the score on the screen, and it will go up every time we make the snake eat the apple. Excellent!

High scores

I hope you had a good play and reached a high score; however, you probably have noticed a bug in the game! Yes, that's right; we don't reset the score when we restart the game. This mean a player can grow their score indefinitely! Let's update our doRestart() method to reset the score:

private void doRestart() {
   // Other code ommited for brevity
   score = 0;
}

Now it will reset and the player will start from zero every time!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.50