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 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.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

game_state-2.0.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: game_state-2.0.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for game_state-2.0.1.tar.gz
Algorithm Hash digest
SHA256 66f0e3456ef85da4c4e879248ea51fac8d6b6b84701a0e33943afe1216552608
MD5 bb67ff7a14e1e40b5de7f7272938c1fa
BLAKE2b-256 ba0918abbca3b1e6e646fd99a70c3b38c34fb1fa5a4f57ccd6578a871306ccca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: game_state-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for game_state-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b929a593f083e60b5ff44c65c2a468e664907c3eec4e7f0b2cb2e9902b1f727
MD5 5da22d4cc849412495a53613846bbfb5
BLAKE2b-256 e11cb2a7dbb3fa0d57e338321e1027f92f52434720c6f68e88eb6826abb9efd5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page