在 pygame 中渲染 unicode [英] Rendering unicode in pygame
本文介绍了在 pygame 中渲染 unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在屏幕上呈现一些 unicode 字符.
I want to render some unicode characeters on screen.
使用 pygame.font
会显示一个奇怪的字符.
Using pygame.font
displays a weird character.
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("TEST")
FONT = pygame.font.Font(None, 64)
font_surf = FONT.render("♛", True, pygame.Color("red"))
screen.blit(font_surf, (20, 20))
pygame.display.flip()
pygame.time.delay(1000)
我也尝试过使用 pygame.freetype.它什么都不显示.
I also tried using pygame.freetype. It displays nothing at all.
import pygame.freetype
pygame.freetype.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("TEST")
FONT = pygame.freetype.Font(None)
FONT.render_to(screen, (20, 20), "♛", size=(40, 40))
pygame.display.flip()
pygame.time.delay(1000)
推荐答案
您需要添加字体名称和位置.
You need to add your font name and location .
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
在 Python 3.4 上,您不再需要 "♛"
之前的 u
,就像在 Python 2.7 中一样.
On Python 3.4 you no longer need the u
before "♛"
like in Python 2.7.
unistr = "♛"
基于其他链接的示例,但以 3.4 为例,示例为 2.7
sample based on that other link but for 3.4 as example is 2.7
# -*- coding: utf-8 -*-
import pygame
import sys
unistr = "♛"
pygame.font.init()
srf = pygame.display.set_mode((500,500))
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
srf.blit(f.render(unistr,True,(255,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()
这篇关于在 pygame 中渲染 unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文