Skip to main content

PyPI Github Releases Downloads PyPI Downloads License Docs Versions

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 with pygame, pygame-ce or 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

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)

Source distribution for game-state 2.5.0
File Size Uploaded
game_state-2.5.0.tar.gz 24.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for game-state 2.5.0
File Interpreter ABI Platform
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 log

Release 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

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page