2017-04-06 12 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:型オブジェクト 'BlackPiece'には属性 '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

なぜこれが下落しているのですか?クラスのインスタンスを作成する必要があることを理解していないのは正直な間違いです。 –

答えて

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]を 感謝!

関連する問題