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

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



The first time I did something like that I made a fish class which
did things like get-hungry.  Each fish stored its time of creation
and time of last feeding, mating, egg-laying etc.  Then I kept doing:


     for every fish:
	       check the time of day, and update the fishes' state
	       based on the time elapsed

This worked fine for a few fish.  But it did not scale.  When I had
10s of thousands of fish, I couldn't get through the whole list of
fish before too much time had passsed.  My fish were carrying too
much state around with them as well.

So I changed the design to make my fish schedule 'at this time
something interesting will happen to me' events into a queue which
was sorted by time.  Then, for every bit of time, I only had to see
if it was later than or equal to the first item in the queue, and
pop things off the queue until the first item on it was 'later than
now'.

Just in case you run into the same problem that I had.

Laura