A simple yet featureful framework for simplifying pygame game creation.
Project description
Pyrite
The Foolproof Game Framework
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
About The Project
Pyrite is a framework for eliminating much of the boilerplate of setting up a pygame project and getting running, while encouraging good project architecture with proper seperation of responsibilities among game elements. It includes several built-in systems to get development up and going.
Getting Started
Pyrite is written in pure python, with no system dependencies, and should be OS-agnostic.
Installation
Pyrite can be installed from the PyPI using pip:
pip install pyrite-framework
and can be imported for use with:
import pyrite
Pyrite additionally is built for pygame-ce, which must also be installed.
Usage
At its core, Pyrite is built around a single main class: the Game class. Game is useable as-is in its default state, or it can be subclassed to allow for more specific behaviors.
Using Game
A game can be started in multiple ways.
- Traditional way
import pyrite
my_game = pyrite.Game() # Creating with default settings
# ----------------------
#
# Game setup stuff here
#
# ----------------------
my_game.main()
This will create a window with a black background, at the default size (800x600), with a caption of "Game"
Keep in mind, anything after calling main() will be on hold until after the game stops running.
- Context Manager
Alternatively, we can use python's with syntax to create a game.
import pyrite
with pyrite.Game() as my_game:
# ----------------------
#
# Game setup stuff here
#
# ----------------------
As with the above example, this will start a game with default settings. As a context manager, Game will start itself once the context ends.
Using context manager syntax offers a couple benefits.
-
Avoids needing to manually call main().
This is minor, but it's an additional step that needs to be minded otherwise.
-
Indentation helps make it clear what code is being used to set up the game.
-
Errors are captured.
Any error that occurs during the set up will be captured by the context manager, and Game has a setting to suppress these errors.
Game Settings
Game has several data classes that hold information for it. You can construct the yourself and pass them to the game instance, or you can pass their parameters as keywords into the Game constructor.
# This:
display_settings = DisplaySettings((400, 300))
with Game(display_settings=display_settings) as my_game:
...
# Is the same as this
with Game(resolution=(400, 300)) as my_game:
...
Loop Phases
A game is built around a loop, and that loop has certain phases. The pyrite game loop offers these phases:
-
Events: The pygame event queue is processed
-
Const_update: Runs at a fixed rate, regardless of frame rate. Useful for time-invariant things, like physics.
-
Pre_update: Runs earlier than the main update phase.
-
Update: Main update phase. Most logic is run here.
-
Post-update: Runs after the main update phase.
-
Render: For drawing to the screen.
The basic Game class will call each of the phases on all enabled Entities and Renderables, but also has these available as methods to allow for more specific behavior when subclassed.
Entities and Renderables
These are the core feature of Pyrite. Most objects in your game should inherit from at least one of these. They allow you to define behaviors, and render out images onto the screen.
Entity
An entity is any object that has behavior. They can be typical objects, like an enemy, or obstacle, or they can be systems and services, like a physics service. To use, simply subclass Entity, and overwrite at least one of the four update-phase methods or the on_event method. Then, when the entity is created, it will automatically have those methods called each frame.
class MyEntity(Entity):
def __init__(self, container=None, enabled=True):
super().__init__(container, enabled)
self.position: tuple[int, int] = (0, 0)
def update(self, delta_time: float):
keys = pygame.key.get_pressed()
x = y = 0
if keys[pygame.K_w]:
y -= 1
if keys[pygame.K_s]:
y += 1
if keys[pygame.K_a]:
x -= 1
if keys[pygame.K_d]:
x += 1
self.move((x, y))
def move(self, direction: tuple[int, int]):
self.position = (
self.position[0] + direction[0], self.position[1] + direction[1]
)
This will create a simple entity that will move with the WASD keys. Note, however, that it does not show anything on screen, as it is not renderable.
Renderables
Renderables are anything that needs to be drawn to the screen. They must implement the render method, which must return a ready-to-draw surface, and the get_rect method, which returns a pygame Rectangle, with the position and size of the renderable. Classes can inherit from both Entity and Renderable, to allow them to both be drawn and have behavior.
class MyRenderable(Renderable):
# __init__ here
def get_rect(self) -> pygame.Rect:
return pygame.Rect(100, 50, 10, 10)
def render(self, delta_time: float):
surface = Surface((10, 10))
surface.fill(Color("fuchsia"))
return surface
This will draw a small fuchsia square at a world position of 100, 50.
UI Elements
There's a special layer in the the render system, the UI layer. Renderables in the UI layer are always drawn in screen space, not world space.
Screen Space vs World Space
In base pygame, all surfaces are rendered in terms of screen space. This means that changing the resolution changes the size of the display, but not the objects on it. This means either a complicated setup that accounts for the window size before figuring out where to draw everything, or locking down the resolution, never to be changed.
Pyrite features a camera system as part of its default renderer. Cameras are moveable and zoomable, and automatically ignore any renderables they can't see, speeding up your game when items are offscreen.
You can even have multiple cameras, rendered to different parts of the screen!*
Renderables have layers and draw indexes to ensure that everything is drawn in the desired order. You can even add additional layers, and have cameras ignore layers, as needed.
Cameras are, or course, optional. Pyrite can treat your world space just like screen space if you don't want/need cameras for your project.
*Currently, multiple cameras slows the game greatly.
Roadmap
- (Eternal) Improve the renderer. Faster rendering means more renderables!
See the open issues for a full list of proposed features (and known issues).
License
Distributed under the MIT License. See LICENSE.txt for more information.
Contact
Better Built Fool - betterbuiltfool@gmail.com
Bluesky - @betterbuiltfool.bsky.social
Project Link: https://github.com/BetterBuiltFool/pyrite_framework
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyrite_framework-0.11.0.tar.gz.
File metadata
- Download URL: pyrite_framework-0.11.0.tar.gz
- Upload date:
- Size: 34.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7dcec3dd5b6da03666a6f7801730578884402ee915ddf105ccb8205ec036684d
|
|
| MD5 |
e6ff1a6db8484adfdc1aec5e2487128f
|
|
| BLAKE2b-256 |
9a869b5f01b4f1f77f86dad23ebeba3ba592dcb8331b2106063e0d7d13d0c495
|
File details
Details for the file pyrite_framework-0.11.0-py3-none-any.whl.
File metadata
- Download URL: pyrite_framework-0.11.0-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d62942f3fcf37fb4823d6f3675f82b3acdfb6b7140d64eb5dabb71830f3c8a4b
|
|
| MD5 |
78f868b9d3fe8b1be7afd9ad5ca465d5
|
|
| BLAKE2b-256 |
2c813c01dedc654b9ecece73277d8b7713c9b5868894ad86287e34afae9deab5
|