栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

模块五:font模块

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模块五: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.boldGets or sets whether the font should be rendered in (faked) bold.
pygame.font.Font.italicGets or sets whether the font should be rendered in (faked) italics.
pygame.font.Font.underlineGets or sets whether the font should be rendered with an underline.
pygame.font.Font.renderdraw text on a new Surface
pygame.font.Font.sizedetermine the amount of space needed to render text
pygame.font.Font.set_underlinecontrol if text is rendered with an underline
pygame.font.Font.get_underlinecheck if text will be rendered with an underline
pygame.font.Font.set_boldenable fake rendering of bold text
pygame.font.Font.get_boldcheck if text will be rendered bold
pygame.font.Font.set_italicenable fake rendering of italic text
pygame.font.Font.metricsgets the metrics for each character in the passed string
pygame.font.Font.get_italiccheck if the text will be rendered italic
pygame.font.Font.get_linesizeget the line space of the font text
pygame.font.Font.get_heightget the height of the font
pygame.font.Font.get_ascentget the ascent of the font
pygame.font.Font.get_descentget 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:背景颜色(可选参数)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/700559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号