How do you delay specific events in a while loop?

2020-02-16 03:24发布

问题:

Recently I've been working with a simple and straightforward RPG in python with pygame, but I'm having some problems delaying specific events. Running the code below, everything happens at once.

if event.key == pygame.K_SPACE and buttonHighlight == 0:

    FireAnimation() #displays a fire image

    #DELAY HERE

    if player[6] == 'Magic': #you deal damage to the enemy
        enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[4]))
    else:
        enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[3]))

    #DELAY HERE

    StarAnimation() #displays a star image

    #DELAY HERE

    if enemy[6] == 'Magic': #enemy deals damage to you
        player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[4]))
    else:
        player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[3]))

The rest of the code isn't really relevant, I just wanted to point out where I want to delay. Running this, both images displays, the player and the enemy takes damage at the same time. Thanks!

EDIT: I forgot to mention that I already have tried pygame.time.delay/wait and time.sleep, but all those delays the whole operation! It simply pushes everything forward when I use it, so everything happens at the same time several seconds later

回答1:

You can create two new events (FIRE_ANIMATION_START, STAR_ANIMATION_START) which you post to the event queue with a delay (with pygame.time.set_timer(eventid, milliseconds)). Then in your event loop you just check for it.

FIRE_ANIMATION_START = pygame.USEREVENT + 1
STAR_ANIMATION_START = pygame.USEREVENT + 2

# ... Your code ...

for event in pygame.event.get():

    if event.key == pygame.K_SPACE and buttonHighlight == 0:
        pygame.time.set_timer(FIRE_ANIMATION_START, 10)    # Post the event every 10 ms.
        pygame.time.set_timer(STAR_ANIMATION_START, 1000)  # Post the event every 1000 ms.

    elif event.code == FIRE_ANIMATION_START:
        pygame.time.set_timer(FIRE_ANIMATION_START, 0)     # Don't post the event anymore.
        FireAnimation() #displays a fire image
        if player[6] == 'Magic': #you deal damage to the enemy
            enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[4]))
        else:
            enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[3]))

    elif event.code == STAR_ANIMATION_START:
        pygame.time.set_timer(STAR_ANIMATION_START, 0)     # Don't post the event anymore.
        StarAnimation() #displays a star image
        if enemy[6] == 'Magic': #enemy deals damage to you
             player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[4]))
        else:
             player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[3]))

Documentation for pygame.time.set_timer(eventid, milliseconds). Also, as the code is right now it has bugs in it. The attributes for the events differs between different event types, so always make sure to check whether an event is KEYDOWN or USEREVENT before accessing the attributes event.key or event.code. The different types and attributes can be found here.



回答2:

If you know how much time you need, you can simply add:

from time import sleep
...
sleep(0.1)

This will add a 100 milliseconds delay



回答3:

You can use

pygame.time.delay(n)

or

pygame.time.wait(n)

to pause the program for n milliseconds. delay is a little more accurate but wait frees the processor for other programs to use while pygame is waiting. More details in pygame docs.



标签: python pygame