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

Re: [pygame] A way of telling time?



Typically instead of counting down (which requires changing the counter over time), I prefer to schedule things ahead. So instead of a counter, I store a timeout value when I want something to happen (like current time + 365 sec) and then compare that to the current time periodically and do what I need to when current time >= timeout.

To show "progress" during the interim, you can also store the original duration of the timeout (like say 365 sec) and calculate a progress value as (timeout - current time) / duration. Then display whatever animation, etc. based on the progress value between 0 and 1.0.

A simply way to do this is to have timer objects that get updated every frame. Their update() methods would check to see if the current time had reached the timeout. This would allow a bunch of timers, but gets very inefficient if there are many many timers.

In the case there are too many timers for the simple update() strategy, use a priority queue. These keeps all the timers sorted in order by their timeout value. As timeouts are set, the timer objects are pushed onto the queue. Each frame, you check the queue to see if any timeouts have expired. If so, you pop them off the queue and call a method on each timer to tell it it's expired. This is really only a win though if there is no animation to perform before the timeouts expire. If you need to update an object anyhow during the time it waits, the additional logic of checking if it's timed out is pretty negligible.

hth,

-Casey

On Aug 5, 2007, at 10:31 AM, Joseph Quigley wrote:

Hi,
I just dug fairly deep and discovered pygame.time.set_timer(), but it looks like I can only have 2 events at one time. This might work for the simulation game I'm working on, but yes, I would like to determine the progression of time. Oh, I should probably mention that one of my needs is a countdown timer that starts at 365 seconds and stops at 0.
Thanks for your reply,
Joe

PS> I don't want to use time.sleep because the game needs to blit things to the screen while it's waiting.


On 8/5/07, Luke Paireepinart <rabidpoobear@xxxxxxxxx> wrote: Joseph Quigley wrote:
> Hi,
> I'm using PGU and Pygame and would like to fairly accurately calculate
> when 1 second has passed. What is the best way of doing this?
> Thanks,
> Joe
The pygame.time module is probably what you're looking for.
Do you want to wait for a second, or do you want to be able to determine
the progression of time?
Because if the latter, you could probably use the builtin time module.
However, I bet Pygame's waits are more accurate than time.sleep.
-Luke



--
All Your Base Are Belong To Us!!! chown -r us ./base

"After three days without programming, life becomes meaningless.'' -- Tao of Programming Book 2