我用pygame做了一个蛇形游戏,我有两个变量x
和y
,我正在尝试定义它们。我有如下几点:
def drawSnake(snakeCoords):
for coord in snakeCoords:
x = coord["x"] * CellSize
y = coord['x'] * CellSize
snake_segmentRect = pygame.draw.rect(x, y, CellSize, CellSize)
pygame.draw.rect(displaysurf, green, snake_segmentRect)
snake_segmentInnerRect = pygame.Rect(x + 4, y + 4, CellSize - 8, CellSize - 8)
pygame.draw.rect(displaysurf, green, snake_segmentInnerRect)
...and获取错误:
line 175, in drawSnake
x = coord['x'] * CellSize
TypeError: 'int' object is not subscriptable
请帮帮忙,其他类似的问题一点帮助都没有。
发布于 2017-05-04 21:53:30
看起来,进入该函数的snakeCoords的值是一个It列表。
输入一条print语句以确保:
def drawSnake(snakeCoords):
for coord in snakeCoords:
print(type(coord))
x = coord["x"] * CellSize
y = coord['x'] * CellSize
您应该会看到以下输出python2:
<class 'int'>
或者在python3中是这样:
<object 'int'>
这导致了您的错误。
https://stackoverflow.com/questions/43793333
复制