Skip to main content

A simple state manager for pygame.

Project description

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 (PyGameStateMachine)

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):
        print("Playing")

    def update(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(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

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

pgsm-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.

pgsm-0.0.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pgsm-0.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for pgsm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0b5bd7dcf2b11dbd7c64f12d7cbf33e1565a8e1a6a6a984ade9ccf2416fd0cf1
MD5 ffa6b4d45ad0660b16c8af01257b0550
BLAKE2b-256 d3b6a678d910a09301cd502e50cddf987d30351d55e1a5d84b7bc8d05b242afb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgsm-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for pgsm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bbcfdcece1f8fa05a6d6f7ab8a0a9b4bff79e861a87052819049947e6f84f417
MD5 d20f90eb215b2c948660d69984f84bd6
BLAKE2b-256 e7c05c4ab549bd92e435f72d8b98ea48ca708c0fec0096c71f3b15f6fe61ee67

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