A pygame font is a pygame.font.Font object at one size. There are three ways to get one:
default = pygame.font.Font(None, 36) # pygame's built-in font
system = pygame.font.SysFont("arial", 36) # a font installed on the computer
custom = pygame.font.Font("fonts/pixel.ttf", 36) # a font file you ship with the game
Make fonts once, before the game loop. Each size is a separate Font, so a title and body text need two.
Your own font file
Shipping a .ttf or .otf file with your game is the only way to know it looks the same everywhere. This example uses the pixel fonts in the art pack these docs provide:
Pixel fonts look best with antialias=False and at whole multiples of their design size. Free fonts with game-friendly licenses are on Google Fonts and on Kenney's site.
System fonts: SysFont and get_fonts()
pygame.font.SysFont(name, size, bold=False, italic=False) looks for an installed font by name. Names are lowercase with no spaces, as get_fonts() lists them:
import pygame
pygame.init()
print(pygame.font.get_fonts())
On a Windows computer the start of the list looks something like this:
['arial', 'arialblack', 'comicsansms', 'couriernew', 'georgia', 'impact', 'timesnewroman', 'verdana', ...]
The list depends on the operating system and what the person has installed, so a font you see on your machine may be missing on someone else's. SysFont never stops the game: if no name matches, it uses the default font (pygame-ce prints a warning saying so). Give it fallbacks as a comma-separated list:
font = pygame.font.SysFont("segoeui,helvetica,arial", 24)
pygame.font.match_font("arial") returns the file path a name resolves to, or None, which is a way to check before relying on it.
The examples on these pages run in your browser, where SysFont always gives the default font, since a browser game cannot read the fonts installed on your computer.
Bold, italic, underline
font = pygame.font.Font(None, 36)
font.set_bold(True)
font.set_italic(True)
font.set_underline(True)
SysFont also takes bold=True and italic=True. When a font has no bold version, pygame makes the letters thicker itself, which looks rougher than a real bold file; for the best result load the bold .ttf directly.
Font errors
FileNotFoundError for a font file means the path is wrong, for the same reason as with images: relative paths start at the working directory. Build the path from your script's folder (see loading images). pygame.error: font not initialized means pygame.init() (or pygame.font.init()) was not called before making the font.
Frequently Asked Questions
What fonts are available in pygame?
Always the built-in default, used with pygame.font.Font(None, size). System fonts depend on the computer: pygame.font.get_fonts() lists their names, and pygame.font.SysFont("arial", 24) loads one.
How do I use a custom font in pygame?
Put the .ttf or .otf file next to your game and load it with pygame.font.Font("myfont.ttf", 32). This is the reliable way to make the game look the same on every computer.
What happens if SysFont cannot find the font?
It falls back to pygame's default font: pygame-ce prints a warning (The system font ... couldn't be found) and the game keeps running. Pass a comma-separated list, such as SysFont("helvetica,arial", 24), to try several names in order.