Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What fonts can I use with pygame.font.Font?

Tags:

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

like image 773
Edu Grando Avatar asked Jun 23 '16 21:06

Edu Grando


1 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).

like image 55
Isa Avatar answered Sep 20 '22 13:09

Isa