Home > Back-end >  Not sure how to blit font with a variable onto a surface, pygame
Not sure how to blit font with a variable onto a surface, pygame

Time:02-28

trying to blit onto the screen a font with a variable - the score you have achieved - onto the screen after dying.

BLACK = (0, 0, 0)
font_small = pygame.font.SysFont("Verdana", 20)
scoreMsg = "Your score: {0}".format(SCORE)
show_score = font_small.render(scoreMsg, True, BLACK)

later on I call the show_score variable like this:

screen.blit(show_score, (30, 400))

here is the full code: https://pastebin.com/5RnShSCG

edit: i forgot to mention. the text "Your score:" shows up on the screen, but the variable is always 0, even if score is higher.

CodePudding user response:

The SCORE is not tied to the show_score surface. The show_score surface does not magically change when you change SCORE. You must rerender the show_score surface:

class Enemy(pygame.sprite.Sprite):
  # [...]

  def move(self):

    global SCORE, show_score
    
    self.rect.move_ip(0,SPEED)
    if (self.rect.top > 600):
      self.rect.top = 0
      SCORE =1

      show_score = font_small.render("Your score: {0}".format(SCORE), True, BLACK)

      self.rect.center = (random.randint(30, 370), 0)
  • Related