I want to spawn an enemy every (some number) seconds, say 5.
I could do:
start_time = pygame.time.get_ticks()
if pygame.time.get_ticks() - start_time >= (some number):
spawn_enemy()
But there's one problem with that: when I change the FPS (clock.tick()
) from 120 to say 60 then the enemy spawn rate will remain the same.
I could also just make a variable:
var = 0
while True:
var += 1
if var >= (some number):
spawn_enemy()
But that seems like bad practice to me.
pygame.time.get_ticks()
measures the time. It doesn't relay on the frames per second.You can define a time span. When the time span is exceeded then spawn an enemy and increment the time:
Alternatively you can use a timer event. Use
pygame.time.set_timer()
to repeatedly create anUSEREVENT
. e.g.: