Game-State
A state machine for organizing different in-game screens in python.
Table of Contents
Requirements
This library supports python versions 3.8 - 3.14.
Installation
Create and activate a virtual environment in your workspace (optional) and run the following command-
pip install game_state
Note: This package does not have any dependancy on
pygame, hence you will need to install them separately on your own. This gives you the freedom to work withpygame,pygame-ceor any other library.
Getting Started
This is an example of creating two screens. One displaying green colour and the other blue with a player.
import pygame
from game_state import State, StateManager
from game_state.utils import MISSING
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
speed = 200
pygame.init()
pygame.display.init()
pygame.display.set_caption("Game State Example")
class MyBaseState(State["MyBaseState"]):
window: pygame.Surface = MISSING
# Attributes we want all our states to share.
def process_event(self, event: pygame.event.Event) -> None:
pass
def process_update(self, dt: float) -> None:
pass
class MainMenuState(MyBaseState, state_name="MainMenu"):
def process_event(self, event: pygame.event.Event) -> None:
if event.type == pygame.QUIT:
self.manager.is_running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
self.manager.change_state("Game")
def process_update(self, dt: float) -> None:
self.window.fill(GREEN)
pygame.display.update()
class GameState(MyBaseState, state_name="Game"):
def __init__(self) -> None:
self.player_x: float = 250.0
def process_event(self, event: pygame.event.Event) -> None:
if event.type == pygame.QUIT:
self.manager.is_running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
self.manager.change_state("MainMenu")
def process_update(self, dt: float) -> None:
self.window.fill(BLUE)
pressed = pygame.key.get_pressed()
if pressed[pygame.K_a]:
self.player_x -= speed * dt
if pressed[pygame.K_d]:
self.player_x += speed * dt
pygame.draw.rect(
self.window,
"red",
(self.player_x, 100, 50, 50),
)
pygame.display.update()
def main() -> None:
window = pygame.display.set_mode((500, 600))
state_manager = StateManager(bound_state_type=MyBaseState, window=window)
state_manager.load_states(MainMenuState, GameState)
state_manager.change_state("MainMenu")
clock = pygame.time.Clock()
while state_manager.is_running:
dt = clock.tick(60) / 1000
for event in pygame.event.get():
state_manager.current_state.process_event(event)
state_manager.current_state.process_update(dt)
if __name__ == "__main__":
main()
You can have a look at the game state guide for a more detailed explaination.
Links
- Guide & API Reference: https://game-state.readthedocs.io/en/stable/
- PyPI Page: https://pypi.org/project/game-state/
- Github Page: https://github.com/Jiggly-Balls/game-state/
Metadata
Release files for game-state 2.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| game_state-2.5.0.tar.gz | 24.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| game_state-2.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 50.4 kB
Release files / game_state-2.5.0.tar.gz
| Download URL | game_state-2.5.0.tar.gz |
|---|---|
| Size | 24.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c284a5029ea4cf70b9739d1cecec643dd9ab926bff2751da5409840e6ab087a
|
|
BLAKE2b-256 checksum How to use checksums |
4f693c3dcbd7ca22bf22d0223334a7e028d737e109c7f344f929192193770cf6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 25, 2026.
Transparency logRelease files / game_state-2.5.0-py3-none-any.whl
| Download URL | game_state-2.5.0-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e850f869fe959a585741f2924c7a0ac4a47522579380df2e19df1af19a947339
|
|
BLAKE2b-256 checksum How to use checksums |
13584326d528f8a9a071e618af5a2f820d2f543583e549fd82d60589005874d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 25, 2026.
Transparency log