This commit is contained in:
papush! 2019-12-08 23:47:32 +01:00
parent 9e1dc1c152
commit fbfedc539f
5 changed files with 61 additions and 27 deletions

View File

@ -3,7 +3,7 @@ from ..servers.physics import PhysicsServer
class CollideRect(Component):
def __init__(self, parent, rect, static=True, solid=True):
def __init__(self, parent, rect, static=True, solid=True, cb=None):
super().__init__(parent)
self.rect = rect
def x_change_cb(x):
@ -14,6 +14,7 @@ class CollideRect(Component):
parent.subscribe('y', y_change_cb)
self.static = static
self.solid = solid
self.cb = cb
self.vx = 0
self.vy = 0
@ -21,4 +22,4 @@ class CollideRect(Component):
PhysicsServer().register_component(self)
def unregister(self):
PhysicsServer().register_component(self)
PhysicsServer().unregister_component(self)

View File

@ -13,4 +13,4 @@ class Sprite(Component):
GraphicsServer().register_component(self)
def unregister(self):
GraphicsServer().register_component(self)
GraphicsServer().unregister_component(self)

View File

@ -8,36 +8,43 @@ class PhysicsServer(Server):
self._dynamic = []
def register_component(self, component):
if component.solid:
if component.static:
self._static.append(component)
else:
self._dynamic.append(component)
else:
self._area.append(component)
def unregister_component(self, component):
if component in self._static:
self._static.remove(component)
if component in self._dynamic:
self._dynamic.remove(component)
def _check_collide(self, rect):
for s in self._static:
if s.rect.colliderect(rect):
return True
return False
def _check_collide(self, dyn):
for s in self._static + self._dynamic:
if s != dyn and s.rect.colliderect(dyn.rect):
return s
return None
def step(self):
for d in self._dynamic:
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):
c = self._check_collide(d)
if c is not None:
if c.cb:
c.cb()
if c.solid:
d.parent.x -= x_step
d.vx = 0
break
d.parent.x += x_step
for i in range(abs(d.vy)):
if self._check_collide(d.rect):
c = self._check_collide(d)
if c is not None:
if c.cb:
c.cb()
if c.solid:
d.parent.y -= y_step
d.vy = 0
break

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -15,6 +15,24 @@ from itertools import product
S=20
entities=[]
class PacDot(Entity):
s = Surface((S, S))
draw.circle(s, (255, 255, 0), (S//2, S//2), S//4)
def __init__(self):
super().__init__()
CollideRect(self, Rect(0, 0, S, S),
static=True, solid=False, cb=self.cb)
Sprite(self, PacDot.s, 1)
def cb(self):
self.unregister()
class Level(Entity):
def __init__(self, path):
super().__init__()
@ -26,16 +44,22 @@ class Level(Entity):
wall = Surface((S, S)).convert()
wall.fill((0, 0, 255))
for x, y in product(range(w), range(h)):
if desc.get_at((x, y)) == (0, 0, 255):
col = desc.get_at((x, y))
if col == (0, 0, 255):
CollideRect(self, Rect(x * S, y * S, S, S))
self.surf.blit(wall, (x * S, y * S))
elif col == (139, 139, 139):
pc = PacDot()
pc.x = x * S
pc.y = y * S
entities.append(pc)
Sprite(self, self.surf, 0)
class MovingEntity(Entity):
def __init__(self, surf):
super().__init__()
sprite = Sprite(self, surf, 1)
sprite = Sprite(self, surf, 2)
self.phys = CollideRect(
self,
Rect(self.x, self.y, sprite.width, sprite.width),
@ -81,8 +105,10 @@ class Lvl0(Scene):
pacman.phys.rect.w = S//T
pacman.phys.rect.h = S//T
self.entities.append(pacman)
self.entities.append(Level('lvl0.png'))
for entity in entities:
self.entities.append(entity)
self.entities.append(pacman)
super().load()