【发布时间】:2014-02-06 16:58:36
【问题描述】:
我有一个 python 2.7.6 程序,我想转换为 3.3.3。
我收到一个错误:
File "H:\My Game Python 3,3\MoonSurvival.py", line 199, in run
self.moon.levelStructure[x][y] = None
回溯是:
Traceback (most recent call last):
File "H:\My Game Python 3,3\MoonSurvival.py", line 591, in <module>
Game().run()
File "H:\My Game Python 3,3\MoonSurvival.py", line 199, in run
self.moon.levelStructure[x][y] = None
TypeError: list indices must be integers, not float
但是当我查看levelStructure 的代码时,并没有错。这是分配 levelStructure 的代码:
tempBlock = Block(x*64, y*64)
# add block to both and self.levelStructure
self.levelStructure[x][y] = tempBlock
如您所见,levelStructure 是 tempBlock。当它成倍增加时,它似乎在浮动。我知道除法是两个斜线/。我试图乘以 '*' 但我仍然得到错误。我对 3.3.3 语法并不完全熟悉,因为我通常使用 2.7.6。
块代码:
class Block(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('data/images/moon.jpg')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def render(self, surface):
surface.blit(self.image, self.rect)
是什么导致了这个错误?
【问题讨论】:
-
Block是什么,我们能看到它的代码吗? -
如果
x和y在乘法之前是浮点数,那么它们在乘法之后将保持浮点数。乘以一个整数不会改变这一点。
标签: python python-2.7 python-3.x pygame