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 pygame utility package that allows you to handle different screens in an organized manner.

Table of Contents

Analytics

Alt

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.

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 for a more detailed explaination.

Links

Project details


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.0.2.tar.gz (11.5 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.0.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: game_state-2.0.2.tar.gz
  • Upload date:
  • Size: 11.5 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.0.2.tar.gz
Algorithm Hash digest
SHA256 b311d00e8adf9c2aeb9c1a6dc97487ecada17a121cfd0e16543cf88b474f8b70
MD5 15a224db8643884515f18304807c5ad5
BLAKE2b-256 9e6cacacb97147fda475d633bc08c3c92a73da4a9d64b8cab5b030b2d4264c96

See more details on using hashes here.

Provenance

The following attestation bundles were made for game_state-2.0.2.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.0.2-py3-none-any.whl.

File metadata

  • Download URL: game_state-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.7 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.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b87f4c8ad3527d3bf61c9024d00e7f7c02f171165dc2fb509abc3b3cc31553c9
MD5 8d2082295a5199487bfef7db6823c187
BLAKE2b-256 f4b0fe07907ca0bc8318ef5f189fa7f3913b4db03ec745027353d9382b36f81b

See more details on using hashes here.

Provenance

The following attestation bundles were made for game_state-2.0.2-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