Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';bool对象不可调用';皮加梅_Python_Pygame - Fatal编程技术网

Python ';bool对象不可调用';皮加梅

Python ';bool对象不可调用';皮加梅,python,pygame,Python,Pygame,在尝试重新分配实例的x和y值时,出现“bool对象不可调用”错误。我不相信我在召唤一个“bool”物体。唯一的可能是将self.selected重新指定为False,但我尝试摆脱该行,并得到了相同的错误。怎么了 引发错误的函数: class Pawns(Pieces): def __init__(self, x, y, image): Pieces.__init__(self, x, y, image) self.upgra

在尝试重新分配实例的x和y值时,出现“bool对象不可调用”错误。我不相信我在召唤一个“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
回溯(最近一次呼叫最后一次):
文件“/Users/marcelolejeune/Documents/Python/Python
rojects/Pygame/Chess/ChessGame.py”,第145行,in
拖板
文件“/Users/marcelolejeune/Documents/Python/Python
项目/Pygame/Chess/ChessGame.py”,第115行,在绘图板上
典当
TypeError:“bool”对象不可调用
如果这还不够,下面是我所有的代码

    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()
从操作系统导入路径
导入pygame
pygame.init()
棕色,白色=(112,94,94),(255255)
win=pygame.display.set_模式((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
类别作品:
定义初始化(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抽签(自我,赢):
全局sel
如果self.x+80>cursor[0]>self.x和self.y+80>cursor[1]>self.y:
pygame.draw.rect(赢,(128128),self.hitbox)
如果单击[0]和self.x+80>光标[0]>self.x和self.y+80>光标[1]>self.y和sel光标[0]>self.x和self.y-80>光标[1]>self.y,然后单击[0]:
自我保护y-=80
self.selected=False
班级皇后(件):
定义初始化(self,x,y,image):
片段。u_初始化(self,x,y,image)
班王(件):
定义初始化(self,x,y,image):
片段。u_初始化(self,x,y,image)
等级车(件):
定义初始化(self,x,y,image):
片段。u_初始化(self,x,y,image)
班主教(件):
定义初始化(self,x,y,image):
片段。u_初始化(self,x,y,image)
职业骑士(件):
定义初始化(self,x,y,image):
片段。u_初始化(self,x,y,image)
wking=[King(240560,k[0])]
bking=[King(320,0,k[1])]
wqueens=[Queens(320560,q[0])]
bqueens=[Queens(240,0,q[1])]
wpawns=[范围(0641,80)内x的典当(x,480,p[0])]
bpawns=[范围(0641,80)内x的典当(x,80,p[1])]
范围(0561560)内x的wrooks=[Rooks(x,560,r[0])]
brooks=[Rooks(x,0,r[1]),对于范围内的x(0561560)]
wknight=[骑士(x,560,kn[0]),对于范围内的x(80481400)]
bknights=[骑士(x,0,kn[1]),对于范围内的x(80481400)]
wbishops=[主教(x,560,b[0]),代表范围内的x(160401240)]
bbishops=[主教(x,0,b[1]),代表范围内的x(160401240)]
字符=[wking+bking+wqueens+bqueens+wpawns+bpawns+wrooks+brooks+Wknight+Bknight+wbishops+bbishops]
def牵引板():
尺寸=80
win.fill(白色)
对于范围(0,9)内的x:
如果x%2==0:
对于范围(0、8、2)内的y:
pygame.draw.rect(赢,布朗,(x*大小,y*大小,大小,大小))
其他:
对于范围(1、9、2)内的y:
pygame.draw.rect(赢,布朗,(x*大小,y*大小,大小,大小))
对于wpawns中的典当:
典当
对于字符中的字符:
对于单件字符:
抽签(赢)
pygame.draw.rect(赢,(255,0,0),wking[0]。hitbox,1)
pygame.display.update()
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
运行=错误
click=pygame.mouse.get_pressed()
cursor=pygame.mouse.get_pos()
拖板

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

在pieces类中,您将move设置为布尔值

由于
典当
继承自
碎片
它接收此属性

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
中实例化

class Pawns(Pieces):
    def __init__(self, x, y, image):
        Pieces.__init__(self, x, y, image)
        self.upgrade = False
将其从
\uuuu init\uuuu
中删除,它应该可以工作