Call function by button click in Pygame

2019-07-27 01:37发布

问题:

I got a screen with buttons in Pygame here in the code below. Now I want to click the button, then a random() function starts and after 5 seconds it returns to the screen from the beginning with the buttons and let me the option to click again and call a random function again.

def loop():
    clock = pygame.time.Clock()
    number = 0
    # The button is just a rect.
    button = pygame.Rect(300,300,205,80)
    done = False
    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:
                    # `event.pos` is the mouse position.
                    if button.collidepoint(event.pos):
                        # Incremt the number.
                        number += 1

random()
loop()
pygame.quit()

回答1:

Add a state variable (runRandom), which indicates if the the function random has to be run:

runRandom = False
while not done:

    # [...]

    if runRandom:
        random()

Add user defined pygame.event, which can be used for a timer:

runRandomEvent = pygame.USEREVENT + 1
for event in pygame.event.get():

   # [...]

   elif event.type == runRandomEvent:
      # [...]

Allow the button to be pressed if random ins not running. If the button is pressed then state runRandom and start the timer (pygame.time.set_timer()) with the decided period of time (e.g. 5000 milliseconds = 5 seconds):

# [...]

elif event.type == pygame.MOUSEBUTTONDOWN:
    if event.button == 1:
        if button.collidepoint(event.pos) and not runRandom:
            # [...]

            runRandom = True
            pygame.time.set_timer(runRandomEvent, 5000)

When the time has elapse, the stop running random by runRandom = False and stop the timer:

# [...]

elif event.type == runRandomEvent:
    runRandom = False
    pygame.time.set_timer(runRandomEvent, 0)

Apply the suggestion to your code somehow like this:

# define user event for the timer
runRandomEvent = pygame.USEREVENT + 1

def loop():
    clock = pygame.time.Clock()
    number = 0
    # The button is just a rect.
    button = pygame.Rect(300,300,205,80)
    done = False
    runRandom = False
    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:
                    # `event.pos` is the mouse position and  "random" is not running
                    if button.collidepoint(event.pos) and not runRandom:
                        # Incremt the number.
                        number += 1

                        # Start timer and enable running "random"
                        runRandom = True
                        pygame.time.set_timer(runRandomEvent, 5000) # 5000 milliseconds

            elif event.type == runRandomEvent:
                runRandom = False
                pygame.time.set_timer(runRandomEvent, 0)

        # [...]

        # run "random"
        if runRandom:
            random()

        # [...]