From 9e1dc1c1524739646ee2e5d0d40130f027ae4b63 Mon Sep 17 00:00:00 2001 From: papush! Date: Sat, 7 Dec 2019 12:49:24 +0100 Subject: [PATCH] =?UTF-8?q?Nettoyage=20du=20mouvement=20et=20de=20la=20d?= =?UTF-8?q?=C3=A9tection=20de=20collision?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine/servers/physics.py | 44 ++++++++++++++++++--------------------- scenes/lvl0.py | 9 +++++--- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/engine/servers/physics.py b/engine/servers/physics.py index ef78b34..4390f0e 100644 --- a/engine/servers/physics.py +++ b/engine/servers/physics.py @@ -20,31 +20,27 @@ class PhysicsServer(Server): self._static.remove(component) self._dynamic.remove(component) + def _check_collide(self, rect): + for s in self._static: + if s.rect.colliderect(rect): + return True + return False + def step(self): for d in self._dynamic: - for x in range(abs(d.vx)): - stop = False - for s in self._static: - if s.rect.colliderect(d.rect): - stop = True - break - if stop: - d.parent.x += -1 if d.vx > 0 else 1 - d.rect.x = d.parent.x + 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)): + if self._check_collide(d.rect): + d.parent.x -= x_step + d.vx = 0 break - d.parent.x += 1 if d.vx > 0 else -1 - # d.rect.x = d.parent.x - for y in range(abs(d.vy)): - stop = False - for s in self._static: - if s.rect.colliderect(d.rect): - stop = True - break - if stop: - d.parent.y += -1 if d.vy > 0 else 1 - d.rect.y = d.parent.y + d.parent.x += x_step + for i in range(abs(d.vy)): + if self._check_collide(d.rect): + d.parent.y -= y_step + d.vy = 0 break - d.parent.y += 1 if d.vy > 0 else -1 - # d.rect.y = d.parent.y - d.vx = 0 - d.vy = 0 + d.parent.y += y_step + # d.vx = 0 + # d.vy = 0 diff --git a/scenes/lvl0.py b/scenes/lvl0.py index 0565726..25b51e0 100644 --- a/scenes/lvl0.py +++ b/scenes/lvl0.py @@ -53,7 +53,7 @@ class Ghost(MovingEntity): class Lvl0(Scene): def load(self): - T = 2 + T = 1 v = 2 pac_surf = Surface((S//T, S//T)).convert() @@ -63,6 +63,9 @@ class Lvl0(Scene): def pacman_script(entity): p = pacman.phys inputs = key.get_pressed() + if T > 1: + p.vx = 0 + p.vy = 0 if inputs[K_UP]: p.vy = -v if inputs[K_DOWN]: @@ -72,8 +75,8 @@ class Lvl0(Scene): if inputs[K_RIGHT]: p.vx = v pacman.script = pacman_script - pacman.x = pacman.phys.rect.x = S*15 - pacman.y = pacman.phys.rect.y = S*15 + pacman.x = pacman.phys.rect.x = S*1 + pacman.y = pacman.phys.rect.y = S*1 pacman.phys.rect.w = S//T pacman.phys.rect.h = S//T