2017-04-06 28 views
-1
class BlackPiece(pygame.sprite.Sprite): 
    def __init__(self, x, y, checkerType): 
     pygame.sprite.Sprite.__init__(self) 

     self.CheckerType = checkerType 

     if checkerType == 1: 
      checkerImage = pygame.image.load('bp.png') 
      self.image = pygame.Surface([100, 100]) 

     if checkerType == 2: 
      checkerImage = pygame.image.load('bpKing.png') 
      self.image = pygame.Surface([100, 100]) 

     self.image.set_colorkey(black) 
     self.image.blit(checkerImage,(0, 0)) 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

     allSprites.append(self) 

    def update(self, x, y): 
     self.rect.x = x 
     self.rect.y = y 

    BlackPiece.update(BlackPiece, NewLocation[0], NewLocation[1]) 

我打電話給最後一行並在標題中出現錯誤。我很確定這很簡單。最後一行在另一個子例程中。我已經創建了對象,但我試圖編輯它們的位置。AttributeError:type object'BlackPiece'has no attribute'rect'

while i != 8: 
    if i % 2 == 1: 
     [x, y] = PieceLocation(i, 6) 
     NewBP = BlackPiece(x, y, 1) 
     blackPieces.add(NewBP) 
     ObjectBoard.append(NewBP) 
    else: 
     [x, y] = PieceLocation(i, 5) 
     NewBP = BlackPiece(x, y, 1) 
     blackPieces.add(NewBP) 
     ObjectBoard.append(NewBP) 
     [x, y] = PieceLocation(i, 7) 
     NewBP = BlackPiece(x, y, 1) 
     blackPieces.add(NewBP) 
     ObjectBoard.append(NewBP) 
    i = i + 1 

我首先使用它。這是創建一個實例嗎?我已經組成了一個組來繪製它,並將其添加到所有對象的數組中。我通過對象數組進行搜索,嘗試用它來更新它:

BlackPiece.update(BlackPiece, NewLocation[0], NewLocation[1]) 

line 61, in Update

self.rect.x = x AttributeError: type object 'BlackPiece' has no attribute 'rect'

+1

爲什麼你要在課堂上調用'update'?你應該創建一個「BlackPiece」類的實例並使用它。 – Matthias

+0

要更新位置?我已經創建了一個位置的對象。 –

+0

這是爲什麼downvoted?不明白要成爲一個班級的實例是一個誠實的錯誤。 –

回答

0
for i in range(len(ObjectBoard)): 
    if type(ObjectBoard[i]) == BlackPiece: 
      NewLocation = PieceLocation(a, b) 
      ObjectBoard[i].update(NewLocation[0], NewLocation[1]) 

我需要使用ObjectBoard [I] 的感謝!

相關問題