Skip to main content

A simple timer library for PyGame

Project description

Alt text

Licence Python

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
Alt text



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 seconds
  • callback (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

  • duration int - Duration in seconds
  • start_value int - The start value
  • end_value int - The end value
  • on_update callable - Returns the updated value as float
  • easing str - Sets the easing type
  • callback callable - An optional callback function, none by default

[!TIP] You can nest after and tween functions 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

  • name str - The name of you custom easing type
  • function callable - 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgtimer-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgtimer-1.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

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

Hashes for pgtimer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e08c07e369a85e62dfe21da5cb3d2d4aced923063097300c9e8fee8ea21e733c
MD5 a5815117fdcb3b03c6ca32f9d273dd5b
BLAKE2b-256 8b3f4fcd48fd92c94d3d316acdfc1a87b16512b2df4a420c297948f2351ffa49

See more details on using hashes here.

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

Hashes for pgtimer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6a9c2e85d59053088498b29560c6c34e6b415e22a395385ea18be6b23149622
MD5 af8c0bb8e62cb3b252aba22ee3d3d11c
BLAKE2b-256 31877d3349528b6520a0d270db31d05131ab0cd7e72c4cb7b1972f6f2f1d27e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page