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

Re: [pygame] frame independant movement





On Sun, Jan 31, 2010 at 1:44 PM, Yanom Mobis <yanom@xxxxxxxxxxxxxx> 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.


One way to do this is to use a time delta.
I.E.

prev_time = get_time()
while frames_updating:

    update_display()
    delta = get_time() - prev_time
    move_distance = delta * 64 #(would move 64 pixels per second because delta is a fraction of a second)
    prev_time = get_time()

this is pseudocode, depending on your platform you'll have to figure out what is best to use for get_time() function.  For example on some platforms time.time will give you microsecond resolution and on some other platforms only 1-second resolution.  Pygame has a function you can use for this in the Clock() module.

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.