本文介绍了PyGame中的Unicode字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PyGame中显示汉字?

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

推荐答案

pygame使用SDL_ttf进行渲染,因此您应该在

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.

我抓住了Cyber​​bit的pan-unicode字体并提取了包含的ttf。以下是在Windows Vista Home Basic和Python 2.6上运行的在我的机器上工作:

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()

只要您只显示unicode文本,应该处于梦幻般的状态。但是,如果您想从用户那里实际读取unicode输入,则情况就更加暗淡。 Pygame没有任何输入方法。

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.

这篇关于PyGame中的Unicode字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 14:17
查看更多