From a7c1b606d3058559afbba65b1c05a5ccf2ac6b92 Mon Sep 17 00:00:00 2001 From: papush! Date: Mon, 9 Dec 2019 09:57:53 +0100 Subject: [PATCH] =?UTF-8?q?fant=C3=B4me=20basique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine/scene.py | 6 ++-- engine/scene_manager.py | 12 +++---- engine/servers/physics.py | 6 ++-- res/ghost.png | Bin 0 -> 652 bytes scenes/lvl0.py | 70 +++++++++++++++++++++++++++++++++----- 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 res/ghost.png diff --git a/engine/scene.py b/engine/scene.py index 7f2bbe2..ede4c0f 100644 --- a/engine/scene.py +++ b/engine/scene.py @@ -1,11 +1,11 @@ class Scene: def __init__(self): - self.entities = [] + self.entities = {} def unload(self): - for entity in self.entities: + for entity in self.entities.values(): entity.unregister() def load(self): - for entity in self.entities: + for entity in self.entities.values(): entity.register() diff --git a/engine/scene_manager.py b/engine/scene_manager.py index 9d6e339..60c19eb 100644 --- a/engine/scene_manager.py +++ b/engine/scene_manager.py @@ -4,15 +4,15 @@ from .singleton import Singleton class SceneManager(metaclass=Singleton): def __init__(self, scene): super().__init__() - self._scene = scene - self._scene.load() + self.scene = scene + self.scene.load() def change_scene(self, new_scene): - self._scene.unload() - self._scene = new_scene - self._scene.load() + self.scene.unload() + self.scene = new_scene + self.scene.load() def step(self): - for entity in self._scene.entities: + for entity in self.scene.entities.values(): if entity.script is not None: entity.script(entity) diff --git a/engine/servers/physics.py b/engine/servers/physics.py index 38a57e7..4f3b55e 100644 --- a/engine/servers/physics.py +++ b/engine/servers/physics.py @@ -30,6 +30,7 @@ class PhysicsServer(Server): x_step = -1 if d.vx < 0 else 1 y_step = -1 if d.vy < 0 else 1 for i in range(abs(d.vx)): + d.parent.x += x_step c = self._check_collide(d) if c is not None: if c.cb: @@ -38,8 +39,8 @@ class PhysicsServer(Server): d.parent.x -= x_step d.vx = 0 break - d.parent.x += x_step for i in range(abs(d.vy)): + d.parent.y += y_step c = self._check_collide(d) if c is not None: if c.cb: @@ -48,6 +49,3 @@ class PhysicsServer(Server): d.parent.y -= y_step d.vy = 0 break - d.parent.y += y_step - # d.vx = 0 - # d.vy = 0 diff --git a/res/ghost.png b/res/ghost.png new file mode 100644 index 0000000000000000000000000000000000000000..bc571509c9025d18b86c8b2e5178e0e7201eba8d GIT binary patch literal 652 zcmV;70(1R|P)EX>4Tx04R}tkv&MmKpe$iTcuSh9qgdukfAzR5EXIMDionYs1;guFuC*#nlvOS zE{=k0!NHHks)LKOt`4q(Aou~|;_9U6A|?JWDYS_3;J6>}?mh0_0scmXsb<$WplX(p zP9}tGZdC}rB8(n{5W%3tOnpuiQ}7&L_we!cF3z*O&;2?2l)T9RpFljzbi*RvAfDc| zbk6(4QC5-^;&b9LgDyz?$aUG}H_kjWYY7*QDULk!Ey()lA#h$5yuo&qkMnX zWrgz=XSGset$XqphV$CWGS_JiBZ);UL4*JqHIz|-g*dGmDJIgipYZSxIew8`GP%lN zc`uDQLn_Hp_EWT>mu4RCM> zj20<--Q(S%&ffk#)9UXB6diJbnpe_400009a7bBm001r{001r{0eGc9b^rhX2XskI zMF-;y2?!GqmbYO^0000PbVXQnLvL+uWo~o;Lvm$dbY)~9cWHEJAV*0}P*;Ht7XSbN zc1c7*R5;7sR6!1aAPj4XclZBS;tTLVL^9?KPA~32LZNhJ17e7RtPnzA1$~-DI1fq) z1gB(`IA@tXKb-cgY!hFGLaoQ*b^`**s47X)M{2$?&(93M&?MiGYa3iW36D%jL2l!p mFHG4ZWJHXAr+2QV&W0V+vMJeZrUM-S0000 0: + # node.left = graph[y][x-1] + # if x < w: + # node.right = graph[y][x+1] + # if y > 0: + # node.up = graph[y-1][x] + # if y < h: + # node.down = graph[y+1][y] Sprite(self, self.surf, 0) @@ -67,12 +111,19 @@ class MovingEntity(Entity): class Ghost(MovingEntity): - def update_ghost(): - pass - def __init__(self): super().__init__(image.load(res('ghost.png')).convert()) - self.script = update_ghost() + self.script = Ghost.script + + def script(self): + # cur_node = graph[self.y//S][self.x//S] + pacman = SceneManager().scene.entities['pacman'] + # pacman_node = graph[pacman.y//S][pacman.x//S] + x_dist = pacman.x - self.x + y_dist = pacman.y - self.y + s = 1 + self.phys.vx = s if x_dist > 0 else -s + self.phys.vy = s if y_dist > 0 else -s class Lvl0(Scene): @@ -105,10 +156,13 @@ class Lvl0(Scene): pacman.phys.rect.w = S//T pacman.phys.rect.h = S//T - self.entities.append(Level('lvl0.png')) - for entity in entities: - self.entities.append(entity) - self.entities.append(pacman) + self.entities['level'] = Level('lvl0.png') + for i, entity in enumerate(entities): + self.entities[f'pacdot-{i}'] = entity + self.entities['pacman'] = pacman + self.entities['ghost'] = Ghost() + self.entities['ghost'].x = S*6 + self.entities['ghost'].y = S*2 super().load()