David Mašek
David Mašek

Reputation: 922

Rendering unicode in pygame

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)

Upvotes: 5

Views: 8391

Answers (1)

john taylor
john taylor

Reputation: 1100

working 3.4 unicode

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

Upvotes: 5

Related Questions