font模块主要用于获取字体Font对象,而freetyep模块是font模块的增强版本,保留了font模块的所有功能,并在font模块的基础之上增加了许多的功能。在pygame中,绘制文字的方法只有一种,就是先获取字体对象,然后字体对象调用render()方法生成一个新的图像(Surface),再将这个图像(Surface)绑定到窗口(Surface)中。
# 获取系统字体的对象的方法
font_name = pygame.font.get_default_font()
font1 = pygame.font.SysFont(font_name, 50)
font_list = pygame.font.get_fonts()
font_path = pygame.font.match_font(font_list[1])
font2 = pygame.font.Font(font_path, 50)
# 获取图片Surface
text_surface1 = font1.render("123", True, (255, 0, 0), (255, 255, 255))
text_surface2 = font1.render("456", True, (255, 0, 0), (255, 255, 255))
# 将图片Surface绑定到窗口Surface
surface.blit(text_surface1, (200, 100))
surface.blit(text_surface2, (400, 100))
下面介绍一下font模块中的函数,如下表:
函数 | 描述 |
---|---|
pygame.font.init() | 初始化font模块 |
pygame.font.quit() | 取消初始化font模块 |
pygame.font.get_init() | 判断font模块是否初始化 |
pygame.font.get_default_font() | 获得系统字体的文件名 |
pygame.font.get_fonts() | 获得所有系统字体文件名的列表 |
pygame.font.match_font() | 根据文件名查找字体的路径 |
pygame.font.SysFont() | 根据字体的名称获得对应的Font对象 |
pygame.font.Font() | 根据字体的路径或者字体的文件名获得对应的Font对象 |
对于一个Font对象,有下列方法:
函数 | 描述 |
---|---|
pygame.font.Font.bold | Gets or sets whether the font should be rendered in (faked) bold. |
pygame.font.Font.italic | Gets or sets whether the font should be rendered in (faked) italics. |
pygame.font.Font.underline | Gets or sets whether the font should be rendered with an underline. |
pygame.font.Font.render | draw text on a new Surface |
pygame.font.Font.size | determine the amount of space needed to render text |
pygame.font.Font.set_underline | control if text is rendered with an underline |
pygame.font.Font.get_underline | check if text will be rendered with an underline |
pygame.font.Font.set_bold | enable fake rendering of bold text |
pygame.font.Font.get_bold | check if text will be rendered bold |
pygame.font.Font.set_italic | enable fake rendering of italic text |
pygame.font.Font.metrics | gets the metrics for each character in the passed string |
pygame.font.Font.get_italic | check if the text will be rendered italic |
pygame.font.Font.get_linesize | get the line space of the font text |
pygame.font.Font.get_height | get the height of the font |
pygame.font.Font.get_ascent | get the ascent of the font |
pygame.font.Font.get_descent | get the descent of the font |
总结一下,font模块中比较常用的方法有:
pygame.font.Font(filename, size)
pygame.font.Font(pathlib.Path, size)
filename:字体文件的文件名;
pathlib.Path: 字体文件的路径;
size:字体的高height,单位为像素;
pygame.font.Font.render(text, antialias, color, background=None)
text:要显示的文字;
antialias: 是否抗锯齿;
color:字体颜色;
background:背景颜色(可选参数)
freetype模块的函数如下:
函数 | 描述 |
---|---|
pygame.freetype.get_error | 返回最近的FreeType错误 |
pygame.freetype.get_version | 返回FreeType版本 |
pygame.freetype.init | 初始化FreeType模块 |
pygame.freetype.quit | 取消初始化FreeType模块 |
pygame.freetype.get_init | 判断FreeType模块是否初始化 |
pygame.freetype.get_cache_size | 返回缓存大小 |
pygame.freetype.get_default_resolution | 返回模块的默认像素大小,以每英寸点数为单位。默认值为 72 DPI |
pygame.freetype.set_default_resolution | 为模块设置默认像素大小,以每英寸点数为单位。如果可选参数被省略或为零,则分辨率将重置为 72 DPI |
pygame.freetype.SysFont | 从系统字体中创建一个Font对象 |
pygame.freetype.get_default_font | 获取默认字体的文件名 |
pygame.freetype.Font | 根据所支持的字体中创建一个Font对象 |
对于使用freetype模块创建的Font对象,有下列方法:
函数 | 描述 |
---|---|
pygame.freetype.Font.name | Proper font name. |
pygame.freetype.Font.path | Font file path |
pygame.freetype.Font.size | The default point size used in rendering |
pygame.freetype.Font.get_rect | Return the size and offset of rendered text |
pygame.freetype.Font.get_metrics | Return the glyph metrics for the given text |
pygame.freetype.Font.height | The unscaled height of the font in font units |
pygame.freetype.Font.ascender | The unscaled ascent of the font in font units |
pygame.freetype.Font.descender | The unscaled descent of the font in font units |
pygame.freetype.Font.get_sized_ascender | The scaled ascent of the font in pixels |
pygame.freetype.Font.get_sized_descender | The scaled descent of the font in pixels |
pygame.freetype.Font.get_sized_height | The scaled height of the font in pixels |
pygame.freetype.Font.get_sized_glyph_height | The scaled bounding box height of the font in pixels |
pygame.freetype.Font.get_sizes | return the available sizes of embedded bitmaps |
pygame.freetype.Font.render | Return rendered text as a surface |
pygame.freetype.Font.render_to | Render text onto an existing surface |
pygame.freetype.Font.render_raw | Return rendered text as a string of bytes |
pygame.freetype.Font.render_raw_to | Render text into an array of ints |
pygame.freetype.Font.style | The font’s style flags |
pygame.freetype.Font.underline | The state of the font’s underline style flag |
pygame.freetype.Font.strong | The state of the font’s strong style flag |
pygame.freetype.Font.oblique | The state of the font’s oblique style flag |
pygame.freetype.Font.wide | The state of the font’s wide style flag |
pygame.freetype.Font.strength | The strength associated with the strong or wide font styles |
pygame.freetype.Font.underline_adjustment | Adjustment factor for the underline position |
pygame.freetype.Font.fixed_width | Gets whether the font is fixed-width |
pygame.freetype.Font.fixed_sizes | the number of available bitmap sizes for the font |
pygame.freetype.Font.scalable | Gets whether the font is scalable |
pygame.freetype.Font.use_bitmap_strikes | allow the use of embedded bitmaps in an outline font file |
pygame.freetype.Font.antialiased | Font anti-aliasing mode |
pygame.freetype.Font.kerning | Character kerning mode |
pygame.freetype.Font.vertical | Font vertical mode |
pygame.freetype.Font.rotation | text rotation in degrees counterclockwise |
pygame.freetype.Font.fgcolor | default foreground color |
pygame.freetype.Font.bgcolor | default background color |
pygame.freetype.Font.origin | Font render to text origin mode |
pygame.freetype.Font.pad | padded boundary mode |
pygame.freetype.Font.ucs4 | Enable UCS-4 mode |
pygame.freetype.Font.resolution | Pixel resolution in dots per inch |