[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] time progression independent of game "ticks"



Yeah, a time queue is the way to go, don't bother with threads.  On each update, you can get the current time (pygame.time, or pygame.clock), and pass that into your time queue.  The time queue, will look something like this:

[(5.0,eat,fish1),(6.5,eat,fish2)]

At each iteration, you continue to pop() the list, calling each function (eat) with the argument (fish1, fish2), until the item on the list has a time that is greater than the current time.  If you spread out your scheduling, so there aren't too many fish doing the same thing at the exact same time, this will scale really well and be a good way to handle timed events.

As for tracking time, I use pygame.time as well.  I've found pygame.clock to be inaccurate in some cases, and of course it's not usable in situations where you don't want to use pygame (server).

I store lasttime = pygame.time.time() at the start of the simulation, then on each iteration I calculate dt = pygame.time.time()-lasttime;.  Then if I have some sort of game timer (like your 15 minutes = 1 year) I add the dt(real time difference) times a modifier (to get game time difference) to the game time variable.