Skip to content

Commit

Permalink
(repo cleaning)sort files to exhibit 2 examples clearly
Browse files Browse the repository at this point in the history
  • Loading branch information
wkta committed May 9, 2023
1 parent c13159d commit 701f667
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 245 deletions.
32 changes: 32 additions & 0 deletions examples/essai_naif/en_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Iterable

from pyved import component, entity


@component
class Position:
x: int
y: int


@component
class Speed:
vx: float
vy: float


@component
class Health:
max_hp: int
hp: int


@component
class Perks:
li_perks: Iterable[str]


# --- def entités ---
@entity
class Player(Health, Perks, Position, Speed):
pass
49 changes: 49 additions & 0 deletions examples/essai_naif/en_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pygame

import pyved as pyv
from en_entities import Player
from en_systems import SysInput, SysEntityMover, SysGraphicalRepr


# ---
def run_game():
tester = Player(
x=0, y=256, max_hp=125, hp=None, li_perks=['toughGuy', 'kamikaze'],
vx=0.0, vy=0.0
)
print(tester)

pygame.init() # init all imported pygame modules
pygame.display.set_caption('Pong')
screen = pygame.display.set_mode((800, 500)) # w h
clock = pygame.time.Clock()

entities_mger = pyv.EntityManager()
entities_mger.add(
tester
)

system_manager = pyv.SystemManager([
SysInput(entities_mger),
SysEntityMover(entities_mger),
SysGraphicalRepr(entities_mger, screen)
])

system_manager.init_all()

# game_state_info: GameStateInfo = next(entities.get_by_class(GameStateInfo))
# while game_state_info.play:
# clock.tick_busy_loop(60) # tick_busy_loop точный + ест проц, tick грубый + не ест проц
# system_manager.update_systems()
# pygame.display.flip() # draw changes on screen

while not system_manager['SysInput'].gameover:
system_manager.proc_all()
pygame.display.flip()
clock.tick_busy_loop(60)

system_manager.cleanup_all()
print('All Systems stopped')


run_game()
80 changes: 1 addition & 79 deletions src/essai_naif_pyved.py → examples/essai_naif/en_systems.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
from typing import Iterable

import pygame

import pyved as pyv


@pyv.component
class Position:
x: int
y: int


@pyv.component
class Speed:
vx: float
vy: float


@pyv.component
class Health:
max_hp: int
hp: int


@pyv.component
class Perks:
li_perks: Iterable[str]


# --- def entités ---
@pyv.entity
class Player(Health, Perks, Position, Speed):
pass


# programme principal

t = Player(
x=0, y=256, max_hp=125, hp=None, li_perks=['toughGuy', 'kamikaze'],
vx=0.0, vy=0.0
)

print(t)
from en_entities import Player


# SysInit(entities),
Expand Down Expand Up @@ -102,41 +62,3 @@ def proc(self):
pygame.draw.circle(self.screen, 'red', (pl_obj.x, pl_obj.y), 8)
# for visible_entity in self.entities.get_by_class(Table, Score, Ball, Racket, Spark):
# self.screen.blit(visible_entity.sprite.image, (visible_entity.x, visible_entity.y))


# ---
def run_game():
pygame.init() # init all imported pygame modules
pygame.display.set_caption('Pong')
screen = pygame.display.set_mode((800, 500)) # w h
clock = pygame.time.Clock()

entities_mger = pyv.EntityManager()
entities_mger.add(
t
)

system_manager = pyv.SystemManager([
SysInput(entities_mger),
SysEntityMover(entities_mger),
SysGraphicalRepr(entities_mger, screen)
])

system_manager.init_all()

# game_state_info: GameStateInfo = next(entities.get_by_class(GameStateInfo))
# while game_state_info.play:
# clock.tick_busy_loop(60) # tick_busy_loop точный + ест проц, tick грубый + не ест проц
# system_manager.update_systems()
# pygame.display.flip() # draw changes on screen

while not system_manager['SysInput'].gameover:
system_manager.proc_all()
pygame.display.flip()
clock.tick_busy_loop(60)

system_manager.cleanup_all()
print('All Systems stopped')


run_game()
2 changes: 1 addition & 1 deletion src/example1/components.py → examples/pong/components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pygame.sprite import Sprite
from ecs_pattern import component

from consts import Team
from pyved import component


@component
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions src/example1/entities.py → examples/pong/entities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from ecs_pattern import entity

from components import ComMotion, ComScore, ComTeam, ComVisible, ComWait
from pyved import entity


@entity
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions src/example1/main.py → examples/pong/run_pong.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os

import pygame
from ecs_pattern import EntityManager, SystemManager

