+from .constants import OBSTACLE_CATEGORY
+
+OBSTACLE_FILTER = pymunk.ShapeFilter(categories=OBSTACLE_CATEGORY)
+
+
+class BaseObstacle(object):
+ def __init__(self):
+ self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
+ self.shapes = []
+
+ def add(self, space):
+ if self.body.space is not None:
+ space.remove(self.body, *self.body.shapes)
+ for shape in self.shapes:
+ shape.filter = OBSTACLE_FILTER
+ space.add(self.body, *self.shapes)
+
+ def render(self, surface):
+ raise NotImplementedError("Obstacles should implement .render().")
+
+ @classmethod
+ def load(cls, config):
+ kw = config.copy()
+ obstacle_type = kw.pop("type")
+ [obstacle_class] = [
+ c for c in cls.__subclasses__()
+ if c.__name__.lower() == obstacle_type]
+ return obstacle_class(**kw)
+