Display Text Pygame Tutorial – Complete Guide

In the fascinating realm of game development, one of the most crucial aspects that ensures player engagement is the art of displaying in-game text. This text serves multiple purposes – from guiding players through their journey to adding depth to the storyline. As we delve deeper into the use of Python for game development using the Pygame module, our focus in this tutorial will steer towards understanding how to display text in Pygame.

What is Display Text in Pygame?

The Pygame module in Python is a set of tools designed for video game creation. Displaying text in Pygame is a fundamental skill as it forms the base of player communication within the games. Whether it is high scores, player instructions, or engaging dialogues, displayed text forms an essential part of a player’s in-game experience.

What Purpose Does It Serve?

Without text display, a game can feel soulless and confusing. The necessity of text in a game ranges from functional to narrative aspects, including:

  • Providing instructions to the player.
  • Displaying scores, lives, or timers.
  • Adding depth to the storyline through in-game dialogues.
  • Enhancing the user interface through menus, buttons, and other elements.

Why Should I Learn It?

As a budding game developer, it is vital to possess the skill of displaying text in games. Here’s why:

  • Appropriate use of text enhances player engagement and makes your game more immersive and interactive.
  • It forms a core skill in your Python and Pygame toolkit, increasing your proficiency as a game developer.
  • Learning how to add text displays allows you to provide clear instructions and feedback to your players, thus augmenting player experience.

Your journey into mastering text displays in Pygame is about to kick-off. Stay with us as we dive right into it with some engaging code examples to get you started.

CTA Small Image




Setting up Pygame for Text Display

To start displaying text in Pygame, you first need to make sure you’ve installed pygame. If you haven’t already, you can install it with this pip command:

pip install pygame

To display text, we need a font. Pygame includes a font system, which can be initialized like this:

import pygame

This way, we have loaded all available fonts and are ready to create our first text display.

Creating Text in Pygame

Creating text objects in Pygame is a two-step process. First, we set the font type and size, then we create the actual text surface.

# Set font type and size
font = pygame.font.Font(None, 36)

# Create text surface
text = font.render('Hello, World!', True, (255, 255, 255))

In the above code, we set the font to the default (None) and size to 36. Then, we create a text surface saying ‘Hello,World!’ rendered in white (255,255,255).

Displaying Text in Pygame

With a text surface ready, we are set to display it on the screen. Here’s how to do it:

# Set up display
screen = pygame.display.set_mode((500, 500))

# Blit text surface onto display
screen.blit(text, (250, 250))

# Update display

We start by setting up the display with dimensions 500×500. Then, we place our text in the middle of the screen (250, 250) using the ‘blit’ function. Finally, we update the display to show our changes.

Changing Text Properties

You can easily customize the properties of the text such as font, size, and color:

# Set font to Comic Sans, size 50
font = pygame.font.SysFont('Comic Sans MS', 50)

# Create red text
text = font.render('Hello, Player!', True, (255, 0, 0))

In this example, we changed the font to Comic Sans, increased the size to 50, and changed the text’s color to red. You can test these customization options to create text displays fitting your unique game design.

Understanding Default Fonts vs Custom Fonts

Pygame allows the use of either default fonts like we used above (‘None’) or custom fonts. To use a custom font, it needs to be present in the same directory as your python file. Here’s how you can do it:

# Set custom font to 'YourFontName.ttf', size 30
font = pygame.font.Font('YourFontName.ttf', 30)

This way, you can infuse variety and uniqueness in your games by using a custom font.

Adding Interactivity with Changing Text

Games are all about interactivity, and changing your on-screen text based on some action adds depth. For example, consider updating the score every time the player reaches a milestone:

# Initialize score
score = 0

# Create a game loop
while game_active:
    # Score a point when player crosses milestone
    if player_crosses_milestone:
        score += 1

    # Update score text
    font = pygame.font.Font(None, 36)
    score_text = font.render("Score: " + str(score), True, (255, 255, 255))

    screen.blit(score_text, (50, 50))


In the example above, we initialize a score variable. When the player achieves the milestone, we increment the score variable, update the score_text variable, and display it on the screen.

Moving Text in Pygame

To add more dynamism to your games, Pygame lets you move the text as well. Let’s consider a simple use-case where text moves from left to right:

# Initialize text position
text_position = [0, 250]

# Create a game loop
while game_active:
    # Move text to right
    text_position[0] += 1

    # Reset position to left edge once right edge is crossed
    if text_position[0] > 500:
        text_position[0] = 0

    screen.blit(text, text_position)


In this code, we start by initializing the text_position variable. The text then moves towards the right each game loop iteration. Once the right edge of the screen is crossed, we reset the text_position, causing the text to appear to loop from right to left continuously.

Rotating Text in Pygame

To make your game text even more engaging, Pygame provides the ability to rotate text:

# Rotate text by 45 degrees
rotated_text = pygame.transform.rotate(text, 45)

