Adding the Title and Score

Title

In pygame to create and display some text we need to follow three steps.

First we need to create a font, then a surface with the text we want to display, and then we can display that surface using the blit method of the display surface object.

First let’s draw the game title. So, outside of the game loop, below the pygame.init() call, let’s create a font and call it title_font. title_font = pygame.font.Font() and we have to give it a font family and a size.

For the font family I will enter None so it will use the default font of the pygame module.

For the size of the font I give it the value 60. The first step is done.

Now we have to create a surface for the title inside the game loop, in the drawing part of it. To do this we use the render method from our title_font object.

title_surface = title_font.render() We pass in the string that we want to display, “Retro Snake”

we set the anti-alias argument to true and finally we set the color of the font.

I’m going to set it to DARK_GREEN. Now we can call the blit method to display the title_surface on the display_surface which we have named screen. Blit means block image transfer. Most of the time in pygame we are going to be blitting images to the display_surface

We have to pass in the surface that we want to display and the location on the screen.

So we type: screen.blit(title_surface and the position

we want it to be in a tuple, let say (OFFSET-5, 20)

That’s it! If we run the game now we can see that the game title appears.

Score

Let’s now add a score to our game. First we need to add a score attribute to the game class.

So in the init method of the game class we add: self.score = 0

The score will be increased by one every time the snake eats a food object.

Let’s go to the collision_with_food() method. Here we have to increase the score by 1.

self.score += 1

The last thing we have to do is to reset the score to 0 every time the game ends.

So, inside the game_over() method we set the score to 0

self.score = 0 Now that we have implemented the scorekeeping feature, let's display the score on the screen.

Again we are going to follow the three steps required to display a text on the screen. First we are going to create a new font object for the score below the title_font.

score_font = pygame.font.Font(None, 40:)

Now in the game loop. We need to create a surface for the score text.

score_surface = score_font.render(str(game.score), True, DARK_GREEN)

Here, we convert the game.score to a string because it is a number. Now all we have to do is to blit the score_surface,

so we type: screen.blit(score_surface, ()) and now we have to enter a position here.

For x we enter OFFSET-5 and for y OFFSET + cell_size * number_of_cells + OFFSET + 10

If we run the game now, we are going to see the score displayed on the screen. Every time the snake eats some food, the score increases, and every time the game ends, the score resets to zero. Now, let’s implement the final step of the project. Let’s add sounds to the game.