"""
import pymunk
+import pymunk.pygame_util
+import pygame.draw
class BaseLight(object):
self.colour = colour
self.position = position
- def add_to_space(self, space):
- space.remove(self.body, **self.body.shapes())
+ def add(self, space):
+ if self.body.space is not None:
+ space.remove(self.body, *self.body.shapes)
shapes = self.determine_ray_polys(space)
- space.add(self.body, **shapes)
+ space.add(self.body, *shapes)
def determine_ray_polys(self, space):
raise NotImplementedError(
self.direction = direction
self.spread = spread
+ def determine_ray_polys(self, space):
+ x, y = self.position
+ return [pymunk.Poly(self.body, [
+ self.position, [x + 50, y], [x, y + 50]])]
+
+ def render(self, surface):
+ for shape in self.body.shapes:
+ pygame_poly = [
+ pymunk.pygame_util.to_pygame(v, surface) for v in
+ shape.get_vertices()]
+ pygame.draw.polygon(surface, (255, 255, 255), pygame_poly)
+
class Lamp(BaseLight):
def __init__(self, colour="white", position=None, radius=100.0):
+""" Obstacles for light and space mould. """
+
import pymunk
import pymunk.pygame_util
import pygame.draw
-class Wall(object):
+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)
+ 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)
+
+
+class Wall(BaseObstacle):
- def __init__(self, vertices, space):
- body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
- self._shape = pymunk.Poly(body, vertices)
- space.add(self._shape)
+ def __init__(self, vertices):
+ super(Wall, self).__init__()
+ self.shapes.append(pymunk.Poly(self.body, vertices))
def render(self, surface):
- pygame_poly = [pymunk.pygame_util.to_pygame(v, surface) for v in
- self._shape.get_vertices()]
- pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)
+ for shape in self.shapes:
+ pygame_poly = [
+ pymunk.pygame_util.to_pygame(v, surface) for v in
+ shape.get_vertices()]
+ pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)
import pymunk
from .base import BaseScene
-from ..obstacles import Wall
+from ..lights import BaseLight
+from ..obstacles import BaseObstacle
from ..events import SceneChangeEvent
class NightScene(BaseScene):
def enter(self, gamestate):
- import pprint
- pprint.pprint(gamestate.station)
-
self._space = pymunk.Space()
-
- self._obstacles = []
- self._lights = []
- for obs in gamestate.station['obstacles']:
- wall = Wall(obs['vertices'], self._space)
- self._obstacles.append(wall)
+ self._obstacles = [
+ BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
+ self._lights = [
+ BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
+ for obs in self._obstacles:
+ obs.add(self._space)
+ for light in self._lights:
+ light.add(self._space)
def render(self, surface, gamestate):
surface.fill((0, 0, 255))
for obs in self._obstacles:
obs.render(surface)
+ for light in self._lights:
+ light.render(surface)
def event(self, ev, gamestate):
if ev.type == pgl.KEYDOWN: