Search code examples
pythonpygametext-rendering

Pygame Text Doesn't Display


def score():
    global x_score, o_score
    
    scoreFont = pygame.font.SysFont('Comic Sans', 20, bold=True)
    SCORE = scoreFont.render(f'X   {x_score} : {o_score}   O', True, (0, 0, 0))
    screen.blit(SCORE, ((WIDTH / 2) - (SCORE.get_width() / 2), HEIGHT + SCORE.get_height()))
  • WIDTH and HEIGHT = screens width and height

  • x and o scores are defined

  • pygame and font are initialized from the start

  • Called pygame.display.flip() after the function

I have everything else in my code displaying BUT this. Can anyone help me out and tell me what I'm doing wrong on trying to display the scores?


Solution

  • The score is at the bottom outside the window. Change the calculation of the y-coordinate. The vertical position is HEIGHT - SCORE.get_height() instead of HEIGHT + SCORE.get_height():

    screen.blit(SCORE, ((WIDTH / 2) - (SCORE.get_width() / 2), HEIGHT + SCORE.get_height()))

    screen.blit(SCORE, ((WIDTH / 2) - (SCORE.get_width() / 2), HEIGHT - SCORE.get_height()))