from consts import FPS_MAX
from entities import GameStateInfo
from pyved import EntityManager, SystemManager
from systems import SysControl, SysDraw, SysGoal, SysMovement, SysInit, SysRoundStarter


os.environ['SDL_VIDEO_CENTERED'] = '1' # window at center


Expand All @@ -29,15 +30,15 @@ def pong():
SysDraw(entities, screen),
])

system_manager.start_systems()
system_manager.init_all()

game_state_info: GameStateInfo = next(entities.get_by_class(GameStateInfo))
while game_state_info.play:
clock.tick_busy_loop(FPS_MAX) # tick_busy_loop точный + ест проц, tick грубый + не ест проц
system_manager.update_systems()
system_manager.proc_all()
pygame.display.flip() # draw changes on screen

system_manager.stop_systems()
system_manager.cleanup_all()


if __name__ == '__main__':
Expand Down
File renamed without changes.
28 changes: 14 additions & 14 deletions src/example1/systems.py → examples/pong/systems.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from typing import Callable
from random import uniform, choice
from typing import Callable

import pygame
from pygame import Surface
from pygame.rect import Rect
from pygame.event import Event
from pygame.display import Info as VideoInfo
from pygame.event import Event
from pygame.locals import QUIT, KEYDOWN, KEYUP, K_ESCAPE, K_UP, K_DOWN, K_w, K_s, K_SPACE
from ecs_pattern import System, EntityManager
from pygame.rect import Rect

from components import ComMotion, ComVisible
from consts import Team, BALL_SIZE, RACKET_WIDTH, RACKET_HEIGHT, RACKET_SPEED, BALL_SPEED_MIN
from entities import Ball, GameStateInfo, Racket, Score, Table, TeamScoredGoalEvent, WaitForBallMoveEvent, Spark
from pyved import System, EntityManager
from sprites import ball_sprite, racket_sprite, table_sprite, score_sprite, spark_sprite
from consts import Team, BALL_SIZE, RACKET_WIDTH, RACKET_HEIGHT, RACKET_SPEED, BALL_SPEED_MIN


def set_random_ball_speed(ball: Ball, screen_info: VideoInfo, x_direction: int):
Expand All @@ -26,7 +26,7 @@ class SysInit(System):
def __init__(self, entities: EntityManager):
self.entities = entities

def start(self):
def initialize(self):
screen_info = pygame.display.Info()
self.entities.init(
TeamScoredGoalEvent(Team.LEFT),
Expand Down Expand Up @@ -80,7 +80,7 @@ def start(self):
)
print('Ping')

def stop(self):
def cleanup(self):
print('Pong')


Expand All @@ -89,10 +89,10 @@ def __init__(self, entities: EntityManager):
self.entities = entities
self.game_state_info = None

def start(self):
def initialize(self):
self.game_state_info = next(self.entities.get_by_class(GameStateInfo))

def update(self):
def proc(self):
if self.game_state_info.pause:
return
# get entities
Expand Down Expand Up @@ -158,7 +158,7 @@ class SysGoal(System):
def __init__(self, entities: EntityManager):
self.entities = entities

def update(self):
def proc(self):
team_scored_goal_event: TeamScoredGoalEvent = next(self.entities.get_by_class(TeamScoredGoalEvent), None)
if team_scored_goal_event:
score_entity: Score
Expand All @@ -174,7 +174,7 @@ def __init__(self, entities: EntityManager, clock: pygame.time.Clock):
self.entities = entities
self.clock = clock

def update(self):
def proc(self):
wait_for_ball_move_event: WaitForBallMoveEvent = next(self.entities.get_by_class(WaitForBallMoveEvent), None)
if wait_for_ball_move_event:
wait_for_ball_move_event.wait_ms -= self.clock.get_time()
Expand All @@ -192,10 +192,10 @@ def __init__(self, entities: EntityManager, event_getter: Callable[..., list[Eve
self.game_state_info = None
self.pressed_keys = set()

def start(self):
def initialize(self):
self.game_state_info = next(self.entities.get_by_class(GameStateInfo))

def update(self):
def proc(self):
for event in self.event_getter(self.event_types):
event_type = event.type
event_key = getattr(event, 'key', None)
Expand Down Expand Up @@ -250,6 +250,6 @@ def __init__(self, entities: EntityManager, screen: Surface):
self.entities = entities
self.screen = screen

def update(self):
def proc(self):
for visible_entity in self.entities.get_by_class(Table, Score, Ball, Racket, Spark):
self.screen.blit(visible_entity.sprite.image, (visible_entity.x, visible_entity.y))
7 changes: 0 additions & 7 deletions src/example1/ecs_pattern/__init__.py

This file was deleted.

0 comments on commit 701f667

Please sign in to comment.