Text and Fonts

Text is a vital key to games. pygame has a way to make text. It can be broken down into fonts and text.

Fonts

The first step to displaying text is knowing which font to use. pygame has support for several fonts, such as Arial, Comic Sans, Times New Roman, and more. It all depends on what fonts your system has.

To load the font, we do the following: ourfont = pygame.font.SysFont(fontname, fontsize)

<aside> 💡 You should use a positive fontsize because negative integers lead to extremely small text.

</aside>

So, if we wanted to have the font for our text be Arial and we wanted its size to be 32, then we'd do

ourfont = pygame.font.SysFont("Arial", 32)

<aside> 💡 Instead of defining a certain font, you can also just get the default font with pygame.font.get_default_font() in place of fontname

</aside>

Text

Now that we have a font, we need to have the actual text written in that font. So, we call our font's render method.

Here's how the render method works:

(font variable name).render(text: string, antialias: bool, color: tuple, background=None)