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