Skip to main content

Coroutine runner for games

Project description

game_coro

Coroutines for games. Write logic as a python generator, then tick it every frame. Use yield to pause till the next frame, or use helpers to pause a given number of frames or seconds.

This library is currently experimental: no tests, few docs. For usage, check out the example: example/example.py

Run the example by cloning this repository, then:

python -m example.example

Design, Behavior

Coroutines should not expect raised exceptions to be swallowed.
Just like any other call, exceptions will crash the game.

Coroutines can wrap themselves in try-finally to implement cleanup logic.

Coroutines should use yield from to delegate to another generator function.

When a coroutine is stopped, generator .close() is called. This follows standard python rules, where a GeneratorExit is raised within the generator.

When coroutine A yields to coroutine B, the return value of B is passed to A. Any raised exception from B is raised in A. Just like a normal call stack.

When implementing a state machine, back-and-forth yield from other_state() will eventually fill up the call stack, crashing the game. Instead, return other_state(). return will close the current generator function, and the coroutine manager will detect that you've returned another generator and start iterating it. This only works when returning from your top-level generator. If in a nested generator call, use trampoline(): yield from trampoline(other_state())

Tips and Tricks

return (yield from self.other())

Allows self.other() to return a value, which will be returned through Can trampoline from it.

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

game_coro-0.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

game_coro-0.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file game_coro-0.0.1.tar.gz.

File metadata

  • Download URL: game_coro-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.6.1 CPython/3.11.2

File hashes

Hashes for game_coro-0.0.1.tar.gz
Algorithm Hash digest
SHA256 386ae47b4e1690c1958b879d64d214f2786f1e064fb8891e2ee8a7d28a22aed6
MD5 1dc95ded918cd5e837bec261140e5a2d
BLAKE2b-256 3b74aad0cae598fb05df4fee34c21f8188e9d5b461e988321aefe9521ab2b4b0

See more details on using hashes here.

File details

Details for the file game_coro-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: game_coro-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.6.1 CPython/3.11.2

File hashes

Hashes for game_coro-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9847b5f84fc0b2f8bdbbf717bf2ec14ae0cad602d4d20f6c710a40bb0fbfa2d
MD5 be5c3b8913fca35fe1c6a36f62950f6d
BLAKE2b-256 158c1ecf5de70e6f712a924ddb6e5bf025e1ca3f3b51cb3b6cbc3ec5cec1b30c

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