python - How to draw str(non-latin-word) in Pygame? -


i'm learning program , question can trivial . sorry !

i want programm draw word (in russian) on screen using pygame . , ok, working :

# coding=utf-8 import pygame,sys  pygame.init()  screen = pygame.display.set_mode((640,480))  some_font=pygame.font.font(none,30)  word_to_print=some_font.render(u'привет',true,(255,255,255),(0, 0, 0))  while 1:         screen.blit(word_to_print,(0,0))         event in pygame.event.get():         if event.type == pygame.quit: sys.exit()         pygame.display.update() 

but when try draw same word, in form :

some_font.render(str('привет'),true,(255,255,255),(0, 0, 0)) 

=> output strange looking rubbish (something ÐÐµÑ Ñа).

i got 2 questions :

1) how program drow str(any word in russian) ?

2) why working ?

1) how program draw str(any word in russian) ?

strictly speaking, can't. if want display arbitrary russian text, should store text in unicode() object, not str() object:

unicode_string = u'привет' some_font.render(unicode_string, ...) 

if don't have access original unicode() string, have encoded str() string, can decode it, if know encoding:

str_string = 'привет' unicode_string = str_string.decode('utf-8') some_font.render(unicode_string, ...) 

if object apply str() not str() (see comments below), can call unicode() instead:

unicode_string = unicode(my_object) some_font.render(unicode_string, ... 

finally, if object apply str() not provide .__unicode__() method, return encoded str() string, can decode string if know encoding:

str_string = str(my_object) unicode_string = str_string.decode('utf-8') some_font.render(unicode_string, ... 

2) why working ?

from pygame docs, "both unicode , char (byte) strings accepted. char strings latin1 encoding assumed." have passed utf-8-encoded string method requires latin1-encoded string.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -