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): 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) super().__init__(parent)
self.rect = rect self.rect = rect
def x_change_cb(x): def x_change_cb(x):
@ -14,6 +14,7 @@ class CollideRect(Component):
parent.subscribe('y', y_change_cb) parent.subscribe('y', y_change_cb)
self.static = static self.static = static
self.solid = solid self.solid = solid
self.cb = cb
self.vx = 0 self.vx = 0
self.vy = 0 self.vy = 0
@ -21,4 +22,4 @@ class CollideRect(Component):
PhysicsServer().register_component(self) PhysicsServer().register_component(self)
def unregister(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) GraphicsServer().register_component(self)
def unregister(self): def unregister(self):
GraphicsServer().register_component(self) GraphicsServer().unregister_component(self)

View File

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

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 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): class Level(Entity):
def __init__(self, path): def __init__(self, path):
super().__init__() super().__init__()
@ -26,16 +44,22 @@ class Level(Entity):
wall = Surface((S, S)).convert() wall = Surface((S, S)).convert()
wall.fill((0, 0, 255)) wall.fill((0, 0, 255))
for x, y in product(range(w), range(h)): 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)) CollideRect(self, Rect(x * S, y * S, S, S))
self.surf.blit(wall, (x * S, y * 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) Sprite(self, self.surf, 0)
class MovingEntity(Entity): class MovingEntity(Entity):
def __init__(self, surf): def __init__(self, surf):
super().__init__() super().__init__()
sprite = Sprite(self, surf, 1) sprite = Sprite(self, surf, 2)
self.phys = CollideRect( self.phys = CollideRect(
self, self,
Rect(self.x, self.y, sprite.width, sprite.width), 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.w = S//T
pacman.phys.rect.h = S//T pacman.phys.rect.h = S//T
self.entities.append(pacman)
self.entities.append(Level('lvl0.png')) self.entities.append(Level('lvl0.png'))
for entity in entities:
self.entities.append(entity)
self.entities.append(pacman)
super().load() super().load()