This tutorial demonstrates how to use Font and Text in Pygame.
No game is complete without the addition of font or text. Whether it’s a simple “Game Over” message or dialogue between several characters, Font and Text plays an important role in any Pygame application.
Fonts use the file .ttf, which stands for True Type File.
Font and Text
There are generally two different ways to use fonts in pygame.
pygame.font.SysFont(). The difference between the two of them is that
pygame.font.Font() requires the file path for a font to be passed into it’s parameters whereas
pygame.font.SysFont() just requires the name of the font. You’ll understand where to use which in the examples shown below.
pygame.font.Font() is used mainly when you’ve included an actual
.ttf Font file with your code. For example, if you have a
.ttf file for the font arial in the same directory as your python file, you can use it with the following code.
The first parameter is the file path, and the second is the font size.
The obvious benefit of this is that there is no chance of the font you’ve selected not being available.
Another thing you can do is simply to make the your program use the system default font from the beginning. This method is 100% error free and not prone to any “missing font” problems.
font = pygame.font.Font(None, size)
Just remember, the system default font will vary from system to system.
There might be cases where the font-type you specify was not found, or some error occurred. In cases like these, pygame will fallback to the default system font instead.
If you’re not looking to be including any ttf files in your code, we then turn to using
pygame.font.SysFont(). This is the method I personally recommend and use in my own pygame programs.
A good strategy is to first find the fonts supported by the system the code is executing on. The
pygame.font.get_fonts() function will return a list of all the names of the fonts it can find on your system. We ran the code on our windows desktop and the following output was received. (We only included about 10 lines for readability, there were 52 in total)
['arial', 'arialblack', 'bahnschrift', 'calibri', 'cambriacambriamath', 'cambria', 'candara', 'comicsansms', 'consolas', 'constantia', 'corbel', 'couriernew', 'ebrima', 'franklingothicmedium', 'gabriola', 'gadugi', 'georgia', 'impact', 'inkfree', 'javanesetext', 'leelawadeeui', 'leelawadeeuisemilight', 'lucidaconsole', 'lucidasans', 'malgungothic', 'malgungothicsemilight', 'microsofthimalaya', 'microsoftjhengheimicrosoftjhengheiui', 'microsoftjhengheimicrosoftjhengheiuibold', 'microsoftjhengheimicrosoftjhengheiuilight', 'microsoftnewtailue', ....
It looks like a lot, but certain system have less than a dozen system fonts available to them. Now that we have these font names in front of us, we can simply pick one and pass it into the
dialogue_font = pygame.font.SysFont('arial', 15) name_font = pygame.font.SysFont('Helvetica', 20) game_over_font = pygame.font.SysFont('Verdana', 60)
SysFont() function only requires the name of the font, not the file path. For this reason the ttf extension is not included. The second parameter remains the same however, representing font size.
As you can see, there we’ve created several types of fonts. The reason being that in an average game, there are many different types of fonts you’ll be using for different parts of the text. It would be highly unusual to have the same type of the font family and size throughout your game.
For instance, we’ve created a font type for dialogue using a rather small font size. For the “Game Over” text that many games have, we’ve used a much larger font size. We’ve even created a different font for names, with it’s font size being a little larger than text (to make it stand out a bit).
dialogue = dialogue_font.render("Hey there, Beautiful weather today!", True, (0,0,0)) name = name_font.render("John Hubbard", True, (0,0,255)) game_over = game_over_font.render("Game Over", True, (255,0,0))
Defining the font is only the first step. Next up you actually have to render your chosen font and create a surface object. You also get to decide the color while rendering. The True parameter stands for anti-aliasing, used to make the edges smoother. Pass True if you want to use it, otherwise pass False.
screen.blit(dialogue, (40,40)) screen.blit(name, (40,140)) screen.blit(game_over, (40,240))
The final step is to actually display the object onscreen. For this purpose, we use the
surface.blit() function. It takes two parameters, first a surface object and second a pair of co-ordinates to draw the surface object to.
Pygame Fonts Example
Now we’ll actually put together all the code shown above into one comprehensive program and show you the output.
The code for our program.
import pygame pygame.init() screen = pygame.display.set_mode((400, 400)) clock = pygame.time.Clock() dialogue_font = pygame.font.SysFont('arial', 15) name_font = pygame.font.SysFont('Helvetica', 20) game_over_font = pygame.font.SysFont('Verdana', 60) dialogue = dialogue_font.render("Hey there, Beautfiul weather today!", True, (0,0,0)) name = name_font.render("John Hubbard", True, (0,0,255)) game_over = game_over_font.render("Game Over", True, (255,0,0)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.fill((255, 255, 255)) screen.blit(dialogue, (40,40)) screen.blit(name, (40,140)) screen.blit(game_over, (40,240)) pygame.display.flip() clock.tick(60)
Below is the output of the below code.
You can be a little more organized and efficient by creating tuples with RGB values beforehand, stored in variables.
color_light = (170,170,170) color_dark = (100,100,100) color_white = (255,255,255)
Now all you have to do is write the name of the color (variable) you want in the appropriate place, and you’re done.
Be sure to check out our Game creation with Pygame Tutorial as well!
This marks the end of the Pygame Font and Text article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article material, can be asked in the comments section below.