3

I am getting a 'bool object is not callable' error when trying to reassign the x and y values of an instance. I don't believe I'm calling on a 'bool' object. The only possibility would be the reassignment of self.selected to False, but I tried getting rid of that line and got the same error. What's wrong?

The function which brings up the error:

    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

The function is called here. I have the instances stored as lists by class which is why i have the nested loop.

    for pawn in wpawns:
            pawn.move()

    for character in characters:
        for piece in character:
                piece.draw(win)

The Error:

 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

If that's not enough, here is all of my code.

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

Please ask if any clarification is needed. Thanks for the help.

Jignasha Royala
  • 1,032
  • 10
  • 27

1 Answers1

4

In your pieces class you set move to a boolean.

Since Pawns inherits from Pieces it receives this attribute.

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)

Also because you call the super method init from Pawns, these attributes are instantiated within Pawns aswell.

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

Remove it from the __init__ and it should work.

Pax Vobiscum
  • 2,551
  • 2
  • 21
  • 32