I am trying to display the score of a platformer game at the top of the screen but every time I run it I get this error : “Text must be a Unicode or bytes” I’ve already looked on the site and the code all looks exactly like what I’ve written but I’m still getting the same error. This is my code so far:
def __init__(self):
#this has all the things needed to initialise the game but the only relevant one to my problem is this line
self.font_name = pygame.font.match_font(FONT_NAME)
def draw(self):
self.screen.fill(BLACK)
self.all_sprites.draw(self.screen)
self.draw_text(self.score, 22, WHITE, WIDTH / 2, 15)
pygame.display.flip()
def draw_text(self, text, size, colour, x, y):
font = pygame.font.Font(self.font_name, size)
text_surface = font.render(text, True, colour)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
self.screen.blit(text_surface, text_rect)
All things in capitals have been given values in another file I called settings and then imported to this file.
The issue seems to be with the text_surface line but I have no idea what the problem is.
The first argument to render
has to be a string and it seems you try to pass a number.
From the docs:
The text can only be a single line: newline characters are not rendered. Null characters ('x00') raise a TypeError. Both Unicode and char (byte) strings are accepted.
Convert your value to a string first with the str
function:
font.render(str(text), True, colour)