1

I am trying to put a play button in my game. and when I launch the game I click the play button and it gives me an error

Traceback (most recent call last):
   File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 370, in <module>
   game_intro()
   File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 336, in game_intro
   button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, run)
   File "C:\Users\Zee_S\OneDrive\Desktop\python projects\lil Shooter\Player\Lil Shooter.py", line 308, in button
   action()
TypeError: 'bool' object is not callable
[Finished in 8.3s]

and I've looked at the code for this 'bool' thing but I cant find anything. here is the code for the buttons and title screen

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

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(screen, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(screen, ic, (x, y, w, h))
    smallText = pygame.font.SysFont("comicsansms", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    screen.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    quit()

def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            # print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        screen.fill(white)
        largeText = pygame.font.SysFont("comicsansms", 115)
        TextSurf, TextRect = text_objects("Lilshooter", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        screen.blit(TextSurf, TextRect)

        button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, run)
        button("Quit", 480, 450, 100, 50, red, bright_red, quitgame)

        pygame.display.update()
        clock.tick(15)

and this is where I called it in the game loop

run = True
game_intro()
while run:
    [...]

can I get help whit this problem

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Zina
  • 159
  • 8

2 Answers2

1

The last argument to the function button needs to be a function. Therefore it cannot be run because run is a Boolean value.

If you want to change the status of the intro variable when the button is pressed, write a startGame function:

def startGame():
    global intro
    intro = False

Pass startGame to button, instead of run:

button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, startGame)

Note the variable intro needs to be a variable in global namespace:
(see global statement)

intro = True
def game_intro():
    global intro

    while intro:   
        # [...]   
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

You have a problem with the name action. Your error is

in button action() TypeError: 'bool' object is not callable

It means that somewhere in the code you useaction as function but it's a bool. First, let's look at action in the button() function. We see that it is an input argument and that after an if check there is a possibility that action will be called as a function: the problem arises because the only check you make on action is that it is not None, so if it is not None but neither a function it will raise an error. Somewhere in the code you invoke button() and pass it a True (or equivalent variable) as action argument.

In particular, button is invoked by game_intro() twice:

button("LETS PLAY!", 20, 450, 115, 50, green, bright_green, run)
button("Quit", 480, 450, 100, 50, red, bright_red, quitgame)

Since there is no initialisation of run inside game_intro(), I assume that the variable run is the same initialised outside the while loop (run = True).

Wippo
  • 853
  • 8
  • 22