https://github.com/jiggly-balls/game-state
A pygame utility package that allows you to handle different screens in an organized manner.
https://github.com/jiggly-balls/game-state
game oop pygame pygame-ce python python3 state-management utility
Last synced: 3 months ago
JSON representation
A pygame utility package that allows you to handle different screens in an organized manner.
- Host: GitHub
- URL: https://github.com/jiggly-balls/game-state
- Owner: Jiggly-Balls
- License: mit
- Created: 2024-10-26T04:03:08.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-28T15:23:01.000Z (4 months ago)
- Last Synced: 2025-06-28T16:40:33.954Z (4 months ago)
- Topics: game, oop, pygame, pygame-ce, python, python3, state-management, utility
- Language: Python
- Homepage: https://game-state.readthedocs.io/en/stable/
- Size: 268 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pypi.org/project/game-state/)
[](https://github.com/Jiggly-Balls/game-state/releases)
[](https://pypi.org/project/game-state/)
[](https://pepy.tech/projects/game-state)
[](https://github.com/Jiggly-Balls/game-state/blob/main/LICENSE)
[](https://game-state.readthedocs.io/en/latest/)

# Game-State
A pygame utility package that allows you to handle different screens in an organized manner.
## Table of Contents
- [Analytics](#analytics)
- [Requirements](#requirements)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [Links](#links)
### Analytics

### Requirements
This library supports python versions `3.8` - `3.13`.
### 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.
```py
import pygame
from game_state import State, StateManager
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
speed = 100
pygame.init()
pygame.display.init()
pygame.display.set_caption("Game State Example")
class ScreenOne(State, state_name="FirstScreen"):
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_c:
self.manager.change_state("SecondScreen")
def process_update(self, dt: float) -> None:
self.window.fill(GREEN)
pygame.display.update()
class ScreenTwo(State, state_name="SecondScreen"):
def on_setup(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_c:
self.manager.change_state("FirstScreen")
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, 700))
state_manager = StateManager(screen)
state_manager.load_states(ScreenOne, ScreenTwo)
state_manager.change_state("FirstScreen")
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](https://game-state.readthedocs.io/en/latest/guide.html#using-the-library) for a more detailed explaination.
### Links
- Guide & API Reference: https://game-state.readthedocs.io/en/stable/
- PyPI Page: https://pypi.org/project/game-state/