Text is a vital key to games. pygame has a way to make text. It can be broken down into fonts and text.
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)
fontname is the name of the font as a str. For example, "Arial" and "Times New Roman" are valid font names to pass as the first parameterfontsize is the size of the font. It should be an integer. For example, 32 is a valid size (2.0 isn't because it isn't an integer)<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>
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)
text is the text that you want to have displayedantialias is whether or not to antialias the textcolor is the color that the text should be (use an RGB tuple)background is the background color. It defaults to None (no background)