Skip to main content

A pygame utility package that allows you to handle different screens in an organized manner.

Project description

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 of it's forks.

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"]):
    screen: pygame.Surface = MISSING


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, *args: float) -> None:
        self.screen.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, *args: float) -> None:
        dt = args[0]

        self.screen.fill(BLUE)

        # Player movement-
        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.screen,
            "red",
            (
                self.player_x,
                100,
                50,
                50,
            ),
        )
        pygame.display.update()


def main() -> None:
    screen = pygame.display.set_mode((500, 600))

    state_manager = StateManager[MyBaseState](
        bound_state_type=MyBaseState, screen=screen
    )
    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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

game_state-2.3.1.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

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

game_state-2.3.1-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file game_state-2.3.1.tar.gz.

File metadata

  • Download URL: game_state-2.3.1.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for game_state-2.3.1.tar.gz
Algorithm Hash digest
SHA256 a12ef00bcf5309b128d7b5a1f1b1c73c02bc97860f12f302d31c71815f1a18c6
MD5 e7c96e8349642013171c5c32b67a7f8c
BLAKE2b-256 d78a9a95e68bda107504aa04cc6272efdb8693b0b2e16b8cdb7755c70986f6a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for game_state-2.3.1.tar.gz:

Publisher: python-publish.yml on Jiggly-Balls/game-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file game_state-2.3.1-py3-none-any.whl.

File metadata

  • Download URL: game_state-2.3.1-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for game_state-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1cfec355318a6b32ea2962cbad90f6cc636f2a068e5003b532c31ab8f0c9805
MD5 248ed5bbafd7651c987871e11d986c0d
BLAKE2b-256 a5754112d83a7b07a1cdc2e466e092f7e6b3bfdc23c860075c8f535b56f5e37e

See more details on using hashes here.

Provenance

The following attestation bundles were made for game_state-2.3.1-py3-none-any.whl:

Publisher: python-publish.yml on Jiggly-Balls/game-state

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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