I've never found a clear answer to how I could create a timed event in pygame,
I want to create a timed movement with duration and cooldown. (let's say 1s and 2s respectively for this example). Similar to an Evade or Dash skill in games
I've reedited the code and:
dash_now = 0
dash_cooldown = 2
if dash:
while dash_now < 2 and dash_cooldown >= 0:
dash_now +- clock.tick(60) / 1000
player.dash()
firing == False
else:
dash = not dash
dash_now == 0
dash_cooldown == 3
dash_cooldown +- clock.tick(60) / 1000
Not working so far... I must be commiting a stupid mistake over here and I can't see which it is.
Its actually pretty simple:
Lets say you have a timer variable
timer = 0
After that, during every iteration, you add the time that has passed since the last frame:
timer += my_clock.tick(60) / 1000
Finally, later in the code, you can check if it meets a certain threshold.
if timer >= 2:
# Insert whatever you want
timer = 0
Therefore your code can be greatly simplified like this, as long as youre not expecting to be in a loop for the entirety of the movement.
self.dash_delay = 0
self.is_dashing = 0
def dash(self):
if dash_delay < 0 and self.is_dashing < 2:
self.rect.x += self.speedx * 2
self.rect.y += self.speedy * 2
self.is_dashing +- clock.tick(60) / 1000
elif self.is_dashing > 2:
self.dash_delay = 3
self.is_dashing = 0
self.dash_delay -= clock.tick(60) / 1000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.