Skip to content

Jiggly-Balls/game-state

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages