Skip to main content

ECS implementation for Python

Project description

pavouk-ecs

An Entity Component System (ECS) framework for Python.

Installation

pip install pavouk_ecs

Example

import pygame
import sys

from dataclasses import dataclass
from pavouk_ecs import Manager
from pavouk_ecs.system import System


RESOLUTION = (160, 160)
BACKGROUND_COLOR = (10, 10, 10)


###
### COMPONENTS
###


@dataclass
class Area:
    w: int = 0
    h: int = 0


@dataclass
class Color:
    r: int = 0
    g: int = 0
    b: int = 0


@dataclass
class Transform:
    x: int = 0
    y: int = 0


@dataclass
class Velocity:
    x: float = 0.0
    y: float = 0.0


###
### SYSTEMS
###


class RenderSystem(System):
    def on_update(self, surface, deltatime):
        for e in self.manager.query([Transform, Area, Color]):
            t = self.manager.get(Transform, e)
            a = self.manager.get(Area, e)
            c = self.manager.get(Color, e)

            r = pygame.Rect(t.x, t.y, a.w, a.h)
            pygame.draw.rect(surface, (c.r, c.g, c.b), r)


class BouncingSystem(System):
    def on_update(self, surface, deltatime):
        for e in self.manager.query([Transform, Area, Velocity]):
            t = self.manager.get(Transform, e)
            a = self.manager.get(Area, e)
            v = self.manager.get(Velocity, e)

            if t.x <= 0 and v.x < 0 or t.x + a.w >= RESOLUTION[0]:
                v.x = -v.x
            if t.y <= 0 and v.y < 0 or t.y + a.h >= RESOLUTION[1]:
                v.y = -v.y
            
            t.x += v.x
            t.y += v.y


###
### GAME
###

class Game:
    def __init__(self):
        self.running = True

        pygame.init()
        pygame.display.set_caption("Pavouk ECS Example")

        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode(RESOLUTION)

        self.manager = Manager()

        player = self.manager.create_entity()
        t = self.manager.assign(Transform, player)
        t.x = 16 * 4
        t.y = 16 * 4

        a = self.manager.assign(Area, player)
        a.w = 16
        a.h = 16

        c = self.manager.assign(Color, player)
        c.r = 200
        c.g = 200
        c.b = 200

        v = self.manager.assign(Velocity, player)
        v.x = -1.0
        v.y = 2.0

        self.manager.add_system(BouncingSystem)
        self.manager.add_system(RenderSystem)
    
    def run(self):
        while self.running:
            self.screen.fill(BACKGROUND_COLOR)

            events = pygame.event.get()
            self._handle_events(events)
            
            self.manager.update_systems(self.screen, 0)

            pygame.display.flip()

            self.clock.tick(60)

        pygame.quit()
        sys.exit()

    def _handle_events(self, events):
        for e in events:
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_ESCAPE:
                    self.running = False

            elif e.type == pygame.QUIT:
                self.running = False

    def quit(self):
        self.running = False


if __name__ == "__main__":
    game = Game()
    game.run()

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

pavouk_ecs-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pavouk_ecs-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file pavouk_ecs-0.1.0.tar.gz.

File metadata

  • Download URL: pavouk_ecs-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for pavouk_ecs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f1846599335db3c52bd4f032c7cf0057b491d385eab18baf9acd023d0fb1dee
MD5 2f2c4efdc81a991903cf9334bd0c360f
BLAKE2b-256 5384bab8c183757c51bd671b125a701ceef27b1d6271f440dc50ea84febb5e73

See more details on using hashes here.

File details

Details for the file pavouk_ecs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pavouk_ecs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for pavouk_ecs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf4d24af35dc1557159f756eaf7234bfccaecd9b7b0c826c6278af81e6baa0e4
MD5 6392db7db93b35a905aed81311254907
BLAKE2b-256 c471b5fff42f307c56fe125a510ae84264bf710987dffd75b4b4068c14fd3de1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page