Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

python - Chinese unicode fonts in PyGame

How can I display Chinese characters in PyGame? And what's a good free/libre font to use for this purpose?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

pygame uses SDL_ttf for rendering, so you should be in fine shape as rendering goes.

unifont.org appears to have some extensive resources on Open-Source fonts for a range of scripts.

I grabbed the Cyberbit pan-unicode font and extracted the encluded ttf. The folowing 'worked on my machine' which is a Windows Vista Home Basic and Python 2.6:

# -*- coding: utf-8 -*-

import pygame, sys


unistr = u"黒澤 明"
pygame.font.init()
srf = pygame.display.set_mode((640,480))
f = pygame.font.Font("Cyberbit.ttf",20)
srf.blit(f.render(unistr,True,(0,0,0)),(0,0))
pygame.display.flip()

while True:
    srf.blit(f.render(unistr,True,(255,255,255)),(0,0))
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

As long as you're just displaying unicode text, you should be in fantastic shape. If, however, you want to actually read unicode input from the user, the situation is much more bleak. Pygame has no input methods of any sort.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...