我正在创建一个cookie clicker游戏,其中有一个表面显示我有多少个cookie。TypeError:'int'类型的对象没有len() - Python/Pygame
这是我的绘图文本代码。
def draw_text(self, text, font_name, size, color, x, y, align="nw"):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
self.screen.blit(text_surface, text_rect)
然后在我的游戏循环的新功能(一个新的游戏开始的时候),我创建了一个变量:
def new(self):
self.cookie_count = 0
然后终于,我有我的绘图功能。
def draw(self):
self.draw_text('Cookies: {}'.format(len(self.cookie_count)), self.cookie_font, 105, SKYBLUE, 325, HEIGHT * 3/4, align="nw")
pg.display.update()
然而,当我运行该程序,我得到:
TypeError: object of type 'int' has no len()
我是新来创建一个“比分反超”你可以称呼它。但为什么
self.draw_text('Cookies: {}'.format(len(self.cookie_count))
给我一个错误? self.cookie_count的长度如何不打印?
我不太清楚你想用'len'做什么,但它不'int'工作。 'len'返回一个字符串的长度。 – TheDetective
'.format(self.cookie_count)' – Alexander