PyGame中如何显示汉字?什么是用于此目的的好的免费/自由字体?
Bopete
问问题
7727 次
3 回答
7
pygame 使用 SDL_ttf 进行渲染,因此您应该在渲染过程中保持良好状态。
unifont.org似乎有一些关于开源字体的大量资源,用于一系列脚本。
我抓取了 Cyberbit pan-unicode 字体并提取了包含的 ttf。以下“在我的机器上工作”是 Windows Vista Home Basic 和 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 没有任何类型的输入法。
于 2009-03-21T02:10:27.923 回答
0
据我所知——我自己还没有尝试过——当你向 PyGame 传递一个包含中文字符的 Unicode 字符串时,它应该可以正常工作,例如。你'\u4e2d\u56fd'。
请参阅http://www.unifont.org/fontguide/下的“东亚”,了解相当多的合适的开放许可字体。
于 2009-03-21T02:08:56.243 回答
0
pygame
以下是可以呈现中文的内置字体列表:
arialms
fzshuti
fzyaoti
lisu
malgungothic
microsoftjhengheimicrosoftjhengheiui
microsoftjhengheimicrosoftjhengheiuibold
microsoftjhengheimicrosoftjhengheiuilight
microsoftyaheimicrosoftyaheiui
microsoftyaheimicrosoftyaheiuibold
microsoftyaheimicrosoftyaheiuilight
msgothicmsuigothicmspgothic
simsunnsimsun
stcaiyun
stfangsong
sthupo
stkaiti
stliti
stsong
stxihei
stxingkai
stxinwei
stzhongsong
youyuan
yugothicmediumyugothicuiregular
yugothicregularyugothicuisemilight
yugothicyugothicuilight
yugothicyugothicuisemiboldyugothicuibold
上述每一种字体都适用于汉字。这是一个例子"stsong"
:
import pygame
text = "中文"
font = "stsong"
pygame.font.init()
wn = pygame.display.set_mode((500, 500))
font = pygame.font.SysFont(font, 190)
wn.blit(font.render(text, True, (255, 255, 255)), (50, 50))
pygame.display.update()
输出:
于 2021-02-13T16:34:32.370 回答