screen.blit(rotated_text, (250, 250))


Here, we use the pygame.transform.rotate() function to rotate our text_surface by 45 degrees. Then, we draw the rotated text_surface to our screen and update the display.

With these capabilities unlocked, you can now experiment. Thread your text displays into your game narrative, your feedback systems, or even your game controls to create engaging and immersive game experiences. Remember, texts in games are as much about the game mechanics as about inciting emotions. Don’t shy away from pushing the limits and creating masterpieces.

Text Appearance Effects

Besides placement, color and movement, Pygame also allows for some special visual effects to make our text even more captivating. Let’s have a look at some of these:

Text with Background

We can provide a background to our text to make it more distinct:

# Create text with background
text = font.render('Hello, World!', True, (255, 255, 255), (0, 0, 255))

In this code, the fourth parameter creates a blue background (0, 0, 255) for our white text.

Text Outline

Creating an outline for our text requires us to render the same text multiple times, offsetting the position a bit each time. This gives the appearance of an outline effect:

# Create text
text = font.render('Hello, World!', True, (255, 255, 255))

# Render outline
for i in range(-3, 4):
    for j in range(-3, 4):
        screen.blit(text, (250 + i, 250 + j))

# Render centered text
screen.blit(text, (250, 250))

Here, we’re first rendering the text to the screen several times. The text is shifted a bit each time, forming an outline around our centered text.

Text Animation Effects

Animations can add a touch of liveliness to your games, and text is no exception. Here’s how you can create some text animations:

Fading Text

Fading text in and out can be achieved by gradually changing the text color’s alpha value:

# Initialize alpha value
alpha = 255

# Create a game loop
while game_active:
    # Fade out text
    if alpha > 0:
        alpha -= 1

    text = font.render('Hello, World!', True, (255, 255, 255))

    # Set text alpha

    screen.blit(text, (250, 250))


In this example, we initialize an alpha variable to 255 (which is the maximum value). We then gradually decrease the alpha value, thus giving a fade-out effect to our text.

Bouncing Text

Creating a bouncing text effect involves moving the text’s position up and down in a controlled manner:

# Initialize y direction and position
y_dir = 1
y_pos = 250

# Create a game loop
while game_active:
    # Invert direction at screen bounds
    if y_pos > 450 or y_pos < 50:
        y_dir *= -1

    # Move text
    y_pos += y_dir

    screen.blit(text, (250, y_pos))


In this bouncing text example, we dynamically change the text’s vertical position within the game loop.

Scaling Text

You can also make your text grow and shrink, much like a pulsating effect:

# Initialize scale
scale = 1.0
scale_dir = 0.01
text = font.render('Hello, World!', True, (255, 255, 255))

# Create a game loop
while game_active:
    # Invert scaling direction at scale bounds
    if scale > 2.0 or scale < 1.0:
        scale_dir *= -1

    # Scale text
    scale += scale_dir
    scaled_text = pygame.transform.scale(text, (int(text.get_width() * scale), int(text.get_height() * scale)))

    screen.blit(scaled_text, (250 - scaled_text.get_width() // 2, 250 - scaled_text.get_height() // 2))


In this example, we’re dynamically changing the scale of our text within the game loop.

These examples showcase the power of text in Pygame and its potential to captivate a player’s attention. Get creative, tweak around, and use the power of texts to bring life to your games!

Where to Go Next

From here on, your adventure into Python and Pygame’s text generation potentials can only go forward. There’s a vast terrain of possibilities that awaits you. To take your journey ahead, we recommend exploring our Python Mini-Degree at Zenva Academy.

The Python Mini-Degree is a comprehensive collection of Python courses that will take your Python skills to new heights. From the basics to advanced concepts, from algorithms and object-oriented programming, game development to app development, this mini-degree has got you covered. Put your learning to the test with hands-on projects and quizzes, guiding you from beginner to professional.

For Python enthusiasts looking to diversify their learning, feel free to browse through our collection of Python courses. With Zenva, you’ll find a friendly learning environment, accommodating both beginners and seasoned coders. Get ready to become a part of our ever-growing community, with over 1 million learners worldwide.

Keep exploring, keep refining, and continue to use your creativity to create captivating, immersive text displays in your games. This is your journey. Let’s climb higher.


Immersing yourself in the world of in-game text display opens up a plethora of opportunities to make your games more engaging and interactive. Through this guide, we ventured into the exciting aspects of displaying text in Pygame, from the basics to customized fonts, positions, animations and effects.

As game developers, the capability to employ effective text displays equips us with a powerful storytelling tool. Remember, a game without text is like a story without words. It’s time for you to take the next step. Dive into our Python Mini-Degree and get set to create games that offer compelling narratives and unforgettable adventures! Let the power of Python and Pygame be your guide to innovative game development. Happy coding!


Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Unreal, Python, Godot and more.