Pygame Guide for Sneks


Home | GitHub | Pygame Docs


Project maintained by pygame-guide-for-sneks Hosted on GitHub Pages — Theme by mattgraham

Fonts in Pygame


In this article, we'll learn how to use custom and system fonts in Pygame.

Table of contents

Initializing Pygame

Before using fonts in pygame, keep in mind that you have to initialize pygame before using the pygame.font module. To do that, you can simply put pygame.init() or pygame.font.init() at the top of your python script.

Loading fonts in Pygame

System fonts

To load a system font in pygame, we create an instance of the pygame.font.SysFont class:
pygame.font.SysFont(name, size, bold=False, italic=False) -> Font
The class requires 2 arguments: first being the name of the font (as a string, for example: "comicsans"), and the second being the size of the font (as an integer).
You can also use the bold and italic parameters (they are booleans), which are False by default.

Example:

font = pygame.font.SysFont("comicsans", 32, bold=True)

Custom fonts (from a file)

To load a custom font in pygame, we create an instance of the pygame.font.Font class:
Font(filename, size) -> Font
Font(pathlib.Path, size) -> Font
Font(object, size) -> Font
The class requires 2 arguments: first being the file object or the name of the font file (as a string, for example: "my_font.ttf"), and the second being the size of the font (as an integer).

Example:

font = pygame.font.Font("arima.ttf", 32)

Default font

You can also use the default pygame font (which is freesansbold) as a pygame.font.Font. To do this, you can use None as the font argument, for example:
font = pygame.font.Font(None, 32)

Rendering fonts in Pygame

To render a pygame.font.Font object, we can use the pygame.font.Font.render method:

render(text, antialias, color, background=None) -> Surface
As we can see, the method requires 3 arguments (and 1 optional argument): Note: for all supported color formats, see Colors in pygame.

The method returns a pygame.Surface. Here's some docs from the pygame documentation: "This creates a new Surface with the specified text rendered on it. pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render() to create an image (Surface) of the text, then blit this image onto another Surface. The text can only be a single line: newline characters are not rendered."

Example:

import pygame
pygame.init()

screen = pygame.display.set_mode((600, 450))
clock = pygame.time.Clock()

# None for the default font
font = pygame.font.Font(None, 64)
# here we are rendering text that says "Test"
font_surface = font.render("Test", True, "white")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("black")
    # draw the text
    screen.blit(font_surface, (0, 0))

    clock.tick(30)
    pygame.display.flip()

pygame.quit()


Useful Stuffs

You can visit the pygame documentation to find the full list of functions and other information about the Pygame library.

Pygame Examples

Want to contribute to this guide or did you find a problem? Visit us here!

Created by the Pygame Guide for Sneks Organization