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

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



"Ian Mallett" <geometrian@xxxxxxxxx> wrote:
> You could try:
> "http://www.pygame.org/docs/ref/time.html#pygame.time.Clock";
> with Clock.tick(framerate) which would normalize the speed of the program
> on any computer.

I wouldn't go about it that way - tick(framerate) tries to achieve the
framerate, but if you're running on a slow machine and can only run at
10Hz, calling Clock.tick(20) won't make it catch up.

That's even what the documentation says:

"By calling Clock.tick(40) once per frame, the program will never run at
more than 40 frames per second."

Also, read on:

"Note that this function uses SDL_Delay function which is not accurate on
every platform"

A few milliseconds every frame, over 15 minutes, who knows how far out of
sync you'll end up?


> On 7/19/07, Daniel Nixon <dan.nixon@xxxxxxxxx> wrote:
>>
>> Hi list,
>>
>> I'm working on a game in which the player looks after a fishtank full
>> of fish. Each fish ages, gestates while pregnant, grows hungrier, etc.
>> For arguments sake lets say 15 minutes = 1 fish year. I want this
>> passage of time to be independent of frame rate and iterations through
>> the main game loop (or do I?).

That plan sounds fine to me.

The way I do this sort of thing is to create a clock (as suggested by Ian)
and read the amount of time each time through the loop takes, and pass
that duration down into your simulation code:

theClock=pygame.time.Clock()

while (gameIsPlaying):
  frameTime=theClock.tick()

  processInput()
  doSim(frameTime)
  renderFrame()


Inside your doSim function, update the game logic based on frameTime going
by.

It might look a little like this:

FISH_DAYS_PER_MILLISECOND=0.0004
fishDays=0

def doSim(milliseconds):
  fishDays += milliseconds*FISH_DAYS_PER_MILLISECOND


And, you should find that after about 15 minutes of the player's time, the
fishDays value has climbed up to about 365 days.

I also usually have ways to suspend the sim, so that if the user leaves
the window, or brings up a pause menu, or whatever, I no longer call the
doSim function, which means the game clock isn't counting up.


If you want to combine this with Ian's suggestion, and add a frame rate
hint to the tick() call, you can, but even if you don't, you should be
able to get some good results.


-Dave LeCompte