Mads
Mads

Reputation: 13

python/pygame error TypeError: 'bool' object is not callable

I'm trying to make this object move when you click it in pygame; and it works the first time you click it but after that it gives me this error:

game_loop()
  File "C:\Users\MadsK_000\Desktop\spil\Python\spiltest\Test spil.py", line 57, in game_loop
    Clicked_ = clicked(x,y,width,height,mouse_pos)
TypeError: 'bool' object is not callable

Here's my code

import pygame
import time
pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("test")
clock = pygame.time.Clock()
mainthingImg = pygame.image.load("mainthing.PNG")
width = 88
height = 85
x = 100
y = 100
mouse_pos = pygame.mouse.get_pos()
def mainthing(x,y):
    gameDisplay.blit(mainthingImg, (x,y))

def clicked(x,y,width,height,mouse_pos):
    clicked = False
    if mouse_pos[0] > x and x + width > mouse_pos[0] and mouse_pos[1] > y and y + height > mouse_pos[1]:
        clicked = True
        global clicked

    return clicked

def text_objects(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

def ptd(text):
    stortext = pygame.font.Font("freesansbold.ttf", 40)
    TextSurf, TextRect = text_objects(text,stortext)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    game_loop()


def game_loop():



    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                Clicked_ = clicked(x,y,width,height,mouse_pos)
                if Clicked_ == True:
                    x += 100
                    y += 100
                    global x
                    global y
        gameDisplay.fill(red)
        mainthing(x,y)
        pygame.display.update()
        clock.tick(60)
ptd("Wellcome")
pygame.display.update()
game_loop()
pygame.quit()
quit()

Upvotes: 0

Views: 1284

Answers (2)

DurgaDatta
DurgaDatta

Reputation: 4170

There is a name conflict for clicked. Inside the function clicked there is variable with same named (clicked = False) and this is also linked to global scope (global clicked). So, when the function is executed, clicked has been modified to boolean, instead of function ( function definition creates the variable name in its scope, global here). Please name the function and variable separately.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121524

You set a global clicked to a boolean, inside the clicked function:

def clicked(x,y,width,height,mouse_pos):
    clicked = False
    if mouse_pos[0] > x and x + width > mouse_pos[0] and mouse_pos[1] > y and y + height > mouse_pos[1]:
        clicked = True
        global clicked

    return clicked

Use a different global name for the boolean, or rename your clicked function. Functions are just globals too.

Upvotes: 3

Related Questions