How to display Chinese characters in pyGame under python3

Time:2021-3-2

This article mainly introduces how to display Chinese in pyGame under python3. The sample code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

1. Look at the code first

import pygame
from pygame.locals import *
def main():
  pygame.init()
  screen =  pygame.display.set_ Mode ((1000, 450)) # window size
  pygame.display.set_ Caption ('Chinese setting of pyGame program interface ') # window title, Chinese does not need special setting
  background = pygame.Surface(screen.get_size())
  background = background.convert()
  background.fill((250, 250, 250))
  #font =  pygame.font.Font (none, 60) # original code, use default font, cannot display Chinese
  font =  pygame.font.Font ('/home/xgj/Desktop/simsun/ simsun.ttf , 60) # display Chinese settings, fonts, and paths
  text =  font.render (Hello, I love you, 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. Effect:

3. Pay attention to the font:

The font needs to be downloaded and placed in a specified folder

For example: the font in this game:

simsun.ttf

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.