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
If you know how much time you need, you can simply add:
This will add a 100 milliseconds delay
You can use
or
to pause the program for
n
milliseconds.delay
is a little more accurate butwait
frees the processor for other programs to use while pygame is waiting. More details in pygame docs.You can create two new events (
FIRE_ANIMATION_START
,STAR_ANIMATION_START
) which you post to the event queue with a delay (withpygame.time.set_timer(eventid, milliseconds)
). Then in your event loop you just check for it.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 isKEYDOWN
orUSEREVENT
before accessing the attributesevent.key
orevent.code
. The different types and attributes can be found here.