python3下pygame如何实现显示中文

yipeiwu_com6年前Python基础

这篇文章主要介绍了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3使用requests登录人人影视网站的方法

早就听说requests的库的强大,只是还没有接触,今天接触了一下,发现以前使用urllib,urllib2等方法真是太搓了…… 这里写些简单的使用初步作为一个记录 本文继续练习使用re...

python生成以及打开json、csv和txt文件的实例

生成txt文件: mesg = "hello world" with open("test.txt", "w") as f: f.write("{}".format(mesg))...

Python实现二维曲线拟合的方法

如下所示: from numpy import * import numpy as np import matplotlib.pyplot as plt plt.close() f...

Django中的“惰性翻译”方法的相关使用

使用 django.utils.translation.gettext_lazy() 函数,使得其中的值只有在访问时才会被翻译,而不是在 gettext_lazy() 被调用时翻译。 例...

tensorflow 使用flags定义命令行参数的方法

tf定义了tf.app.flags,用于支持接受命令行传递参数,相当于接受argv。 import tensorflow as tf #第一个是参数名称,第二个参数是默认值,第三个...