Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use custom fonts in pygame?

Specs:
MacOS Monterey, Python 3.10.4, Pygame 2.1.2

Context:
I’m trying to create a game using the Pygame module in Python. I want to display text on the screen and have figured that part out. What I’m not sure about is if I’m able to use custom fonts. By custom fonts I mean I’ve downloaded a font in the form of a .ttf file.

Question:
Is there any way to use this .ttf file/font in Pygame? I understand there are some built-in fonts like Comic Sans.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a custom font with the following code:

font = pygame.font.Font(<font file>, <size>)

then render it using

font.render(<text>, True, <colour>)

See also https://nerdparadise.com/programming/pygame/part5
and https://www.pygame.org/docs/ref/font.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading