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

2024-04-19 14:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

line 61, in Update

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


Tags: 对象rectimageselfifupdatepygameappend
1条回答
网友
1楼 · 发布于 2024-04-19 14:30:06
for i in range(len(ObjectBoard)):
    if type(ObjectBoard[i]) == BlackPiece:
          NewLocation = PieceLocation(a, b)
          ObjectBoard[i].update(NewLocation[0], NewLocation[1])

我需要使用ObjectBoard[I] 谢谢!你知道吗

相关问题 更多 >