# 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
```python
entity = entity_manager.add_entity()
```
You can delete an entity like this:
```python
entity_manager.kill_entity(entity)
```
Components are just classes that hold data
```python
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
```python
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
```py
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "pygame-ecs",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "ECS, component, ecs, entity, entity component system, pygame, pygame-ce, pygame_ecs, system",
"author": null,
"author_email": "Notenlish <71970100+Notenlish@users.noreply.github.com>",
"download_url": "https://files.pythonhosted.org/packages/02/f3/147c52a26ba89f16b951171da7a9afb037fcf524fe098c7adb581bdb1738/pygame_ecs-0.4.0.tar.gz",
"platform": null,
"description": "# Pygame_ecs\n\nAn Pure Python, simple to use ECS( Entity Component System ) library for pygame.\n\nTo install it, do:\n`pip install pygame-ecs`\nthen just import via: `import pygame_ecs`\n\n## How it works\n\nCreate an entity\n\n```python\nentity = entity_manager.add_entity()\n```\n\nYou can delete an entity like this:\n```python\nentity_manager.kill_entity(entity)\n```\n\nComponents are just classes that hold data\n\n```python\nclass Position(pygame_ecs.BaseComponent):\n def __init__(self, x: int, y: int):\n super().__init__()\n self.x = x\n self.y = y\n```\n\nSystems are just classes that hold logic\n\n```python\n\nclass BallPhysics(pygame_ecs.BaseSystem):\n def __init__(self, screen) -> None:\n super().__init__(required_component_types=[Position, Velocity])\n self.dt = 0\n self.screen = screen\n\n def update_entity(self, entity, entity_components):\n pos: Position = entity_components[Position]\n vel: Velocity = entity_components[Velocity]\n pos.x += vel.vec.x * self.dt\n pos.y += vel.vec.y * self.dt\n if pos.x > WIDTH or pos.x < 0:\n vel.vec.x *= -1\n if pos.y > HEIGHT or pos.y < 0:\n vel.vec.y *= -1\n\n```\n\n## Example Usage\n```py\nimport pygame\nimport pygame_ecs\nimport random\n\n\nclass Position(pygame_ecs.BaseComponent):\n def __init__(self, x: int, y: int):\n super().__init__()\n self.x = x\n self.y = y\n\n\nclass BallRenderer(pygame_ecs.BaseComponent):\n def __init__(self, radius: int, color) -> None:\n super().__init__()\n self.radius = radius\n self.color = color\n\n\nclass BallDrawSystem(pygame_ecs.BaseSystem):\n def __init__(self, screen) -> None:\n super().__init__(required_component_types=[Position, BallRenderer])\n self.screen = screen\n\n def update_entity(self, entity, entity_components):\n pos: Position = entity_components[Position]\n ball_renderer: BallRenderer = entity_components[BallRenderer]\n pygame.draw.circle(\n self.screen, ball_renderer.color, (pos.x, pos.y), ball_renderer.radius\n )\n\n\nscreen = pygame.display.set_mode((800, 600))\nclock = pygame.time.Clock()\n\ncomponent_manager = pygame_ecs.ComponentManager()\nentity_manager = pygame_ecs.EntityManager(component_manager)\nsystem_manager = pygame_ecs.SystemManager(entity_manager, component_manager)\nball_draw_system = BallDrawSystem(screen)\nsystem_manager.add_system(ball_draw_system)\ncomponent_manager.init_components()\n\nfor _ in range(200):\n center = (\n random.randint(0, screen.get_width()),\n random.randint(0, screen.get_height()),\n )\n radius = random.randint(4, 18)\n color = [random.randint(0, 255) for _ in range(3)]\n vel = pygame.math.Vector2(\n (random.random() - 0.5) * 400 / 1000,\n (random.random() - 0.5) * 400 / 1000,\n )\n entity = entity_manager.add_entity()\n component_manager.add_component(entity, Position(center[0], center[1]))\n component_manager.add_component(entity, BallRenderer(radius, color))\n\nwhile True:\n for event in pygame.event.get():\n if event.type == pygame.QUIT:\n raise SystemExit\n\n system_manager.update_entities()\n pygame.display.update()\n clock.tick(60)\n pygame.display.set_caption(f\"FPS: {clock.get_fps()}\")\n```\n\n## Credits\n\nI'd like to give credit to https://www.youtube.com/watch?v=71RSWVyOMEY and https://github.com/seanfisk/ecs\nAs well as `dickerdackel` from pgc server and `SamieZaurus#8030` from UnitOfTime's server.\n",
"bugtrack_url": null,
"license": null,
"summary": "Pure Python, simple to use Entity Component System(ECS) for pygame",
"version": "0.4.0",
"project_urls": {
"Bug Tracker": "https://github.com/Notenlish/pygame_ecs/issues",
"Homepage": "https://github.com/Notenlish/pygame_ecs"
},
"split_keywords": [
"ecs",
" component",
" ecs",
" entity",
" entity component system",
" pygame",
" pygame-ce",
" pygame_ecs",
" system"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "04309f050592bea0cdf06a96be056f5aa4067f1207575c0be01d4ff9657a0542",
"md5": "2e11eb862742f29403462f5605e3d936",
"sha256": "fe8e18325880df79a0aeb458fdcfcbaf2c4b923f3bf9c13a8ec9ae3bf46eaebc"
},
"downloads": -1,
"filename": "pygame_ecs-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e11eb862742f29403462f5605e3d936",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8199,
"upload_time": "2024-09-07T20:37:55",
"upload_time_iso_8601": "2024-09-07T20:37:55.588806Z",
"url": "https://files.pythonhosted.org/packages/04/30/9f050592bea0cdf06a96be056f5aa4067f1207575c0be01d4ff9657a0542/pygame_ecs-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "02f3147c52a26ba89f16b951171da7a9afb037fcf524fe098c7adb581bdb1738",
"md5": "ab5c18248c864ac37e1b8028d069fab2",
"sha256": "05b6e3a32b74bc2136d980f0ebe2bc9477bcf578f141f5340f75622d3e02288a"
},
"downloads": -1,
"filename": "pygame_ecs-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "ab5c18248c864ac37e1b8028d069fab2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 37115,
"upload_time": "2024-09-07T20:37:57",
"upload_time_iso_8601": "2024-09-07T20:37:57.614343Z",
"url": "https://files.pythonhosted.org/packages/02/f3/147c52a26ba89f16b951171da7a9afb037fcf524fe098c7adb581bdb1738/pygame_ecs-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-07 20:37:57",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Notenlish",
"github_project": "pygame_ecs",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pygame-ecs"
}