State Manager
A simple pygame state manager with threaded loading using loading screens.
This means you can use heavy initialization and entry code without having to worry about it. As long as your code needs to finish up, a loading state will be displayed.
Set Up
You can choose to install either pygame or the community version pygame-ce
pip install pygame
# or
pip install pygame-ce
The package is called pgsm (PyGame State Manager)
pip install pgsm
Usage
You first write the states by inheriting from State then you put it all together with the StateManager.
Take this as a simple example with a play state and a pause state.
We want to toggle between play and pause state when the user presses "p"
import pygame as pg
from StateManager import State, StateManager
class PlayState(State):
def on_enter(self, frm):
print("Playing")
def update(self, events, dt):
# This is an example for handling events
# and changing states
for event in events:
if event.type == pg.KEYDOWN and event.key == pg.K_p:
self.exit("pause")
def draw(self, s):
"""
Whatever you want to draw
s is the surface or screen you may draw to
"""
class PauseState(State):
def on_enter(self, frm):
print("Pausing")
def update(self, events, dt):
for event in events:
if event.type == pg.KEYDOWN and event.key == pg.K_p:
self.exit("play")
def draw(self, s):
""" Whatever you want to draw """
# We use strings as keys to refer to our states
sm = StateManager({
"play":PlayState(),
"pause":PauseState()
}, start="play")
Now you can use your state machine like this. The state machine will by default just draw to the main screen.
SCREEN = pg.display.set_mode((900, 600))
while running:
dt = clock.tick(60)
events = pg.event.get()
for event in events:
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
running = False
# we could also have done the toggle here
SCREEN.fill("white")
sm(events, dt)
pg.display.flip()
The default loading state draws a little spinner. If you want to change how that works, simply override the LoadingState.
For example:
from StateManager import LoadingState
class BlankLoadingState(LoadingState):
def __init__(self, color):
self.color = color
def update(self, *args):
pass
def draw(self, s):
s.fill(self.color)
And use like this
sm = StateManager({
"play":PlayState(),
"pause":PauseState()
},
start="play",
loading_state=BlankLoadingState("black")
)
License
MIT License
Metadata
Release files for pgsm 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pgsm-0.0.2.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pgsm-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.0 kB
Release files / pgsm-0.0.2.tar.gz
| Download URL | pgsm-0.0.2.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
68845485d02352a88d7c8d8494a513bb3385341b9305dcc38e7dcd9f40457cde
|
|
BLAKE2b-256 checksum How to use checksums |
20aef9e632f54d1756c2a2aba55f7c6454529c0b96674da637c983c9ab30300b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / pgsm-0.0.2-py3-none-any.whl
| Download URL | pgsm-0.0.2-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9831c2080a672d60334e868e1fb777416e6a07110c797808f30e55e260a57b04
|
|
BLAKE2b-256 checksum How to use checksums |
f19514c4d30117f7d337f949f103acd37c7eaf64a34a89156e2526d6a9ee861b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|