A pygame utility package that allows you to handle different screens in an organized manner.
Project description
Game-State
A state machine for organizing different in-game screens for pygame.
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 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
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
speed = 200
pygame.init()
pygame.display.init()
pygame.display.set_caption("Game State Example")
class MainMenuState(State, 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(State, state_name="Game"):
def __init__(self) -> None:
self.player_x = 250
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:
screen = pygame.display.set_mode((500, 600))
state_manager = StateManager(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
- 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/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file game_state-2.2.0.tar.gz.
File metadata
- Download URL: game_state-2.2.0.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daf96d03ae989dd7324501b3354d4cd071d36dff6746c4737b858c6eec22d28b
|
|
| MD5 |
2493cc45417aa52dc41d7fdfd2af634c
|
|
| BLAKE2b-256 |
4af024562cec8e0bc3dcdc77057d77437128af3062acd95aee40568e768e1878
|
Provenance
The following attestation bundles were made for game_state-2.2.0.tar.gz:
Publisher:
python-publish.yml on Jiggly-Balls/game-state
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
game_state-2.2.0.tar.gz -
Subject digest:
daf96d03ae989dd7324501b3354d4cd071d36dff6746c4737b858c6eec22d28b - Sigstore transparency entry: 757457441
- Sigstore integration time:
-
Permalink:
Jiggly-Balls/game-state@92efd7f460d9731c07f036232bf0569cbb13ef6a -
Branch / Tag:
refs/tags/v2.2.0 - Owner: https://github.com/Jiggly-Balls
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@92efd7f460d9731c07f036232bf0569cbb13ef6a -
Trigger Event:
release
-
Statement type:
File details
Details for the file game_state-2.2.0-py3-none-any.whl.
File metadata
- Download URL: game_state-2.2.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b54f678a91ce24adf991578c0a3c2d8386d46665cf185ebccc868ac743588ec3
|
|
| MD5 |
c8f2cc443e18438352bf2a1c4bbebf75
|
|
| BLAKE2b-256 |
9b968dc002a99738084829cdbd636c3d5ef0cdcb9764aafc9f30e2ae33445149
|
Provenance
The following attestation bundles were made for game_state-2.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on Jiggly-Balls/game-state
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
game_state-2.2.0-py3-none-any.whl -
Subject digest:
b54f678a91ce24adf991578c0a3c2d8386d46665cf185ebccc868ac743588ec3 - Sigstore transparency entry: 757457455
- Sigstore integration time:
-
Permalink:
Jiggly-Balls/game-state@92efd7f460d9731c07f036232bf0569cbb13ef6a -
Branch / Tag:
refs/tags/v2.2.0 - Owner: https://github.com/Jiggly-Balls
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@92efd7f460d9731c07f036232bf0569cbb13ef6a -
Trigger Event:
release
-
Statement type: