TypeError: decoding str is not supported , How do i Decode String in Pygame

Viewed 70

I am working on a Tic Tac Toe Program, But when I am trying to decode my assigned thingie so, can you fix it for me, please? also is there another way I can do this if you can't fix it pls you can also use this code if you want its my own code :)

Here is the Code :


# Initalize pygame
pygame.init()

# Creating the Window
win = pygame.display.set_mode((500 , 500))

# Font
font = pygame.font.Font('freesansbold.ttf', 32)

# Variables
Blackk = 0 , 0 , 0
Redd = 255 , 0 , 0
Bluee = 0 , 0 , 255
Character = ""

# X or O
x_o = random.randint( 0 , 1 )
if x_o == 1:
    Character == "X"

elif x_o == 0:
    Character == "O"

STR_xo = str("You ARE \n" , Character)
    
# Text
You_R = font.render((STR_xo) , True , Blackk , (240 , 240 , 240))

# Make it Rect
textRect = You_R.get_rect()

# Title And Icon
pygame.display.set_caption("Tic Tac Toe")
#TitIco = pygame.image.load()
#pygame.display.set_icon()

# Main Game Loop
RunNow = True
while RunNow:
    for event in pygame.event.get():

        win.fill((240 , 240 , 240 ))

        pygame.draw.rect(win, Blackk, pygame.Rect(150, 0, 20, 500))
        pygame.draw.rect(win, Blackk, pygame.Rect(335, 0, 20, 500))
        
        pygame.draw.rect(win, Blackk, pygame.Rect(0, 150, 500, 20))
        pygame.draw.rect(win, Blackk, pygame.Rect(0, 335, 500, 20))

        win.blit(You_R, textRect)

        
        if event.type == pygame.QUIT:
            RunNow = False

    pygame.display.update()
1 Answers

Just concatenate strings with +:

STR_xo = str("You ARE \n" , Character)

STR_xo = "You ARE" +  Character 

or use a formated string literal:

STR_xo = f"You ARE {Character}" 

However, if you want to render a string with multiple lines in Pygame, you need to render each line separately. See Rendering text with multiple lines in pygame:

You_R_surf = font.render("You ARE" , True , Blackk , (240 , 240 , 240))
Character_surf = font.render( Character, True , Blackk , (240 , 240 , 240))

# [...]

RunNow = True
while RunNow:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            RunNow = False

    win.fill((240 , 240 , 240 ))

    pygame.draw.rect(win, Blackk, pygame.Rect(150, 0, 20, 500))
    pygame.draw.rect(win, Blackk, pygame.Rect(335, 0, 20, 500))
    
    pygame.draw.rect(win, Blackk, pygame.Rect(0, 150, 500, 20))
    pygame.draw.rect(win, Blackk, pygame.Rect(0, 335, 500, 20))

    win.blit(You_R_surf, (0, 0))
    win.blit(Character_surf, (0, 32))

    pygame.display.update()  

Additionally there is typo. The assignment operator is = instead of ==:

x_o = random.randint( 0 , 1 )
if x_o == 1:
    Character = "X"
elif x_o == 0:
    Character = "O"
Related