Nettoyage du mouvement et de la détection de collision

This commit is contained in:
papush! 2019-12-07 12:49:24 +01:00
parent 392fbd8e74
commit 9e1dc1c152
2 changed files with 26 additions and 27 deletions

View File

@ -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

View File

@ -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