Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Pygame, normalizing game-speed across different fps values

Tags:

python

pygame

I'm messing around with Pygame, making some simple games just to learn it. However, I'm having a hard time implementing fps the way that I want.

From what I understand, fps is generally set with:

import pygame
...
clock = pygame.time.Clock()
while True:
    clock.tick(60)

And then throughout the program, I make sure that every loop/frame is written to take 1/60th of a second, so I can, for example, make objects move at the speed I want them too. Increasing the tick to 120 fps will make the game run too fast, while decreasing it will make the game run too slow.

However, this is not how I'm familiar with fps working for any other game. In most games, fps can vary as much as you want (usually based on how well the system is running the application), but the game will always run at the same speed (e.g. moving 100 pixels across the screen will take 1 second no matter how many frames happened in that second).

The only way I can think of getting it to work the way I want is to grab the current fps every frame, and factor that into the calculations of every movement or time based event. But that seems needlessly complicated, and I'm wondering if I'm completely missing a part of pygame functionality that figures that out for me.

like image 700
Nordom Whistleklik Avatar asked Nov 27 '12 19:11

Nordom Whistleklik


People also ask

What is pygame time clock ()?

pygame.time.ClockThis function is used to create a clock object which can be used to keep track of time. The various methods of clock object are below: tick():This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.


1 Answers

Games use a fixed-timestep for physics, while allowing the video timestep (fps) to vary. This means your update(delta) function gets called with a constant delta value. This maintains stability.

This means in practice, update may end up being called multiple times on average per single call of draw(), depending on how much time elapses.

For details see: Gaffer's "fix your timestep"

A larger (python) example is at cookbook: ConstantGametime

like image 57
ninMonkey Avatar answered Sep 23 '22 18:09

ninMonkey