python3下pygame如何实现显示中文
作者:易三一世 时间:2021-01-15 00:57:04
这篇文章主要介绍了python3下pygame如何实现显示中文,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.先看代码:
import pygame
from pygame.locals import *
def main():
pygame.init()
screen = pygame.display.set_mode((1000, 450)) #窗口的大小
pygame.display.set_caption('pygame程序的界面的中文设置') #窗口标题,中文不需要特别的设置
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
#font = pygame.font.Font(None, 60) #原始代码,使用默认字体,不能显示中文
font = pygame.font.Font('/home/xgj/Desktop/simsun/simsun.ttf', 60) #显示中文的设置和字体,及路径
text = font.render("Hello 我爱你", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.center = background.get_rect().center
background.blit(text, textpos)
screen.blit(background, (0, 0))
pygame.display.flip()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__':
main()
2.效果:
3.注意字体:
字体需要自己下载好,放置一个指定的文件夹
如:本游戏中的字体:
simsun.ttf
来源:https://www.cnblogs.com/ysysbky/p/12172761.html
标签:python,pygame,中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
学会sql数据库关系图(Petshop)
2012-10-07 10:34:49
关于JS中变量的显式申明和隐式申明
2008-09-12 13:04:00
科学设计你的网站网页
2008-03-16 13:48:00
PHP面向对象程序设计之类与反射API详解
2023-11-19 12:44:12
轻松掌握SQL Server数据库的六个实用技巧
2009-01-15 12:56:00
Oracle 数据显示 横表转纵表
2009-07-26 08:57:00
Python NLP开发之实现聊天机器人
2021-12-21 18:04:48
![](https://img.aspxhome.com/file/2023/1/66851_0s.png)
python创建与遍历List二维列表的方法
2023-04-29 13:07:48
屏幕分辨率和布局简述
2009-04-20 20:27:00
python爬虫爬取笔趣网小说网站过程图解
2022-10-06 10:56:50
![](https://img.aspxhome.com/file/2023/7/79587_0s.png)
php过滤器使用详解
2023-06-13 01:52:23
使用字符串建立查询能加快服务器的解析速度吗?
2010-07-14 21:03:00
Python sklearn CountVectorizer使用详解
2023-06-20 08:19:05
![](https://img.aspxhome.com/file/2023/4/76024_0s.png)
mac安装python3后使用pip和pip3的区别说明
2021-01-06 21:23:15
![](https://img.aspxhome.com/file/2023/3/77553_0s.jpg)
Pytorch实现Fashion-mnist分类任务全过程
2023-07-14 05:12:47
![](https://img.aspxhome.com/file/2023/3/62323_0s.png)
Python tkinter常用操作代码实例
2021-01-05 21:26:09
解决django无法访问本地static文件(js,css,img)网页里js,cs都加载不了
2023-06-13 19:13:36
![](https://img.aspxhome.com/file/2023/7/59637_0s.jpg)
js+asp总结
2009-11-16 17:49:00
带你轻松了解 SQL Server数据库的组成
2009-02-05 15:53:00
ASP怎么读取指定xml 的节点
2008-04-28 13:12:00