+-

我在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 …但是也许有某种方法吗?我再次提醒大家不要更正我没有问过的任何事情.我知道可能有一些更简单的方式来做这些事情…
最佳答案
health = wordfont.render(("Your health: ", self.a,), 1, (255, 9, 12))
应该
health = wordfont.render("Your health: {0}".format(self.a), 1, (255, 9, 12))
要么
health = wordfont.render("Your health: %s" % self.a), 1, (255, 9, 12))
(“您的健康:”,self.a,)是一个元组.通过传递字符串,可以解决您的问题.
请参阅here以了解我已完成的工作…
点击查看更多相关文章
转载注明原文:python-pygame文本变量 - 乐贴网