pygame-ecs


Namepygame-ecs JSON
Version 0.2.9 PyPI version JSON
download
home_page
SummaryPure Python, simple to use Entity Component System(ECS) for pygame
upload_time2023-06-30 19:36:05
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords ecs component ecs entity entity component system pygame pygame-ce pygame_ecs system
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pygame_ecs

An Pure Python, simple to use ECS library for pygame.

## 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": "",
    "name": "pygame-ecs",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "ECS,component,ecs,entity,entity component system,pygame,pygame-ce,pygame_ecs,system",
    "author": "",
    "author_email": "Notenlish <71970100+Notenlish@users.noreply.github.com>",
    "download_url": "https://files.pythonhosted.org/packages/05/3a/e77e4ae610bf24e61f8cbbb6f826ff489ce44a971541019a167383231ea2/pygame_ecs-0.2.9.tar.gz",
    "platform": null,
    "description": "# Pygame_ecs\n\nAn Pure Python, simple to use ECS library for pygame.\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": "",
    "summary": "Pure Python, simple to use Entity Component System(ECS) for pygame",
    "version": "0.2.9",
    "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": "47bcdb3b50fcb28ee3c99592813fd31997befc8b69ad28980a44fa64fd22e29c",
                "md5": "aa582be6cfde946ef415b100a6fdcf20",
                "sha256": "cb711ccd2752fac1a172cee42130bb16b3c3a6fa68d54be27f541e8edacedc0d"
            },
            "downloads": -1,
            "filename": "pygame_ecs-0.2.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "aa582be6cfde946ef415b100a6fdcf20",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7546,
            "upload_time": "2023-06-30T19:36:03",
            "upload_time_iso_8601": "2023-06-30T19:36:03.332812Z",
            "url": "https://files.pythonhosted.org/packages/47/bc/db3b50fcb28ee3c99592813fd31997befc8b69ad28980a44fa64fd22e29c/pygame_ecs-0.2.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "053ae77e4ae610bf24e61f8cbbb6f826ff489ce44a971541019a167383231ea2",
                "md5": "4fe284bdf6045b20a9a78e9f836ccf0b",
                "sha256": "bc40f899357edc41fcd737ca266b68d5f1089e91553a9a7d22ababe3002ef94a"
            },
            "downloads": -1,
            "filename": "pygame_ecs-0.2.9.tar.gz",
            "has_sig": false,
            "md5_digest": "4fe284bdf6045b20a9a78e9f836ccf0b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 35681,
            "upload_time": "2023-06-30T19:36:05",
            "upload_time_iso_8601": "2023-06-30T19:36:05.155552Z",
            "url": "https://files.pythonhosted.org/packages/05/3a/e77e4ae610bf24e61f8cbbb6f826ff489ce44a971541019a167383231ea2/pygame_ecs-0.2.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-30 19:36:05",
    "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"
}
        
Elapsed time: 0.08551s