2017-10-10 96 views
0

如何在python 3.6中使用pygame(font.render(),font.freetype/font.font)在字符串中顯示錶情符號?如果不可能,我需要在Pillow Draw.text()中渲染的代碼。我會很感激。 棄掉此:(填充的什麼這個問題,因爲長得像我的帖子主要是代碼,所以我加入了一些空話啓用後)Python,如何使用Pygame呈現文本表情符號?

# -*- coding: UTF-8 -*- 

import pygame, emoji 
import pygame.freetype 

pygame.init() 
screen = pygame.display.set_mode((640, 480)) 
clock = pygame.time.Clock() 
done = False 

pygame.font.init() 
font = pygame.freetype.SysFont("segoe-ui-symbol.ttf", size=50) 
#font = pygame.font.SysFont("segoe-ui-symbol.ttf", 72) 
font_color = (255,255,255, 255) 
font_background = (0,0,0, 0) 

text_string = emoji.emojize('') 
#text_string = u'ujiHelloÁpQ|, World ' 
#text_string = u'ujiHelloÁpQ|\U0001f44d, World ' 
#print(emoji.emojize('Python is :thumbs_up_sign:')) 
#text = font.render("♛", True, font_color, (0,0)) 
#text = font.render(text_string, True, font_color, (0,0)) 
text = font.render(text_string, fgcolor=font_color, size=0) 

image_size = list(text[0].get_size()) 
text_image = pygame.Surface(image_size) 
text_image.fill(font_background) 
pygame.display.flip() 

text_image.blit(text[0], (0,0)) 

#print(dir(text_image)) 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
     if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
      done = True 

    screen.fill((0, 0, 0)) 
    #screen.blit(text, 
    screen.blit(text_image, 
     (320 - text[0].get_width() // 2, 240 - text[0].get_height() // 2)) 

    pygame.display.flip() 
    clock.tick(60) 

回答

0

我在這裏看到兩個可能的問題。

首先,如果沒有任何顯示或應用程序似乎失速,則Pygame無法找到SysFont。這發生在Mac上,因爲系統字體沒有segoe-ui-symbol.ttf。因此,我下載了一個version from the internet並從中創建了一個正常的freetype.Font

它工作正常。

第二個問題可能是字體本身沒有這些字符。有時操作系統會將字體無法用不同字體呈現的字符替換掉。

相關問題