AttributeError:类型对象'BlackPiece'没有属性'rect'

时间:2017-04-06 19:56:50

标签: python pygame

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])

我调用最后一行并在标题中得到错误。我很确定这很简单。最后一行是在另一个子程序中。我已经创建了对象,但我正在尝试编辑它们的位置。

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])
  

第61行,更新

     

self.rect.x = x AttributeError:type object'BlackPiece'没有属性'rect'

1 个答案:

答案 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] 谢谢!