HOWTO · Pygame

How to Display Text in Pygame

Learn how to create a font, render text to a surface, and draw it in a Pygame game loop with a complete Python example.

On this page

To display text in Pygame, create a font, render a string into a surface, and draw that surface on the display with blit(). For static labels, render once before the game loop; in each frame, clear the screen, draw the text, process events, and update the display. The complete example below creates its own window, so you can adapt it directly or move its text-rendering steps into an existing game.

Create a font in Pygame

Pygame offers two common ways to create a font object. pygame.font.Font loads a font file from a path, or uses Pygame’s built-in default when the first argument is None. A custom path must point to a font file your program can access. pygame.font.SysFont looks up an installed system font by name, which is convenient when you do not want to bundle a font file. Both constructors take a size in points: pygame.font.Font(None, 36) uses the default font at 36 points, while pygame.font.SysFont("arial", 36) requests Arial if it is installed. Choose one constructor and keep its returned font object. The complete example uses the default font, so it needs no separate asset.

Render text and draw it in the game loop

Call render() on the font object with the string, an antialiasing Boolean, and a color, such as font.render("Score: 10", True, (255, 255, 255)). Setting antialiasing to True smooths the letter edges. The color is an RGB tuple whose channels range from 0 to 255. render() creates a surface; it does not put text on the window. Call screen.blit(text_surface, (x, y)) to draw that surface with its top-left corner at (x, y), then update the display.

For text that stays the same, create the rendered surface before the loop so Pygame does not render the same string on every frame. If the value changes, render a replacement surface when it changes, then keep blitting that surface each frame. To center text, use the rendered surface’s rectangle and set its center to the center of the screen. This keeps text creation separate from drawing and lets the game loop redraw the same surface efficiently.

Run a complete text-display example

This complete example initializes Pygame, creates a 640-by-360 display, renders a centered greeting once, then draws it on a dark background while processing events and updating the display. The preview closes when you exit the window or after five seconds; in a game, remove the preview timer and keep the loop active until the player exits. Static text is rendered once before the loop.

import os
import warnings

os.environ.setdefault("PYGAME_HIDE_SUPPORT_PROMPT", "1")
os.environ.setdefault("SDL_AUDIODRIVER", "dummy")
runtime_dir = os.environ.get("XDG_RUNTIME_DIR")
wayland_ready = bool(
    os.environ.get("WAYLAND_DISPLAY")
    and runtime_dir
    and os.path.isdir(runtime_dir)
    and os.access(runtime_dir, os.R_OK | os.W_OK | os.X_OK)
)
if os.name != "nt" and not wayland_ready and (
    not os.environ.get("DISPLAY")
    or os.environ.get("WAYLAND_DISPLAY")
    or not runtime_dir
    or not os.path.isdir(runtime_dir)
    or not os.access(runtime_dir, os.R_OK | os.W_OK | os.X_OK)
):
    os.environ.setdefault("SDL_VIDEODRIVER", "dummy")
warnings.filterwarnings(
    "ignore",
    message="Your system is avx2 capable.*",
    category=RuntimeWarning,
)

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 360))
pygame.display.set_caption("Text in Pygame")
font = pygame.font.Font(None, 40)
text_surface = font.render("Hello, Pygame!", True, (255, 255, 255))
text_rect = text_surface.get_rect(center=screen.get_rect().center)
clock = pygame.time.Clock()
preview_done = pygame.USEREVENT
pygame.time.set_timer(preview_done, 5000)
running = True
while running:
    for event in pygame.event.get():
        if event.type in (pygame.QUIT, preview_done):
            running = False

    screen.fill((30, 30, 30))
    screen.blit(text_surface, text_rect)
    pygame.display.flip()
    clock.tick(60)
pygame.quit()
print("Text preview finished.")
Text preview finished.