Skip to main content

A cooldown/counter class to wait for stuff in games

Project description

IMPORTANT

Breaking change! Properties gone!

I was aware, that there is a slight overhead when using properties, but during A benchmark, that difference turned out to be 17%.

Since this package is still marked as Alpha and probably nobody is using it besides me, the interface is now changed from properties to functions in all places.

Tests needed to be adapted, but run clean now (and also more exact using pytest.approx instead of round.

The following properties now need to be called as functions:

    cold                -> cold()
    cold.setter         -> set_cold(bool)
    hot                 -> hot()
    temperature         -> temperature()
    temperature.setter  -> set_to(val)
    remaining           -> remaining()
    remaining.setter    -> set_to(val)
    normalized          -> normalized()
    v                   -> removed, just use instance()

As stated initially, this is 17% less overhead when testing for cold, etc. Performance of LerpThing increased from 1.3mio calls to 1.8mio due to this change.

Sorry for any inconvenience, if anybody is using this, but that bad design decision needed to be fixed before any more people would use this.


pgcooldown

Cooldown & co...

This module started with just the Cooldown class, which can be used check if a specified time has passed. It is mostly indended to be used to control objects in a game loop, but it is general enough for other purposes as well.

    fire_cooldown = Cooldown(1, cold=True)
    while True:
        if fire_shot and fire_cooldown.cold:
            fire_cooldown.reset()
            launch_bullet()

        ...

With the usage of Cooldown on ramp data (e.g. a Lerp between an opaque and a fully transparent sprite over the time of n seconds), I came up with the LerpThing. The LerpThing gives you exactly that. A lerp between from and to mapped onto a duration.

    alpha = LerpThing(0, 255, 5)
    while True:
        ...
        sprite.set_alpha(alpha())
        # or sprite.set_alpha(alpha.v)

        if alpha.finished:
            sprite.kill()

Finally, the need to use Cooldown for scheduling the creations of game objects, the CronD class was added. It schedules functions to run after a wait period.

Note, that CronD doesn't do any magic background timer stuff, it needs to be updated in the game loop.

    crond = CronD()
    crond.add(1, create_enemy(screen.center))
    crond.add(2, create_enemy(screen.center))
    crond.add(3, create_enemy(screen.center))
    crond.add(4, create_enemy(screen.center))

    while True:
        ...
        crond.update()

Installation

The project home is https://github.com/dickerdackel/pgcooldown

Installing HEAD from github directly

pip install git+https://github.com/dickerdackel/pgcooldown

Getting it from pypi

pip install pgcooldown

Tarball from github

Found at https://github.com/dickerdackel/pgcooldown/releases

Licensing stuff

This lib is under the MIT license.

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

pgcooldown-0.2.13.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

pgcooldown-0.2.13-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pgcooldown-0.2.13.tar.gz.

File metadata

  • Download URL: pgcooldown-0.2.13.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for pgcooldown-0.2.13.tar.gz
Algorithm Hash digest
SHA256 4303ffa0b4f52b2674d636c7b6fadef9e34688954d07febd3c0850000c41df17
MD5 e98a853478d91ed810095d881a957699
BLAKE2b-256 24c29ac3b04348007e87a4e94d2eed2964c0cf8102c88e60ae16f4610b44cbbe

See more details on using hashes here.

File details

Details for the file pgcooldown-0.2.13-py3-none-any.whl.

File metadata

  • Download URL: pgcooldown-0.2.13-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for pgcooldown-0.2.13-py3-none-any.whl
Algorithm Hash digest
SHA256 4d7c62f99f0478a2e7e6d9cb0c4715dbe5f8deee4991df0abe84016c16e4a8ac
MD5 360e4107718787be33ac88d80a1e653f
BLAKE2b-256 7a7f5b650a218a91c8c0bf9912ede430a17372bd7fbea089438ff04ae231baba

See more details on using hashes here.

Supported by

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