0

So, i'm trying out pygame with Mu and started with a very simple code that creates a window and gives a caption to a window. I'm brazilian so i´ve got words with accents in this caption, and when i run the code the letters got all messed up.

The string i'm using is "Invasão Alienígena". When i run the code this turns into "Invasão AlienÃ-gena"

So i thought, ok, this is an enconding problem. I checked with sys.stdout.enconding which gave me utf-8. I thought that this was weird, shouldn't it give the right characters if i'm using this enconding? Or i'm not understanding it right?

I checked the python version and gave me 3.6.3

I looked for the hex values of the special characters and put them in the string as in "Invas\xE3o Alien\xEDgena" and it worked. But i wasn't satisfied with this approach.

So i read a lot of stuff and tried everything that i found but nothing worked. I then started to play around and got this

title = "Invasão Alienígena".encode("latin-1").decode("utf-8")

And it worked! I just can't understand why it worked. What it seems to me is that pygame is decoding in latin-1 and not in utf-8 but i can't know for sure.

`

import sys  
import pygame  

print(sys.stdout.encoding)  
print(sys.version)  
def run_game():  
    pygame.init()  
    print("Invasão Alienígena") #prints Invasão Alienígena to stdout  
    #title = "Invas\xE3o Alien\xEDgena" #If i use this it will work without encoding/decoding  
    title = "Invasão Alienígena".encode("latin-1").decode("utf-8") #this works
    screen = pygame.display.set_mode((1200, 600))  
    pygame.display.set_caption(title)  

    while True:  
        for event in pygame.event.get():  
            if event.type == pygame.QUIT:  
                sys.exit()  
        pygame.display.flip()  

run_game()  

`

So, what is happening? Did i misunderstood how the encoding works or this is an unusual behaviour?

  • With just `title = "Invasão Alienígena"`, this works fine for me (so does the `print`). Python 3.6.8, Ubuntu Linux 18.04.x. What editor are you using for the code? Maybe it's re-encoding the text or suchlike. – Kingsley Sep 24 '19 at 22:20
  • I am using Mu, which is design for beginers to program with microbit and pygame. Using python from cmd works Fine. I will look for the documentation on Mu to see If there is something like you mentioned – Marcelo Guimarães Sep 25 '19 at 03:57

0 Answers0