Search code examples
pythonvariablestextfontspygame

pygame text variables


I'm having trouble with pygame...again...one of these days I swear I'll be good enough to not have to come here for easy answers...

Anyway, the problem this time is I'm trying to print text on the screen with a variable inside it.

wordfont = pygame.font.SysFont("sans",12)

class Status:

    def __init__(self):
        self.a=30

    def showme(self):
        health = wordfont.render(("Your health: ", self.a,), 1, (255, 9, 12))
        screen.blit(health, (150, 150))

It says that it has to be a string or unicode...but maybe there is some way? Once again, I remind everyone to not correct anything that I'm not asking about. I know there is probably some easier way to do these things...


Solution

  • health = wordfont.render(("Your health: ", self.a,), 1, (255, 9, 12))
    

    Should be

    health = wordfont.render("Your health: {0}".format(self.a), 1, (255, 9, 12))
    

    or

    health = wordfont.render("Your health: %s" % self.a), 1, (255, 9, 12))
    

    ("Your health: ", self.a,) is a tuple. By passing a string, you can solve your problem.

    See here to understand what I have done...