PyGame for Game Development: Font and Sprites - Adding Text (
Page 3 of 4 )
PyGame also makes it easy to insert text into applications. The pygame.font module is responsible for this, and it can be done in just a few lines of Python:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((256, 256))
pygame.display.set_caption('Application')
screen.fill((159, 182, 205))
# Create a font
font = pygame.font.Font(None, 17)
# Render the text
text = font.render('Powered by Python and PyGame', True, (255,
255, 255), (159, 182, 205))
# Create a rectangle
textRect = text.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
# Blit the text
screen.blit(text, textRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
We start by calling the Font method, passing two arguments. The first argument is the filename of the text to use, but since we pass None, a generic font is used. The second argument is simply the size of the font, which we set to 17. We must then render the text we want using the font. To do this, we pass four arguments to render. The first argument is, obviously, the text that is to be rendered. The second is a boolean value that indicate whether or not antialiasing is to be used. The third argument is the color of the font, and the fourth argument is the background color. If the fourth argument is missing, the background becomes transparent. However, this is slow, and since we know what color the background will be already, we can just pass the color as an argument. Next, we create a rectangle and use centerx and centery to center it in the screen. Finally, we blit the text.
PyGame also supports some text effects—bold text and italicized text. Bolding or italicizing a font involves calling a single method:
...
font.set_italic(True)
font.set_bold(True)
...
It's possible to search for a specific font on the user's computer, and it's also possible to get a list of the user's fonts. We can also specify a list of fonts and have PyGame return the first match. This way, your application can fall back on another font if the best font isn't available:
import pygame
import random
import sys
pygame.init()
screen = pygame.display.set_mode((256,256))
pygame.display.set_caption('Application')
screen.fill((159, 182, 205))
# Search for Verdana and create a Font object
verdana = pygame.font.match_font('Verdana')
verdanaFont = pygame.font.Font(verdana, 13)
# Search for a font that does not exist and create a Font object
doesNotExist = pygame.font.match_font('doesNotExist')
dNEFont = pygame.font.Font(doesNotExist, 13)
# Search for a font that does not exist, followed by Arial, and create a Font object
arial = pygame.font.match_font('doesNotExist,Arial')
arialFont = pygame.font.Font(arial, 13)
print arial, arialFont
# Get a list of fonts
fonts = pygame.font.get_fonts()
# Create some random fonts
# get_fonts() merely returns a list of names, so we'll have to
match them
font1 = pygame.font.Font(pygame.font.match_font(random.choice
(fonts)), 13)
font2 = pygame.font.Font(pygame.font.match_font(random.choice
(fonts)), 13)
font3 = pygame.font.Font(pygame.font.match_font(random.choice
(fonts)), 13)
# Render text in verdana, doesNotExist, arial and the random
fonts
text1 = verdanaFont.render(str(verdana), 1, (255, 255, 255),
(159, 182, 205))
text2 = dNEFont.render(str(doesNotExist), 1, (255, 255, 255),
(159, 182, 205))
text3 = arialFont.render(str(arial), 1, (255, 255, 255), (159,
182, 205))
text4 = font1.render('Random', 1, (255, 255, 255), (159, 182,
205))
text5 = font2.render('Random', 1, (255, 255, 255), (159, 182,
205))
text6 = font3.render('Random', 1, (255, 255, 255), (159, 182,
205))
# Make some rectangles
rect1 = text1.get_rect()
rect2 = text2.get_rect()
rect3 = text3.get_rect()
rect4 = text4.get_rect()
rect5 = text5.get_rect()
rect6 = text6.get_rect()
# Position the rectangles
rect1.y, rect1.centerx = 0, screen.get_rect().centerx
rect2.y, rect2.centerx = 20, screen.get_rect().centerx
rect3.y, rect3.centerx = 40, screen.get_rect().centerx
rect4.y, rect4.centerx = 60, screen.get_rect().centerx
rect5.y, rect5.centerx = 80, screen.get_rect().centerx
rect6.y, rect6.centerx = 100, screen.get_rect().centerx
# Blit everything
screen.blit(text1, rect1)
screen.blit(text2, rect2)
screen.blit(text3, rect3)
screen.blit(text4, rect4)
screen.blit(text5, rect5)
screen.blit(text6, rect6)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
Here, we simply search for some fonts and then get three random fonts, displaying a string of text in each font. Notice how None is returned if a font does not exist. This is why it's a good idea to either specify a list of fonts or include the fonts you'll be using (provided that you are allowed to).