Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not sure how to blit font with a variable onto a surface, pygame

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading