Creating a Simple Game

Before you can display and update scores in Pygame, you’ll first need to create a game to work with. In this tutorial, you will create a simple game where a player can move left or right. But before that, you should have pip installed on your device. After installing pip, run the below command to install the pygame module:

To get started, import the necessary modules and set up the basic structure of the game:

With this skeleton code your basic game is playable. Press left or right to move the player character, represented by the red square.

Displaying and Updating Game Score

Now that you have a basic game set up, add a scoring system and display the score on the screen.

Setting Up Pygame for Text Rendering

To display text in Pygame, we first need to import the necessary modules and initialize the font module. To do this, use the following code at the top of your script:

The pygame.font module lets you use Pygame’s text rendering capabilities, and the pygame.init() function initializes the font module.

Creating a Basic Scoring System

Next, create a basic scoring system for your game. To do so, declare and initialize two variables: score and score_increment. The score variable will keep track of the player’s current score, and the score_increment variable will specify how much the score should increase each time the player touches an obstacle.

Add the following code to the top of your script, below the imports:

Next, update the score as the game progresses. In the game loop, you can check if the player character is colliding with the obstacle. If they are, increment the score by the amount specified in the score_increment variable.

Update the game loop to include the following code:

This code checks if the player character is colliding with the obstacle. If they are, it increments the score by the specified amount.

Displaying the Score on the Screen With the Font Module

Now that we have a basic scoring system in place, let’s display the score on the screen using the font module.

First, let’s set up the font object by adding the following code to the top of the game loop:

This code creates a font object with a size of 36 pixels and no specified font family. You can customize the font and font size by specifying a different font file and size in the Font() function.

Next, let’s draw the score to the screen. Add the following code to the game loop, after you’ve updated the game state and filled the screen:

This code uses the render() method of the font object to create a text surface containing the score, and then blits (draws) the text surface to the screen at the position (10, 10).

Customizing the Appearance of the Score Text

Finally, customize the appearance of the score text by choosing a font and font size and changing the color of the text.

To choose a specific font and font size, pass the font file and size as arguments to the Font() function:

You can easily download different font files from the internet. After downloading, just place the font file at the root of your working directory.

To change the color of the text, pass a color tuple as the third argument to the render() method:

And that’s it! With these changes, you should now have a fully functional game score system that displays the score on the screen and updates it as the player progresses through the game. You can further customize the appearance of the score text by experimenting with different font families, sizes, and colors.

Score Tracking in Game

Below is the full working code, you can also find the code on this GitHub repo.

This code creates a window with a size of 750x450 pixels, a player character, and an obstacle. It uses a game loop to handle events, update state, and draw the game to screen. It also includes a scoring system that increments the score by ten points each time the player character collides with the obstacle, and a font object and text rendering code to display the score on the screen.

In the event handling section, the code also includes functionality to move the player character left and right using the arrow keys.

The game loop includes code to update the display and limit the frame rate to 60 FPS, ensuring that the game runs smoothly.

Overall, this code provides a basic example of how to display and update game scores in Pygame, and can be further customized and expanded upon to create more complex and engaging games.

Create Different Games Using Pygame

Pygame is a powerful and easy-to-use library for creating games in Python. With its rich set of features and capabilities, you can create a wide range of games, from simple jumpers and platformers to more complex and immersive games.

Whether you’re a beginner or an experienced game developer, Pygame is an excellent choice for creating engaging and fun games.