ddd
ddd

Reputation: 167

Call function on button click multiple time?

my python/Pygame program starts with a button that calls a function and a button that quits the game. I wanna push this first button, then a function gets called one time and after that, it should return to the beginning button screen and let me call the function again by button click. How can I do that? Currently i am only able to click the button, call the function and then the game ends. In the code below you see the most important parts of the code.

 def function():
 ....

 def main():
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    done = False

 def quit_game():  # A callback function for the button.
        nonlocal done
        done = True

    button1 = create_button(100, 100, 250, 80, 'function', function)
    button2 = create_button(100, 200, 250, 80, 'quit', quit_game)
        # A list that contains all buttons.
        button_list = [button1, button2]

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                # This block is executed once for each MOUSEBUTTONDOWN event.
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    # 1 is the left mouse button, 2 is middle, 3 is right.
                    if event.button == 1:
                        for button in button_list:
                            # `event.pos` is the mouse position.
                            if button['rect'].collidepoint(event.pos):
                                # Increment the number by calling the callback
                                # function in the button list.
                                button['callback']()


            screen.fill(WHITE)
            for button in button_list:
                draw_button(button, screen)
            pygame.display.update()
            clock.tick(30)


    main()

Upvotes: 2

Views: 1028

Answers (1)

Mercury Platinum
Mercury Platinum

Reputation: 1569

What you need here is to isolate each screen/game loop into its own special function:

So, for my button screen I can make a function like this:

def title():
    button1 = create_button(100, 100, 250, 80, 'game', game) # This will call the game function later in the file
    button2 = create_button(100, 200, 250, 80, 'quit_game', quit_game)
    # A list that contains all buttons.
    button_list = [button1, button2]
    # And so on with the rest of the code...

For the main game, you can do the same:

def game():
    button1 = create_button(100, 100, 250, 80, 'Exit', title) # This button will return you to the title
    # And whatever else you need     

After that, at the end of the file, you can add this:

if __name__ == '__main__':
    pygame.init()
    title() # Call the title first, and then further functions
    pygame.quit()

You will have to note that when you activate the buttons callback, a return afterwards is needed in order to deload that screen, otherwise you would just be layering game loops on top of eachother.

So, during the event loop:

if event.button == 1:
    for button in button_list:
        # `event.pos` is the mouse position.
        if button['rect'].collidepoint(event.pos):
            # Increment the number by calling the callback
            # function in the button list.
            button['callback']()
            return # Leave the function now that the new one is called.

Upvotes: 2

Related Questions