pygame文本变量

2024-10-01 22:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pygame上遇到了麻烦…又一次…总有一天我发誓我会很好的,不必为了简单的答案而来这里。。。在

不管怎样,这次的问题是我试图在屏幕上打印带有变量的文本。在

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))

它说它必须是一个字符串或unicode…但也许有什么办法?我再次提醒大家不要纠正我没有问到的任何事情。我知道也许有更简单的方法来做这些事情。。。在


Tags: 答案文本self屏幕defstatus事情pygame
3条回答

您将tuple("Your health: ", self.a,)作为render的第一个参数。我想应该用一根绳子代替。在

有几种方法to format a string with a variable,其中一种方法是:

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

应该是

^{pr2}$

或者

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

("Your health: ", self.a,)是一个元组。通过传递字符串,您可以解决您的问题。在

请看here了解我所做的。。。在

要发送字符串而不是元组作为第一个参数呈现:

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

相关问题 更多 >

    热门问题