I want to render some unicode characeters on screen.
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)
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)
To print any character in the Python interpreter, use a \u to denote a unicode character and then follow with the character code.
To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2. x, you also need to prefix the string literal with 'u'.
We can put the Unicode value with the prefix \u. Thus we can successfully print the Unicode character.
You need to add your font name and location .
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
On Python 3.4 you no longer need the u
before "♛"
like in Python 2.7.
unistr = "♛"
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With