“bool object不可调用”pygam

2024-06-13 23:36:09 发布

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

我在尝试重新分配实例的x和y值时遇到“bool object is not callable”错误。我不相信我在召唤一个“bool”对象。唯一的可能就是自选错了,但我试着去掉那条线却得到了同样的错误。怎么了?在

产生错误的函数:

    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

函数在这里被调用。我将实例按类存储为列表,这就是为什么我有嵌套循环。在

^{pr2}$

错误:

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

请询问是否需要澄清。谢谢你的帮助。在


Tags: inimageselffalseforifpnginit
1条回答
网友
1楼 · 发布于 2024-06-13 23:36:09

在pieces类中,将move设置为boolean。在

因为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调用超级方法init,所以这些属性也在Pawns中实例化。在

^{pr2}$

__init__中删除它,它应该可以工作了。在

相关问题 更多 >