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

Re: [pygame] Adjusting Timing With a User Event



Kris Schnee wrote:
How can I go about adjusting the speed of in-game events based on the current FPS? That is, my physics sim works in steps with a known time interval, but the _game_ might have varying FPS, and I want a thrown ball to move at a constant rate in real time regardless of how many animation frames/simulation steps happen.

Right now I've got a Pygame "clock" in my main loop to regulate game speed to a max of 30 FPS. I just now added the line:

self.time_interval = min(1.0 / clock.get_fps(),.3)

Which should change the amount of time that a simulation step represents to be either the amount of real time since the last step, or .3 s, whichever is smaller. (Bad things happen if the interval gets too big.) But it's stupid to make this adjustment every frame, right? Better to only do it once every few seconds at most. The most elegant way to do that in Pygame is probably to set a user event, but how do I do that and make it appear once every [spam] milliseconds?
The only problem with a user event would be in that it would take up one event slot (since you're writing a library you'd want to try to restrict things like that, I'd assume.)
actually, another problem is that your physics sim would have to have access to the event queue, or the client would have to call fix_yourself (or whatever you name the function) whenever the event arose.
IMHO, the client shouldn't be required to keep your sim updated in that way.


   pygame.time.set_timer(EventCode,HowOftenToEnqueueIt)

that's all you have to do, I believe.
HowOften is in milliseconds, so 300 would be every .3 milliseconds.
HTH,
-Luke


Kris