HomePython Page 3 - PyGame for Game Development: Font and Sprites
Adding Text - Python
PyGame is a library for Python that allows you to develop computer games. It is also useful for very graphical applications. This article will help you grasp the basics of PyGame.
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:
# 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:
# 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)
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).