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

Using formatting to display a score in pygame

so i’m trying to display a score in my game when the player picks up a coin, the score is added by 1:

main.py

scorefont=pygame.font.Font('Fonts/Pixeltype.ttf',50)
score=scorefont.render('Score: {}'.format(level.score),False,'White')
game_active=True
while game_active:
     screen.blit(score,(0,0))

level.py

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

class Level:
    def __init__(self,level_data,surface):
        self.display_surface=surface
        self.level_building(level_data)
        self.world_shift=0
        self.score=0


playercoin=pygame.sprite.groupcollide(self.coins,self.player,True,False)
        if len(playercoin)==1:
            self.score+=1

However, the score does not get updated in the actual game. I’ve debugged it, and found out the score is being updated, but the display isn’t updated. Where did i go wrong in my code? (All variables are defined, i just didn’t show me defining them)

(score is supposed to be 1)

enter image description here

value of the score variable

enter image description here

>Solution :

The rendered surface does not magically change when you change the score. The rendered surface and the score are not tied. You need to render the score surface again with the new score when the score changes:

playercoin=pygame.sprite.groupcollide(self.coins,self.player,True,False)
if len(playercoin)==1:
    global score
    self.score+=1
    score=scorefont.render('Score: {}'.format(self.score),False,'White')
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