Entity component system for Python
Project description
Epal - A Entity component system (ECS) for pygame
Epal is a unity-like entity component system for python, based on pygame. The ECS is based on this article: https://en.wikipedia.org/wiki/Entity_component_system
We all know that pygame can get pretty verbose on bigger projects, so the aim of Epal is to make it less verbose, while at the same time implementing an Entity component system (ECS)
Demo
There is a demo attached to this project, the main.py file
As of epal version 0.0.2 the demo requires an epal asset pack, to generate the asset pack just run the generate_asset_pack.py file
Usage
Epal has a pretty simple api, however there is a lot of boilerplate code to get out of the way. Since Epal is scene based, and a scene requires an application, a window, ready for entities, looks like this:
from epal import *
# Initialize an epal window that fills with white
window = Window(1080, 720, clear_color = Color(255, 255, 255))
# Then we initialize a window
app = Application(window)
# Then a scene (It automatically gets added to the application)
main_scene = Scene()
# And then run the application (This starts the mainloop)
app.run()
Now, this looks like a lot of boilerplate code, and it is. However, once you have all this, adding an entity, is as easy as adding entity = Entity() if you do this however, you wont see anything on the screen, dont worry, the entity is there, there is just no renderer component added.
To do this, you add
from epal import *
...
# Create an entity
entity = Entity()
# Add a rectangle renderer component
entity.add_component(Rect)
Components
Component API
Since epal is an ECS (Entity component system) of course there are components. In epal components are the main way to interact with everything.
Before I can start explaining how to write components, I first have to explain what a component even is. In it's most basic form, a component is, in epal's case, a class that contains functions to manipulate the object it is attached to. This can go from as simple as storing positional data for the entity, to entire player controllers or advanced physics simulations.
An empty epal component would look like this:
from epal import Component
class MyComponent(Component):
# The constructor has to take in an argument, 'parent'
# This argument is passed to the parent class so component in this case
# The parent class constructor defines parent as a member variable to the component
def __init__(self, parent):
super().__init__(parent)
# awake is called when the entity starts it's life (every time the scene gets switched to)
def awake(self):
pass
# And update gets called every frame the parent entity is alive
def update(self):
pass
A component gets added to an entity via the member function .add_component() this takes in the class typename, not an instance of the class, so for our example component it would be entity.add_component(MyComponent)
If you have a component that relies on other components, there is a member function defined for every component self.require_component(). The parameter is the same as add_component(), but instead of blindly adding the component, which returns a warning if the component already is there, require_component() also checks if the component already is there.
If you want other constructor arguments for your component feel free to add them. To accsess them when you add an component, you can add as many keyword arguments as you want to add_component()
Asset manager
For assets, epal also got you covered, in epal there is an AssetManager class, this asset manager unifies all assets into one class, and makes it easy to get an asset from a name.
Usage
Of course most classes in epal also allow non-epal assets, so paths or similar, but some epal classes require epal.Asset's, so its recommended to use the epal asset manager, especially on larger projects
Initializing a asset manager is as simple as this:
import epal
...
# Initialize the asset manager
asset_manager = epal.AssetManager()
# Add a couple of assets to the manager
asset_manager.add_asset("Player", "./assets/player.png", epal.AssetType.Image)
asset_manager.add_asset("Menu theme", "./assets/main_menu_theme.mp3", epal.AssetType.Audio)
Currently Images and Audio are the only asset types supported, but that is due to change
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 epal-0.0.2.tar.gz.
File metadata
- Download URL: epal-0.0.2.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a57ad8f2c8a7e9a15549749d16fe8de6099884059b4056050dcb0be7c07e4d58
|
|
| MD5 |
50b1d2933fa94fb92f7c84dbae54ac19
|
|
| BLAKE2b-256 |
2993ad57d010f0314ef11c24d702f13d4a59dfe38d1003c9e2fe158e95091053
|
File details
Details for the file epal-0.0.2-py3-none-any.whl.
File metadata
- Download URL: epal-0.0.2-py3-none-any.whl
- Upload date:
- Size: 19.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60a4ba500ebbf6150e1e97f68ba7e9319fbf43db1bde30ffa3abaf21bf7fd73d
|
|
| MD5 |
faa63f2d6729e6f7944351f43308e66d
|
|
| BLAKE2b-256 |
79cab5b526188ba8c75c3daf308836df21b1c2640f889da830d72c41cbaf9a3d
|