2017-05-04 71 views
0

我正在使用pygame製作蛇遊戲,並且我有兩個變量xy,我試圖定義它。我有以下:將非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 

請幫幫忙,其他類似的問題是沒有幫助的。

+5

什麼是'蛇協會'?當然,它會在你遍歷它時返回'ints',然後嘗試索引這些:'coord ['x'] * CellSize',你期望*會發生什麼?您需要提供一個*可重現的示例*,包括將重現該錯誤的代碼以及所需的輸出。 –

回答

1

看起來進入該函數的snakeCoords的值是一個整數列表。

把打印語句,以確保:

def drawSnake(snakeCoords): 
    for coord in snakeCoords: 
     print(type(coord)) 
     x = coord["x"] * CellSize 
     y = coord['x'] * CellSize 

你應該看到下面的輸出python2:

<class 'int'>

或這python3:

<object 'int'>

這是導致y我們的錯誤。

+2

這會更好地作爲評論:這是實際問題中需要的信息。 – brianpck

+0

是的,那是我得到的,是否有可能讓它成爲一個整數?這將符合我的代碼的目的。 – Icedart

+0

@Icedart看起來你需要傳入一個包含x和y鍵的字典對象列表(y鍵沒有寫在你的代碼中)。如果所有想要的都是x和y座標,那麼使用元組可能會更好。你將不得不重寫你的函數來處理它。 –