A flexible and efficient scene manager for Pygame, allowing easy organization and switching between different scenes in your game.
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.
- 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.
To use the Pygame Scene Manager, follow these steps:
-
Clone the repository to your local machine:
git clone https://github.com/yourusername/pygame-scene-manager.git
-
Navigate to the project directory:
cd pygame-scene-manager
-
Install the required dependencies:
pip install -r requirements.txt
-
Import the
SceneManager
and your desired scenes:from sceneManager import SceneManager from mainMenuScene import MainMenuScene from settingsScene import SettingsScene
-
Initialize Pygame and create a Pygame display:
import pygame pygame.init() screen = pygame.display.set_mode((800, 600))
-
Create an instance of the
SceneManager
:scene_manager = SceneManager(screen)
-
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)
-
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()
You can create custom scenes by following these steps:
-
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")
-
-
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)
You can switch between scenes in your game:
- Call the
switch_scene
method on theSceneManager
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")
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.
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.
The New Game scene is a blank example of another scene that you can use and copy to create new custom scenes
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
Contributions are welcome! If you have suggestions, bug reports, or feature requests, please open an issue or create a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.