python3下pygame如何实现显示中文

前端之家收集整理的这篇文章主要介绍了python3下pygame如何实现显示中文前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这篇文章主要介绍了python3下pygame如何实现显示中文,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.先看代码

  1. import pygame
  2. from pygame.locals import *
  3. def main():
  4. pygame.init()
  5. screen = pygame.display.set_mode((1000,450)) #窗口的大小
  6. pygame.display.set_caption('pygame程序的界面的中文设置') #窗口标题中文不需要特别的设置
  7. background = pygame.Surface(screen.get_size())
  8. background = background.convert()
  9. background.fill((250,250,250))
  10. #font = pygame.font.Font(None,60) #原始代码,使用默认字体,不能显示中文
  11. font = pygame.font.Font('/home/xgj/Desktop/simsun/simsun.ttf',60) #显示中文的设置和字体,及路径
  12. text = font.render("Hello 我爱你",1,(10,10,10))
  13. textpos = text.get_rect()
  14. textpos.center = background.get_rect().center
  15. background.blit(text,textpos)
  16. screen.blit(background,(0,0))
  17. pygame.display.flip()
  18. while 1:
  19. for event in pygame.event.get():
  20. if event.type == QUIT:
  21. return
  22. screen.blit(background,0))
  23. pygame.display.flip()
  24.  
  25. if __name__ == '__main__':
  26. main()

2.效果

python3下pygame如何实现显示中文

3.注意字体:

字体需要自己下载好,放置一个指定的文件

如:本游戏中的字体:

simsun.ttf

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的Python相关文章