A simple timer library for PyGame
Project description
PgTimer - A Simple PyGame Timer Library
A lightweight Python timer and tweening library insired by Love2D's Hump library with built-in support for multiple easing functions from pytweening, designed for easy time-based animations and delayed callbacks.
Contents
Instalation
git clone https://github.com/clxakz/pgtimer
or
pip install pgtimer
Quick start
place the pgtimer folder inside your project and import it
from pgtimer import Timer
Update the Timer
In your main loop update the Timer with delta time
[!NOTE] There's no need to instantiate the timer using
timer = Timer()since an instance is already created and exposed as Timer at the module level
while running:
dt = clock.tick(60) / 1000
Timer.update(dt)
Create a pulsing rectangle
As an example we can use tweens to pulse a rectangle on the screen
See pulse.py for full example
surface = pygame.Surface((250, 250))
surface.fill((255,255,255))
alpha = 0
def set_alpha(value):
global alpha
alpha = value
def pulse():
Timer.tween(1, 0, 255, set_alpha, "linear", lambda: # Tween from 0 to 255
Timer.after(1, lambda: # Wait 1 second
Timer.tween(1, 255, 0, set_alpha, "linear", pulse) # Tween from 255 to 0 and repeat pulse
)
)
pulse()
# In your mainloop
surface.set_alpha(alpha)
screen.blit(surface, (125, 125))
And that looks like
Documentation
.after(delay, callback)
The after() function will run a callback function after a set time
Timer.after(1, print("Done!")) # <- Prints 'Done!' after 1 second
Arguments
delay(int)- Time in secondscallback(callable)- An optional callback function, none by default
.tween(duration, start_value, end_value, on_update, easing, callback)
The tween() function animates a value smoothly from start_value to end_value over duration seconds, calling on_update(value) each frame with the eased value. Supports custom easing types and an optional callback when complete
alpha = 0
def set_value(value):
global alpha
alpha = value
Timer.tween(1, 0, 255, set_value, "easeOutQuad", print("Done!")) # <- Smoothly animates alpha from 0 to 255 using the easeOutQuad easing type. Prints 'Done!' when finished.
Arguments
durationint- Duration in secondsstart_valueint- The start valueend_valueint- The end valueon_updatecallable- Returns the updated value as floateasingstr- Sets the easing typecallbackcallable- An optional callback function, none by default
[!TIP] You can nest
afterandtweenfunctions to build a sequence of animations
.add_easing_type(name, function)
The add_easing_type() function can be used to add your own custom easing type
def easeInCustom(t):
return math.pow(t, 3)
Timer.add_easing_type("custom", easeInCustom)
Arguments
namestr- The name of you custom easing typefunctioncallable- The function for your custom easing type
.get_easing_types()
The get_easing_types() function will print a full list of all available easing types
Timer.get_easing_types()
You can use any of the following easing types in .tween(), thanks to pytweening for providing these:
- linear
- easeInQuad
- easeOutQuad
- easeInOutQuad
- easeInCubic
- easeOutCubic
- easeInOutCubic
- easeInQuart
- easeOutQuart
- easeInOutQuart
- easeInQuint
- easeOutQuint
- easeInOutQuint
- easeInSine
- easeOutSine
- easeInOutSine
- easeInExpo
- easeOutExpo
- easeInOutExpo
- easeInCirc
- easeOutCirc
- easeInOutCirc
- easeInElastic
- easeOutElastic
- easeInOutElastic
- easeInBack
- easeOutBack
- easeInOutBack
- easeInBounce
- easeOutBounce
- easeInOutBounce
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgtimer-1.0.0.tar.gz.
File metadata
- Download URL: pgtimer-1.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e08c07e369a85e62dfe21da5cb3d2d4aced923063097300c9e8fee8ea21e733c
|
|
| MD5 |
a5815117fdcb3b03c6ca32f9d273dd5b
|
|
| BLAKE2b-256 |
8b3f4fcd48fd92c94d3d316acdfc1a87b16512b2df4a420c297948f2351ffa49
|
File details
Details for the file pgtimer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pgtimer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6a9c2e85d59053088498b29560c6c34e6b415e22a395385ea18be6b23149622
|
|
| MD5 |
af8c0bb8e62cb3b252aba22ee3d3d11c
|
|
| BLAKE2b-256 |
31877d3349528b6520a0d270db31d05131ab0cd7e72c4cb7b1972f6f2f1d27e9
|