Pygame_ecs
An Pure Python, simple to use ECS( Entity Component System ) library for pygame.
To install it, do:
pip install pygame-ecs
then just import via: import pygame_ecs
How it works
Create an entity
entity = entity_manager.add_entity()
You can delete an entity like this:
entity_manager.kill_entity(entity)
Components are just classes that hold data
class Position(pygame_ecs.BaseComponent):
def __init__(self, x: int, y: int):
super().__init__()
self.x = x
self.y = y
Systems are just classes that hold logic
class BallPhysics(pygame_ecs.BaseSystem):
def __init__(self, screen) -> None:
super().__init__(required_component_types=[Position, Velocity])
self.dt = 0
self.screen = screen
def update_entity(self, entity, entity_components):
pos: Position = entity_components[Position]
vel: Velocity = entity_components[Velocity]
pos.x += vel.vec.x * self.dt
pos.y += vel.vec.y * self.dt
if pos.x > WIDTH or pos.x < 0:
vel.vec.x *= -1
if pos.y > HEIGHT or pos.y < 0:
vel.vec.y *= -1
Example Usage
import pygame
import pygame_ecs
import random
class Position(pygame_ecs.BaseComponent):
def __init__(self, x: int, y: int):
super().__init__()
self.x = x
self.y = y
class BallRenderer(pygame_ecs.BaseComponent):
def __init__(self, radius: int, color) -> None:
super().__init__()
self.radius = radius
self.color = color
class BallDrawSystem(pygame_ecs.BaseSystem):
def __init__(self, screen) -> None:
super().__init__(required_component_types=[Position, BallRenderer])
self.screen = screen
def update_entity(self, entity, entity_components):
pos: Position = entity_components[Position]
ball_renderer: BallRenderer = entity_components[BallRenderer]
pygame.draw.circle(
self.screen, ball_renderer.color, (pos.x, pos.y), ball_renderer.radius
)
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
component_manager = pygame_ecs.ComponentManager()
entity_manager = pygame_ecs.EntityManager(component_manager)
system_manager = pygame_ecs.SystemManager(entity_manager, component_manager)
ball_draw_system = BallDrawSystem(screen)
system_manager.add_system(ball_draw_system)
component_manager.init_components()
for _ in range(200):
center = (
random.randint(0, screen.get_width()),
random.randint(0, screen.get_height()),
)
radius = random.randint(4, 18)
color = [random.randint(0, 255) for _ in range(3)]
vel = pygame.math.Vector2(
(random.random() - 0.5) * 400 / 1000,
(random.random() - 0.5) * 400 / 1000,
)
entity = entity_manager.add_entity()
component_manager.add_component(entity, Position(center[0], center[1]))
component_manager.add_component(entity, BallRenderer(radius, color))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
system_manager.update_entities()
pygame.display.update()
clock.tick(60)
pygame.display.set_caption(f"FPS: {clock.get_fps()}")
Credits
I'd like to give credit to https://www.youtube.com/watch?v=71RSWVyOMEY and https://github.com/seanfisk/ecs
As well as dickerdackel from pgc server and SamieZaurus#8030 from UnitOfTime's server.
Metadata
Release files for pygame-ecs 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygame_ecs-0.4.0.tar.gz | 37.1 kB | Without provenance | Details for pygame_ecs-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygame_ecs-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for pygame_ecs-0.4.0-py3-none-any.whl |
Total release size: 45.3 kB
Release files / pygame_ecs-0.4.0.tar.gz
| Download URL | pygame_ecs-0.4.0.tar.gz |
|---|---|
| Size | 37.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05b6e3a32b74bc2136d980f0ebe2bc9477bcf578f141f5340f75622d3e02288a
|
|
BLAKE2b-256 checksum How to use checksums |
02f3147c52a26ba89f16b951171da7a9afb037fcf524fe098c7adb581bdb1738
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Release files / pygame_ecs-0.4.0-py3-none-any.whl
| Download URL | pygame_ecs-0.4.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fe8e18325880df79a0aeb458fdcfcbaf2c4b923f3bf9c13a8ec9ae3bf46eaebc
|
|
BLAKE2b-256 checksum How to use checksums |
04309f050592bea0cdf06a96be056f5aa4067f1207575c0be01d4ff9657a0542
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Security for pygame-ecs 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.