'bool object is not callable' pygame



我在试图重新分配实例的x和y值时会得到'bool对象是不可呼出的'错误。我不相信我在呼吁"布尔"对象。唯一的可能性是重新分配self.seled false,但我试图摆脱这一行并遇到相同的错误。怎么了?

提出错误的功能:

    class Pawns(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)
            self.upgrade = False

        def move(self):
            if self.selected:
                if self.x+80 > cursor[0] > self.x and self.y-80 > 
    cursor[1] > self.y and click[0]:
                    self.y -= 80
                    self.selected = False

此功能在此处调用。我的实例按班级存储为列表,这就是为什么我有嵌套循环。

    for pawn in wpawns:
            pawn.move()
    for character in characters:
        for piece in character:
                piece.draw(win)

错误:

 Traceback (most recent call last):
 File "/Users/marcelolejeune/Documents/Python/Python 
 rojects/Pygame/Chess/ChessGame.py", line 145, in <module>
 DrawBoard()
 File "/Users/marcelolejeune/Documents/Python/Python 
 Projects/Pygame/Chess/ChessGame.py", line 115, in DrawBoard
 pawn.move()
 TypeError: 'bool' object is not callable

如果还不够,这是我的所有代码。

    from os import path
    import pygame
    pygame.init()
    brown,white = (112,94,94),(255,255,255)
    win = pygame.display.set_mode((640,640))
    k = [pygame.image.load('wking.png'), pygame.image.load('bking.png')]
    q = [pygame.image.load('wqueen.png'), pygame.image.load('bqueen.png')]
    p = [pygame.image.load('wpawn.png'), pygame.image.load('bpawn.png')]
    b = [pygame.image.load('wbishop.png'), pygame.image.load('bbishop.png')]
    kn = [pygame.image.load('wknight.png'), pygame.image.load('bknight.png')]
    r = [pygame.image.load('wrook.png'), pygame.image.load('brook.png')]

    sel = 0
    class Pieces:
        def __init__(self, x, y, image):
            self.x = x
            self.y = y
            self.image = image
            self.isAlive = True
            self.selected = False
            self.move = False
            self.hitbox = (self.x, self.y, 80, 80)
        def draw(self, win):
            global sel
            if self.x + 80 > cursor[0] > self.x and self.y + 80 > cursor[1] > self.y:
                pygame.draw.rect(win,(128,128,128),self.hitbox)
            if click[0] and self.x + 80 > cursor[0] > self.x and self.y + 80 > cursor[1] > self.y and sel <= 1:
                self.selected = True
                sel += 1
            if click[2]:
                self.selected = False
            if self.selected == False:
                sel = 0
            if self.selected == True:
                pygame.draw.rect(win,(255,10,10),self.hitbox)
            win.blit(self.image, (self.x, self.y))


    class Pawns(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)
            self.upgrade = False

        def move(self):
            if self.selected:
                if self.x+80 > cursor[0] > self.x and self.y-80 > cursor[1] > self.y and click[0]:
                    self.y -= 80
                    self.selected = False

    class Queens(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)
    class King(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)
    class Rooks(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)
    class Bishops(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)
    class Knights(Pieces):
        def __init__(self, x, y, image):
            Pieces.__init__(self, x, y, image)

    wking = [King(240, 560, k[0])]
    bking = [King(320, 0, k[1])]
    wqueens = [Queens(320, 560, q[0])]
    bqueens = [Queens(240, 0, q[1])]
    wpawns = [Pawns(x, 480, p[0]) for x in range(0,641,80)]
    bpawns = [Pawns(x, 80, p[1]) for x in range(0,641,80)]
    wrooks = [Rooks(x, 560, r[0]) for x in range(0,561,560)]
    brooks = [Rooks(x, 0, r[1]) for x in range(0,561,560)]
    wknights = [Knights(x, 560, kn[0]) for x in range(80, 481, 400)]
    bknights = [Knights(x, 0, kn[1]) for x in range(80, 481, 400)]
    wbishops = [Bishops(x, 560, b[0]) for x in range (160, 401, 240)]
    bbishops = [Bishops(x, 0, b[1]) for x in range (160, 401, 240)]
    characters = [wking+bking+wqueens+bqueens+wpawns+bpawns+wrooks+brooks+wknights+bknights+wbishops+bbishops]
    def DrawBoard():
        size = 80
        win.fill(white)
        for x in range(0, 9):
            if x % 2 == 0:
                for y in range(0, 8, 2):
                    pygame.draw.rect(win, brown, (x*size, y*size, size, size))
            else:
                for y in range(1, 9, 2):
                    pygame.draw.rect(win, brown, (x*size, y*size, size, size))
        for pawn in wpawns:
            pawn.move()
        for character in characters:
            for piece in character:

                piece.draw(win)

        pygame.draw.rect(win, (255,0,0), wking[0].hitbox, 1)
        pygame.display.update()



    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                run = False
        click = pygame.mouse.get_pressed()
        cursor = pygame.mouse.get_pos()

        DrawBoard()

请询问是否需要任何澄清。感谢您的帮助。

在您的碎片类中,您将设置为布尔值。

由于PawnsPieces继承了此属性。

class Pieces:
    def __init__(self, x, y, image):
        self.x = x
        self.y = y
        self.image = image
        self.isAlive = True
        self.selected = False
        self.move = False
        self.hitbox = (self.x, self.y, 80, 80)

也是因为您调用Pawns的超级方法初始化,因此这些属性是在Pawns中实例化的。

class Pawns(Pieces):
    def __init__(self, x, y, image):
        Pieces.__init__(self, x, y, image)
        self.upgrade = False

将其从__init__中删除,应该可以工作。

最新更新