-1

I am using Python and I am making a simple game with PyGame library, and I want to call a function that I wrote at a given time interval (say 1 second, but it will change throughout the game). I have tried using the time.time() function, but I couldn't get it to work, I also tried time.sleep(), but the way I have the game set up, the sleep function will affect the way the game runs. What is an efficient way to do it?

Mr.Robot
  • 49
  • 3

1 Answers1

1

You'll be running the game in a while loop, just check every loop if it's time to call it again or not:

from time import time, sleep

start = time()
cur_cycle = 0

while game_running():
     d_time = int(time() - start)
     if d_time > cur_cycle:
           cur_cycle = d_time
           # do stuff
     # to prevent computer from melting
     sleep(0.1)
Nathan
  • 3,558
  • 1
  • 18
  • 38