In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the Font module in Pygame. I hope you will get something after reading this article. Let's discuss it together.
Text is one of the indispensable important elements in any game. Pygame creates a font object through the pygame.font module to achieve the purpose of drawing text. The common methods of this module are as follows:
Method description pygame.font.init () initialization font module pygame.font.quit () de-initialization font module pygame.font.get_init () checks whether the font module is initialized and returns a Boolean value. Pygame.font.get_default_font () gets the file name of the default font. Return the file name of the fonts in the system pygame.font.get_fonts () to get all available fonts, the return value is a list of all available fonts pygame.font.match_font () matches the font file from the font library of the system, and the return value is the complete font file path pygame.font.SysFont () create a Font object pygame.font.Font () from the font library of the system create a Font object from a font file
The Font module provides two ways to create font (Font) objects, namely:
SysFont (load font files from the system to create font objects)
Font (create font objects from file paths)
The following two methods are introduced separately:
1) font.SysFont ()
Load fonts directly from the system using the following methods:
Pygame.font.SysFont (name, size, bold=False, italic=False)
Name: the list parameter value, which represents the font name to be loaded from the system, will be searched in the order of the elements in the list, and if there are no fonts in the list in the system, the Pygame default font will be used.
Size: indicates the font size
Bold: whether the font is bold or not
Italic: whether the font is italic.
Examples of use are as follows:
Print ("get all available fonts in the system", pygame.font.get_fonts ()) my_font = pygame.font.SysFont (['founder bold black Song simplified', 'microsoftsansserif'], 50)
The above methods will give priority to the use of "founder rough black Song simplified style".
Note that if you want to display Chinese, then be sure to use Chinese font files, such as "founder bold black Song simplified", otherwise there will be text garbled (programming to help three words can not be displayed). As follows:
Figure 1: display Chinese garbled code
2) font.Font ()
When we want to introduce cool fonts into the game, but the system does not exist, we can use another way to load the font file from the outside to draw the text. The syntax format is as follows:
My_font = pygame.font.Font (filename, size)
The parameters are described as follows:
Filename: string format, indicating the path of the font file
Size: sets the font size.
Examples of use are as follows:
F = pygame.font.Font ('CJV _ engine _ UsersUnix _ administrators _ _ Desktop_ willhardy. Ttfcircle _ journal 50)
A font file is loaded from the desktop to create a font object and set the font size to 50. Note that the above font file is downloaded from the Internet, you can also download it at will (click the URL to go), or use the font file in the system library.
3) Font object method
Pygame provides some common methods for working with font objects, as follows:
Method description pygame.font.Font.render () this function creates a Surface object that renders the text pygame.font.Font.size () this function returns the size required to render the text The return value is an one-tuple width Height) whether pygame.font.Font.set_underline () underscores the text content pygame.font.Font.get_underline () checks whether the text is underlined pygame.font.Font.set_bold () starts bold rendering pygame.font.Font.get_bold () checks whether the text uses bold rendering pygame.font.Font.set_italic () starts italics rendering pygame.font.Font.metrics () to get characters The detailed parameter pygame.font.Font.get_italic () for each character in the string checks whether the text uses italic rendering pygame.font.Font.get_linesize () to get the line height of the font text pygame.font.Font.get_height () to get the height of the font pygame.font.Font.get_ascent () to get the distance from the top of the font to the baseline pygame.font.Font.get_descent () to get the distance from the bottom of the font to the baseline
Using the above methods, we can easily render the font or obtain information about the font, such as the height of the font, whether it is bold, italic and so on.
The first method, which is used at most in the above method, is a key method for drawing text content, and its syntax format is as follows:
Render (text, antialias, color, background=None)
The description of the parameters is as follows:
Text: the text content to be drawn
Antialias: Boolean parameter, whether it is a smooth font (anti-aliasing).
Color: setting font color
Background: optional parameter. Default is None. This parameter is used to set the background color of the font.
Let's look at a set of simple examples:
Import sysimport pygame# initializes pygame.init () screen = pygame.display.set_mode ((600400)) # fills the background color of the main window screen.fill ((20pj90) 50) # sets the window title pygame.display.set_caption ('c language Chinese net') # Font file path C:/Windows/Fonts/simhei.ttff = pygame.font.Font ('CJV antialias text, antialias, color) # font file path Background=None)-> Surfacetext = f.render ("URL: c.biancheng.net", True, (255 text,textRect 0), (255255255)) # get the rect area size of the display object textRect = text.get_rect () # set the display object center textRect.center = (300200) screen.blit (text,textRect) while True: # Loop get event Listening event for event in pygame.event.get (): # determine whether the user clicked the close button if event.type = = pygame.QUIT: # Uninstall all pygame modules pygame.quit () # Terminator sys.exit () pygame.display.flip () # update the screen content
The running result of the program is as follows:
Figure 2: displaying text content
In addition to using the above methods, in order to enhance the function of the font module, Pygame adds another font module in the new version, which is the Freetype module. This module belongs to the advanced module of Pygame, it can completely replace the Font module, and many new functions have been added to the Font module, such as adjusting the distance between characters, font vertical mode and rotating text counterclockwise (see the official documentation for details).
If you want to Freetype the module, you must guide the package in the following ways:
Import pygame.freetype
Below, use the Freetype module to draw the text content, as follows:
Import sys,pygameimport pygame.freetypepygame.init () # sets the location variable pos= [180 Cool 50] # sets the color variable GOLD=255,251,0BLACK=0,0,0screen=pygame.display.set_mode ((600400)) pygame.display.set_caption ("c language Chinese net") f1=pygame.freetype.Font ("C:/Users/Administrator/Desktop/willhar_.ttf", 45) # Note that render_to () is used here to draw text content, compared with render There is no return value for this method. # Parameter description: # pos starts drawing text, fgcolor represents foreground color, and bgcolor represents background color. Rotation represents the angle of text rotation freeRect = f1.render_to (screen, pos, "I love c.biancheng.net", fgcolor = GOLD,bgcolor = BLACK, rotation=35) while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () pygame.display.update ()
The running result of the program is as follows:
Figure 3: drawing text content
After reading this article, I believe you have a certain understanding of "how to use the Font module in Pygame". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.