开发者

Keeping track of time

I need 开发者_C百科my functions to get updated with new time values each second, is there a better way of keeping track than setting t1 the checking if t2 - t1 = 1 ?

Edit hopefully for better clarity: The orbit code:

def orbit(center, radius, speed, t):
    theta = math.fmod(t * speed, math.pi * 2)
    c = math.cos(theta)
    s = math.sin(theta)
    x = int(round(center[0] + radius * c))
    y = int(round(center[1] + radius * s))
    coords=[x, y]
    return coords

What i need is to change t in that while calling it from a draw function, what i need is to limit fps and calculate t for every frame. I only see a get_fps function in pygame which returns what the fps is not set it or am i wrong?


You can use threading's Timer Object:

http://docs.python.org/library/threading.html#timer-objects


You haven't specified anything about the needs of your code, so we have no way of telling what is "better" in your scenario. E.g. what does your app do with time, and why would you imagine there is a better way than taking a delta? Do you need different pieces of code to synchronize with each other, etc?

Since you didn't specify, I'll give an all-encompassing answer :)

No matter what solution you use, if your code needs to be in sync, then you should centralize the code that provides time.

In a real-time video game, this is often done inside the top-level game loop, and time deltas (from the previous invocation) are passed to lower level code, rather than time indices. This way, you can pass the same delta to all code, and expect it all to behave correctly.

If it doesn't need to be in sync (you're not doing simulations based on elapsed time), then just grab the current time throughout your code, and take local deltas, as you currently are. This is a fine-enough practice, because the clock code in most programming languages is high level enough that you won't have significant code duplication. You might do this in a business application (e.g. not allowing someone to take some action after a date or time), or in a progress dialog for any other application.

If you don't need synchronization, and don't actually care what the current time is, but simply need some code to be kicked off at semi-regular intervals, then simply use a timer (see utdmr's answer).

Accuracy:

If you don't have strong needs for accurate timing, then you can simply use the equivalent of a timer, or thread sleep (see utdmr's answer).

If you have mission critical needs for your timing accuracy (such as launching a rocket), then your hardware, OS, other apps, programming language etc all need to support real-time computing. This is probably way beyond what you're looking for.

If you have somewhat strong needs for accuracy (such as a game, where jitter or skips in your timing will make your simulation visibly jerk, but won't end the world), then you'll need a more complicated setup. An example of such as setup is available here:

http://gafferongames.com/game-physics/fix-your-timestep/

That code may not be arranged exactly how you need, but it can massage to suit your application's layout. The basic premise is making a producer/consumer model for time, and divvying it out in fixed increments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