python如何在pygame中设置字体并显示中文详解
作者:huadong_xiaolin 时间:2021-03-21 13:32:50
一、查看可用字体
import pygame
print(pygame.font.get_fonts())
二、设置字体
1.使用系统字体
self.font=pygame.font.SysFont(None,48)#None系统默认字体
self.font = pygame.font.SysFont("arial", 16)
2.使用字体文件
字体文件可以外下载
self.font = pygame.font.Font("my_font.ttf", 16) #字体,字号
3.设置中文()见前2
#系统自带
self.font = pygame.font.SysFont("SimHei", 32) # 显示中文
在网上下载一个中文字体文件,将这个文件与我们的程序放在同一个文件夹,如果是中文的文件名,将它改成英文文件名。例如,下载了迷你简毡笔黑.TTF,将文件名改成了mnjzbh.ttf,并将程序的第一句改成:
self.font=pygame.font.Font('mnjzbh.ttf',32) # 显示中文
三、附录:常见系统字体对照表
一般的中文字体名,使用拼音即可,如 仿宋fangsong, 楷体kaiti
新细明体:PMingLiU
细明体:MingLiU
标楷体:DFKai-SB
黑体:SimHei
宋体:SimSun
新宋体:NSimSun
仿宋:FangSong
楷体:KaiTi
仿宋_GB2312:FangSong_GB2312
楷体_GB2312:KaiTi_GB2312
微软正黑体:Microsoft JhengHei
微软雅黑体:Microsoft YaHei
一个应用小例子
import pygame.font
class BUtton:
def __init__(self, ai_game, msg):
"""初始化按钮属性"""
# msg是要在按钮上显示的文本
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# 设置按钮的尺寸和其他属性
self.width, self.height = 200, 50
self.buttom_color=(0,255,0)
self.text_color=(255,255,255)
self.font=pygame.font.SysFont(None,48)
#创建按钮的rect对象,并使其居中
self.rect=pygame.Rect(0,0,self.width,self.height)
self.rect.center=self.screen_rect.center
#按钮的标签只创建一次
self._pre_msg(msg)
def _pre_msg(self,msg):
"""将msg渲染为图像,并使其在按钮上居中"""
self.msg_image=self.font.render(msg,True,self.text_color
,self.buttom_color)
#布尔实参指定开启还是关闭反锯齿功能,反锯齿让文本的边缘更平滑
self.msg_image_rect=self.msg_image.get_rect()
#让图像文本在按钮上居中
self.msg_image_rect.center=self.rect.center
def draw_button(self):
"""绘制一个用颜色填充的按钮,再绘制文本"""
self.screen.fill(self.buttom_color,self.rect)
self.screen.blit(self.msg_image,self.msg_image_rect)
附:在 Pygame 屏幕中添加文字
font = pygame.font.SysFont("freesansbold.ttf", 30)# 30:font size
text = font.render("content", True, (0,0,0))# (0,0,0) color of font
self.window.blit(text,(10,10))# (10,10) rect left top
来源:https://blog.csdn.net/huadong_xiaolin/article/details/126257202
标签:pygame,字体,中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pyqt5 实现窗口缩放,控件在窗口内自动伸缩的操作
2022-10-16 06:32:09
![](https://img.aspxhome.com/file/2023/7/91997_0s.jpg)
巧用Dreamweaver MX制作阴影字
2009-08-31 17:36:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113255757s.gif)
浅谈python中scipy.misc.logsumexp函数的运用场景
2023-11-10 17:10:56
Mysql日期和时间函数大全
2011-03-08 09:52:00
Python 操作 ElasticSearch的完整代码
2021-07-10 17:26:41
![](https://img.aspxhome.com/file/2023/0/79480_0s.png)
IE9初窥:支持CSS3,和HTML5?
2009-12-01 14:20:00
![](https://img.aspxhome.com/file/UploadPic/200912/1/dean_pdc_5-27s.png)
DIV+CSS设计时容易犯的一些错误
2007-11-13 12:42:00
Python实现统计文本文件字数的方法
2023-05-24 11:46:04
python游戏实战项目之俄罗斯方块的魅力
2021-12-07 19:38:23
![](https://img.aspxhome.com/file/2023/3/100833_0s.jpg)
utf8_unicode_ci与utf8_general_ci的区别
2010-03-03 15:54:00
asp代码WinHttp.WinHttpRequest.5.1使用例子
2010-03-11 21:28:00
使用Python处理KNN分类算法的实现代码
2023-11-03 07:03:07
![](https://img.aspxhome.com/file/2023/4/63244_0s.png)
PHP中str_split()函数的用法讲解
2023-06-05 09:43:15
![](https://img.aspxhome.com/file/2023/5/55375_0s.png)
python多进程使用apply_async的使用方法详解
2022-11-14 16:08:50
![](https://img.aspxhome.com/file/2023/3/80813_0s.png)
Django单元测试工具test client使用详解
2021-04-11 22:25:58
如何随机显示图片计数器?
2010-05-16 15:21:00
Python3实现飞机大战游戏
2022-08-25 05:29:03
![](https://img.aspxhome.com/file/2023/6/81826_0s.jpg)
python requests.post请求404问题及解决方法
2022-08-16 09:36:23
![](https://img.aspxhome.com/file/2023/1/68941_0s.png)
Python中的类型提示(Type Hints)总结
2023-12-24 02:26:46
![](https://img.aspxhome.com/file/2023/3/95753_0s.png)
Python yield的使用详解
2021-07-17 22:23:29