1 """ Obstacles for light and space mould. """
4 import pymunk.pygame_util
7 from .constants import OBSTACLE_CATEGORY
9 OBSTACLE_FILTER = pymunk.ShapeFilter(categories=OBSTACLE_CATEGORY)
12 class BaseObstacle(object):
14 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
18 if self.body.space is not None:
19 space.remove(self.body, *self.body.shapes)
20 for shape in self.shapes:
21 shape.filter = OBSTACLE_FILTER
22 space.add(self.body, *self.shapes)
24 def render(self, surface):
25 raise NotImplementedError("Obstacles should implement .render().")
28 def load(cls, config):
30 obstacle_type = kw.pop("type")
32 c for c in cls.__subclasses__()
33 if c.__name__.lower() == obstacle_type]
34 return obstacle_class(**kw)
37 class Wall(BaseObstacle):
39 def __init__(self, vertices):
40 super(Wall, self).__init__()
41 self.shapes.append(pymunk.Poly(self.body, vertices))
43 def render(self, surface):
44 for shape in self.shapes:
46 pymunk.pygame_util.to_pygame(v, surface) for v in
48 pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)