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

Re: [pygame] frame independant movement



Luke, you said:

"Another thing you can do is just use Clock to limit the FPS to 60 or something, but then your game will run slower if you're not getting 60 fps."

Well, true, but you MUST determine the framerate that  you want for your game, otherway you will get a 60 FPS in your PC, 70 in another and 120 in another one that is much more faster/newer than yours and in witch your game will turn unusable at this framerate....

I do this:

while(true): #principal loop
    # get current time
    t = time.time()   
    ....
    doAll()
    ....
    #At the end i get the time needed to do the calculatons & flip
    T = t - time.time()
    delta = T - 1/desiredFrameRate
    if (delta > 0): # if the cycle has been done too fast
        time.wait(delta)
    ####This way you have your desired framerate or the maximum possible. Additionally you can do this:
    else:
        GLOBAL_T = GLOBAL_STANDAR_T - delta # delta is <= 0, - and - is +

Being GLOBAL_T the t that you use for your position calculations and global_standar_t the value of global_t if delta is 0.

Pseudocode, sure


2010/1/31 DR0ID <dr0id@xxxxxxxxxx>
On 31.01.2010 20:44, Yanom Mobis wrote:
How do you make things happen ingame at a constant speed regardless of frames-per-second? For example, i want my game object to move one square per second, even if the game is running 30, 45, 60, or 90 fps.


Hi

make it time dependent.

<code>
# dt is the time passed since the last frame
# use a dt = min(100, dt) to make sure the longest possible frame is 100 ms long
# this prevents 'jumps' when a frame took long (as moving the window on windows)

def update(self, dt):
    self.position += self.velocity * dt
</code>

This solution may have other side effects.

~DR0ID



--
Nota: Tildes omitidas para evitar incompatibilidades.

:wq