我正在使用pygame製作蛇遊戲,並且我有兩個變量x
和y
,我試圖定義它。我有以下:將非int對象置爲「Int對象不可下載」
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)
...並且得到錯誤:
line 175, in drawSnake
x = coord['x'] * CellSize
TypeError: 'int' object is not subscriptable
請幫幫忙,其他類似的問題是沒有幫助的。
什麼是'蛇協會'?當然,它會在你遍歷它時返回'ints',然後嘗試索引這些:'coord ['x'] * CellSize',你期望*會發生什麼?您需要提供一個*可重現的示例*,包括將重現該錯誤的代碼以及所需的輸出。 –