I thought that pygame.font.Font was to load .ttf fonts, and that you can't load a font without having the .ttf file in the same directory, but I have seen a video where someone load a font without having the .ttf font in the same directory. I want to know what fonts can I use with pygame.font.Font without having the .ttf file in the same directory
-
you can use `pygame.font.SysFont` for a system font, and `None` in `SysFont` for the default one. – Jun 03 '21 at 14:04
5 Answers
There are generally two ways to use fonts in pygame: pygame.font.Font()
and pygame.font.SysFont()
. pygame.font.Font()
expects a path to a font file as its first parameter, whereas pygame.font.SysFont()
expects a string with the name of the font.
You can load any TrueType font file (*.ttf) with pygame.font.Font()
. To load the font file myfont.ttf
in your project directory simply call pygame.font.Font("myfont.ttf", size)
. Substitute the path to your file for the first parameter if you have it in a different location. You can use either a relative or an absolute file path.
Alternatively, you can use a system font by calling pygame.font.SysFont("fontname", size)
. Which system fonts you can call depends on the system on which you run this. If it cannot find the font you tried to supply, it will fallback onto the default system font. You can pass it a comma separated list of font names which will then be searched in order and the first available font will be returned. pygame.font.get_fonts()
will return a list of all the names of the fonts it can find on your system which you can then use with it.
Lastly, to load a font file that isn't a TrueType font, you can use the pygame.freetype
module which supports TTF, Type1, CFF, OpenType, SFNT, PCF, FNT, BDF, PFR and Type42 fonts. You can user either font files by calling pygame.freetype.Font()
or system fonts by calling pygame.freetype.SysFont()
similar to the font module.
So if you want to use the font 'Arial' and it is installed on your system, you can call it using pygame.font.SysFont('arial', size)
. If you don't have it installed on your system or are unsure, you may provide the path to the font file directly, e.g. pygame.font.Font("C:\Windows\Fonts\Arial.ttf",size)
.

- 751
- 5
- 6
-
it work with freesansbold.ttf but it doesn't work for arial for example. I know I can use pygame.font.SysFont, but I want to know what fonts I can use with pygame.font.Font – Edu Grando Jun 23 '16 at 21:32
-
I have expanded my previous answer, but I'm still unsure whether I understand your problem correctly. Are you confused about file types or file paths which you can use with `pygame.font.Font()`? – Isa Jun 24 '16 at 10:29
-
I've read that freesansbold.ttf is a font that comes with pygame so you don't need to have the .ttf file to load it with pygame.font.Font ; I want to know if there are more fonts that comes with pygame? – Edu Grando Jun 24 '16 at 18:19
-
5
You should first import pygame
then type print(pygame.font.get_fonts())
in your IDE in order to see all the currently available fonts. :)

- 3,889
- 2
- 22
- 41

- 241
- 2
- 2
Some findings from OS specifics perspective:
- On Windows pygame.font.get_fonts() returns large number of system fonts but on Ubuntu installed as WSL, we get only 3 fonts:
['dejavuserif', 'dejavusansmono', 'dejavusans']
. - There doesn't seem to be OS independent font names working, including
Aerial
. - If you want OS independent font, use
pygame.font.Font(pygame.font.get_default_font(), font_size)

- 63,284
- 17
- 238
- 185
The fonts available will be system dependent. However, you can get a list of all of the fonts available by calling: pygame.font.get_fonts()

- 1,481
- 14
- 16
Sadly the only one you can use in pygame is 'freesansbold.ttf'
unless you download a font from a website. So you should use that if you can't or don't want to download a font.

- 70
- 10