Skip to content

A flexible and efficient scene manager for Pygame, allowing easy organization and switching between different scenes in your game.

Notifications You must be signed in to change notification settings

DumbChester021/pygame-scene-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Pygame Scene Manager

Pygame Scene Manager Logo

A flexible and efficient scene manager for Pygame, allowing easy organization and switching between different scenes in your game.

Table of Contents

Introduction

The Pygame Scene Manager is designed to simplify the management of different scenes within a Pygame-based game or application. It allows you to seamlessly switch between scenes, handle events, and maintain clean code organization.

Features

  • Scene switching with cleanup for efficient resource management.
  • Conditional rendering to optimize performance by rendering only active scenes.
  • Proper event handling, ensuring that only the active scene processes events.
  • Consistent coding style and clear documentation for ease of use and maintenance.
  • Basic scenes included (Main Menu and Settings), with the ability to expand for custom scenes.

Getting Started

Installation

To use the Pygame Scene Manager, follow these steps:

  1. Clone the repository to your local machine:

    git clone https://github.com/yourusername/pygame-scene-manager.git
  2. Navigate to the project directory:

    cd pygame-scene-manager
  3. Install the required dependencies:

    pip install -r requirements.txt

Usage

  1. Import the SceneManager and your desired scenes:

    from sceneManager import SceneManager
    from mainMenuScene import MainMenuScene
    from settingsScene import SettingsScene
  2. Initialize Pygame and create a Pygame display:

    import pygame
    
    pygame.init()
    
    screen = pygame.display.set_mode((800, 600))
  3. Create an instance of the SceneManager:

    scene_manager = SceneManager(screen)
  4. Add the scenes to the SceneManager :

    main_menu_scene = MainMenuScene(screen, scene_manager)
    scene_manager.add_scene("MainMenuScene", main_menu_scene)
    
    settings_scene = SettingsScene(screen, scene_manager)
    scene_manager.add_scene("SettingsScene", settings_scene)
    
    new_game_scene = NewGameScene(screen, scene_manager)
    scene_manager.add_scene("NewGameScene", new_game_scene)
  5. Set the initial scene and Run the game loop with the run_current_scene method:

    #Set the Initial Scene
    scene_manager.switch_scene("MainMenuScene")
    
    while True:
        scene_manager.run_current_scene()
        pygame.display.flip()

Creating New Scenes

You can create custom scenes by following these steps:

  1. Create a New Scene Class:

    • Create a new Python file for your scene, e.g., customScene.py.

    • Implement the necessary methods (setup, update, render, handle_event) in your scene class.

    Example (Used NewGame Scene as an Example):

    # NewGameScene.py
    import pygame
    
    class NewGameScene:
       def __init__(self, screen, scene_manager):
          self.screen = screen
          self.scene_manager = scene_manager
          self.font = pygame.font.Font(None, 36)
    
          self.title_text = self.font.render("New Game", True, (255, 255, 255))
          self.title_rect = self.title_text.get_rect(center=(screen.get_width() // 2, 100))
    
          self.game_started = False
    
       def setup(self):
          self.game_started = True
          # Add any additional setup logic for starting a new game
    
       def cleanup(self):
          pass
    
       def update(self):
          # Add update logic for the new game scene
          pass
    
       def render(self):
          self.screen.fill((0, 0, 0))
    
          self.screen.blit(self.title_text, self.title_rect)
    
          if self.game_started:
                # Render game-related content here
                pass
          else:
                # Render new game scene content
                pass
    
       def handle_event(self, event):
          if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                   self.handle_selection()
    
       def handle_selection(self):
          # Handle new game scene selection (e.g., starting the game)
          print("Starting a new game")
  2. Import and Use the New Scene:

    • Import the new scene in your main script.
    • Add the custom scene to scenemanager at main.

    Example:

    from sceneManager import SceneManager
    from customScene import CustomScene
    
    custom_scene = CustomScene(screen, scene_manager)

Switching Scenes

You can switch between scenes in your game:

  • Call the switch_scene method on the SceneManager instance.
  • Pass the name of the scene you want to switch to (needs to match the name you registered it to when adding it in the main.py).

Example:

# Switch to the CustomScene
scene_manager.switch_scene("CustomScene")

Scenes

Main Menu Scene

The main menu scene provides a basic menu structure with options for continuing the game, starting a new game, accessing settings, and quitting the game.

Settings Scene

The settings scene allows the user to toggle debug mode on and off, providing essential details for developers, such as FPS and other debugging information.

New Game Scene

The New Game scene is a blank example of another scene that you can use and copy to create new custom scenes

Optimizations

The Pygame Scene Manager includes optimizations for:

  • Memory Management
  • Render Efficiency
  • Event Handling
  • Adding Custom Scenes
  • Switching Scenes
  • Code Cleanup and Clarity
  • Consistency and Readability

Contributing

Contributions are welcome! If you have suggestions, bug reports, or feature requests, please open an issue or create a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.


About

A flexible and efficient scene manager for Pygame, allowing easy organization and switching between different scenes in your game.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages