Game Development Reference
In-Depth Information
17 - Graphics and Animation
their RGB values.
Fonts, and the pygame.font.SysFont() Function
18. # set up fonts
19. basicFont = pygame.font.SysFont(None, 48)
A
font
is a complete set of letters,
numbers, symbols, and characters of
a single style. Here is an example of
the same sentence printed in different
fonts:
In our earlier games, we only told
Python to print out text. The color,
size, and font that was used to display
this text was completely determined
by whatever font your operating
system uses for console windows. Our programs could not change the font at all.
However, since we will be drawing out letters to a GUI window we need to tell Pygame
exactly what font to use when drawing the text.
Figure 17-3: Examples of different fonts.
On line 19 we create a
pygame.font.Font
object (which we will just call
Font
objects for short) by calling the
pygame.font.SysFont()
function. The first
parameter is the name of the font, but we will pass the
None
value to use the default
system font. The second parameter will be the size of the font. In our call on line 19, we
want the font size to be 48 points.
The
render()
Method for
Font
Objects
21. # set up the text
22. text = basicFont.render('Hello world!', True, WHITE,
BLUE)
23. textRect = text.get_rect()
The
Font
object that we have stored in the
basicFont
variable has a method called
render()
. This method will create a
Surface
object with the text drawn on it. The
first parameter to
render()
is the string of the text to draw. The second parameter is a
boolean for whether or not we want anti-aliasing. Anti-aliasing is a technique for making a
drawing look less blocky. On line 22, we pass
True
to say we want to use anti-aliasing.
Figure 17-4 is an example of what a line (when we enlarge the individual pixels) looks
like with and without anti-aliasing: