python - ' bool 对象不可调用'pygame

标签 python pygame

尝试重新分配实例的 x 和 y 值时出现“bool object is not callable”错误。我不相信我正在调用一个“bool”对象。唯一的可能性是将 self.selected 重新分配给 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()

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

最佳答案

在您的 pieces 类中,您将 move 设置为 bool 值。

因为 Pawns 继承自 Pieces 它接收此属性。

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

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

将它从 __init__ 中移除,它应该可以工作。

关于python - ' bool 对象不可调用'pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50043740/

相关文章:

Python - 通过解包以太网帧在期望 0x800 的类型上获取 0xc0a8 而不期望数据

python - 需要关闭 python 套接字/在我的开发环境中查找当前正在运行的服务器

python - 如何检测图像中的不规则形状并为其内部添加值(value)?

python - 如何在 pygame 中显示带有可移动透明圆圈的黑色大矩形?

pygame - 如何为 google colab 创建视频设备?

python - 在python中通过正则表达式提取多种日期格式

python - 在pygame中检测键盘输入时只有某些键起作用

python - 生成混沌噪声较少的地形

python - 当我点击移动时,如何为 pygame 角色行走设置动画

python - 导入 SciPy 不起作用