asweigart / fpstimer

A clock timer that provides sleep()-like features for maintaining a certain "frames per second" (FPS) framerate in Python 2 and 3.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fpstimer

A clock timer that provides sleep()-like features for maintaining a certain "frames per second" (FPS) framerate in Python 2 and 3.

Sometimes you'll want to slow down your computer so it doesn't run too fast for the user. For example, if you want to run a video game at 60 frames per second (FPS), but the game can render the graphics for the screen in less than 1/60 second, you'll need the program to pause for however much time is remaining in that 1/60 second. This varible amount of time can be calculated by FPS Timer.

Install

pip install fpstimer

Usage

The frame rate is set by passing the integer FPS to the FPSTimer() constructor. The FPSTimer object has an sleep() method that pauses for a variable amount of time needed to maintain that framerate.

For example, calling FPSTimer(10) creates a timer for 10 fps. Each "frame" should last for 1/10 of a second. Running your program without an FPS timer could cause your program to run too fast for the user, especially as CPUs get faster. After running the code for a single frame, calling sleep() will pause the program for however much is needed for the remaining 1/10 second for that frame. This pause is calculated from the last time that sleep() was called for the previous frame.

>>> import fpstimer
>>> timer = fpstimer.FPSTimer(60) # Make a timer that is set for 60 fps.
>>> for i in range(100): # Each iteration of this loop will last (at least) 1/60 of a second.
...     # Do calculations/work for a single "frame" here.
...     timer.sleep() # Pause just enough to have a 1/60 second wait since last fpstSleep() call.

Support

If you find this project helpful and would like to support its development, consider donating to its creator on Patreon.

About

A clock timer that provides sleep()-like features for maintaining a certain "frames per second" (FPS) framerate in Python 2 and 3.

License:GNU General Public License v3.0


Languages

Language:Python 100.0%