Nettoyage du mouvement et de la détection de collision
This commit is contained in:
parent
392fbd8e74
commit
9e1dc1c152
@ -20,31 +20,27 @@ class PhysicsServer(Server):
|
|||||||
self._static.remove(component)
|
self._static.remove(component)
|
||||||
self._dynamic.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):
|
def step(self):
|
||||||
for d in self._dynamic:
|
for d in self._dynamic:
|
||||||
for x in range(abs(d.vx)):
|
x_step = -1 if d.vx < 0 else 1
|
||||||
stop = False
|
y_step = -1 if d.vy < 0 else 1
|
||||||
for s in self._static:
|
for i in range(abs(d.vx)):
|
||||||
if s.rect.colliderect(d.rect):
|
if self._check_collide(d.rect):
|
||||||
stop = True
|
d.parent.x -= x_step
|
||||||
break
|
d.vx = 0
|
||||||
if stop:
|
|
||||||
d.parent.x += -1 if d.vx > 0 else 1
|
|
||||||
d.rect.x = d.parent.x
|
|
||||||
break
|
break
|
||||||
d.parent.x += 1 if d.vx > 0 else -1
|
d.parent.x += x_step
|
||||||
# d.rect.x = d.parent.x
|
for i in range(abs(d.vy)):
|
||||||
for y in range(abs(d.vy)):
|
if self._check_collide(d.rect):
|
||||||
stop = False
|
d.parent.y -= y_step
|
||||||
for s in self._static:
|
d.vy = 0
|
||||||
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
|
|
||||||
break
|
break
|
||||||
d.parent.y += 1 if d.vy > 0 else -1
|
d.parent.y += y_step
|
||||||
# d.rect.y = d.parent.y
|
# d.vx = 0
|
||||||
d.vx = 0
|
# d.vy = 0
|
||||||
d.vy = 0
|
|
||||||
|
@ -53,7 +53,7 @@ class Ghost(MovingEntity):
|
|||||||
|
|
||||||
class Lvl0(Scene):
|
class Lvl0(Scene):
|
||||||
def load(self):
|
def load(self):
|
||||||
T = 2
|
T = 1
|
||||||
v = 2
|
v = 2
|
||||||
|
|
||||||
pac_surf = Surface((S//T, S//T)).convert()
|
pac_surf = Surface((S//T, S//T)).convert()
|
||||||
@ -63,6 +63,9 @@ class Lvl0(Scene):
|
|||||||
def pacman_script(entity):
|
def pacman_script(entity):
|
||||||
p = pacman.phys
|
p = pacman.phys
|
||||||
inputs = key.get_pressed()
|
inputs = key.get_pressed()
|
||||||
|
if T > 1:
|
||||||
|
p.vx = 0
|
||||||
|
p.vy = 0
|
||||||
if inputs[K_UP]:
|
if inputs[K_UP]:
|
||||||
p.vy = -v
|
p.vy = -v
|
||||||
if inputs[K_DOWN]:
|
if inputs[K_DOWN]:
|
||||||
@ -72,8 +75,8 @@ class Lvl0(Scene):
|
|||||||
if inputs[K_RIGHT]:
|
if inputs[K_RIGHT]:
|
||||||
p.vx = v
|
p.vx = v
|
||||||
pacman.script = pacman_script
|
pacman.script = pacman_script
|
||||||
pacman.x = pacman.phys.rect.x = S*15
|
pacman.x = pacman.phys.rect.x = S*1
|
||||||
pacman.y = pacman.phys.rect.y = S*15
|
pacman.y = pacman.phys.rect.y = S*1
|
||||||
|
|
||||||
pacman.phys.rect.w = S//T
|
pacman.phys.rect.w = S//T
|
||||||
pacman.phys.rect.h = S//T
|
pacman.phys.rect.h = S//T
|
||||||
|
Reference in New Issue
Block a user