Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 770
David Mašek Avatar asked Aug 11 '15 12:08

David Mašek


People also ask

How do I show Unicode in Python?

To print any character in the Python interpreter, use a \u to denote a unicode character and then follow with the character code.

How do I make Unicode support Python?

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'.

How do I print Unicode?

We can put the Unicode value with the prefix \u. Thus we can successfully print the Unicode character.


1 Answers

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()
like image 135
john taylor Avatar answered Nov 03 '22 18:11

john taylor