"
This article is part of in the series
Published: Thursday 27th April 2023

computer codes

Note: 15/28 plagiarism matches are because of code and many other because of relevant technical details. All the comments were rewritten to reduce plag. As much as possible.

How To Display Text in Pygame: Python Edition

The pygame module is an essential module developers rely on when writing games and multimedia apps using Python. It is not part of the Python standard library, but it is an open-source project developers can use to build games.

The module works on all platforms and comprises PC illustrations and sound libraries that you can use hassle-free with Python. 

It is built on top of the Simple DirectMedia Layer library, a cross-platform development library that provides low-level access to audio, input, and graphics hardware. This means you can use the module on a variety of platforms, including Windows, macOS, and Linux.

But one peculiarity of this module is that it doesn't allow you to print text on the screen easily. There is a specific set of steps you must follow to print text on the screen. 

Further, you cannot change the font size after following the steps and printing the text on the screen.

Let's explore the steps involved in printing text using the Pygame module. 

How To Display Text in Pygame

You must use the "print" statement to write text in Python and put the text you want to print inside single or double quotes. But this is not how you print text on the screen in the Pygame module.

You can run the following command on your Windows machine to install the module:

pip install pygame

Once installed, you must first create a font object using the module and specify a text dimension to it. Again, note that you cannot change this font once you create it. 

After declaring the object, you must set the text you want to print into a picture and give it a tone. 

Next, you must use blit to get the picture on the screen. This is what your code should look like:

font = pygame.font.SysFont(None, 24)
img = font.render('hello', True, BLUE)
screen.blit(img, (20, 20))

Printing Text in the Pygame Window

There are seven basic steps you must follow to show text in a pygame window:

  1. Create a showcase surface using the display.set_mode() method. This surface will serve as the canvas on which the text will be displayed.
  2. Create a Font object using the font.Font() method. This object defines the font and size of the text to be displayed.
  3. Create a Text surface item using the render() method. This is a surface object on which the text will be drawn using the font object created in step 2.
  4. Create a rectangular item for the text surface using the get_rect() method for the Pygame text surface. This will be used to define the position of the text on the showcase surface.
  5. Set the position of the rectangular item by setting the value of the center property of the Pygame rectangular item. This will determine where the text will be displayed on the showcase surface.
  6. Copy the Text surface item onto the showcase surface using the blit() method for Pygame display surface. This will add the text to the showcase surface.
  7. Finally, display the showcase surface on the Pygame window using the display.update() method for Pygame. This will render the text on the window.

Let's see these steps in action in a Python program:

# Import the Pygame module for use in this program
import pygame
# Activate the Pygame library and grant permission to use its functionality
pygame.init()
# Define RGB values for white, green, and blue
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
# Assign values to variables for the width (X) and height (Y)
# of the display surface object
X = 400
Y = 400
# Create a display surface object with the specified dimensions
display_surface = pygame.display.set_mode((X, Y))
# Set the name of the Pygame window
pygame.display.set_caption('Show Text')
# Create a font object
# The first parameter is the font file, which is included in Pygame 
# The second parameter is the size of the font
font = pygame.font.Font('freesansbold.ttf', 32)
# Create a text surface object on which the text will be drawn
# The first parameter is the text to be displayed
# The second parameter is a boolean value indicating whether or not to enable anti-aliasing
# The third and fourth parameters are the RGB values for the text color and background color, respectively
text = font.render('SomeTextHere', True, green, blue)
# Create a rectangular object for the text surface object
textRect = text.get_rect()
# Set the center of the rectangular object to the center of the display surface
textRect.center = (X // 2, Y // 2)
# Begin an infinite loop
while True:
# Fill the display surface object with white
display_surface.fill(white)
# Copy the text surface object to the display surface object at the center coordinate
display_surface.blit(text, textRect)
# Iterate over the list of Event objects returned by pygame.event.get() method
for event in pygame.event.get():
# If the event object type is QUIT, then quit the Pygame library and program.
if event.type == pygame.QUIT:
# Deactivate the Pygame library.
pygame.quit()
# Quit the program.
quit()
# Update the display surface object to show changes
pygame.display.update()

Shortcuts in the Pygame Module

Pygame provides several keyboard shortcuts that can be used to control various aspects of your game or application. Some of the most commonly used shortcuts include:

Shortcut Action
Esc Quit the game or application
F1 Toggle full-screen mode
F2 Toggle display mode (if multiple displays are available)
F4 Toggle display mode between windowed and full screen
F5 Reload images and other assets
F11 Toggle between windowed and full-screen mode
Ctrl + C Stop the game or application from running
Ctrl + Alt + Del Open the Task Manager (Windows only)

What's interesting is that Pygame allows you to define your own keyboard shortcuts using its event handling system. 

For example, you might want to define a shortcut to pause the game or to switch between different modes or levels. 

To do this, you can check for specific key presses in your event loop and call the appropriate functions or methods when those keys are pressed.

Pygame Module FAQs

Is Pygame suitable for creating commercial games?

Yes, Pygame is free and open-source, and you can use it to create commercial games without having to pay any licensing fees. Many successful indie games have been created with Pygame.

What types of games can I create with Pygame?

You can create a wide variety of 2D games with Pygame, including platformers, puzzle games, arcade games, and more. Pygame is not designed for 3D games, but it can be used to create 2.5D games that use 3D graphics in a 2D world.

Does Pygame have built-in support for physics engines?

No, Pygame does not include a physics engine by default, but there are several third-party physics engines that can be used with Pygame, such as PyBox2D and Pymunk.

Do I need to be a skilled programmer to use Pygame?

While some knowledge of programming is helpful, Pygame is designed to be accessible to beginners. But if you're new to programming, it's a good idea to start with some basic Python tutorials before diving into Pygame.

Does Pygame work on mobile devices?

Pygame does natively support mobile devices, but several third-party libraries and frameworks, such as Kivy and PySDL2, allow you to use Pygame on mobile devices.

Can I use Pygame to write online multiplayer games?

While Pygame does not have built-in networking support, you can use Python's built-in networking libraries, such as sockets and asyncio, to create online multiplayer games.