Enter Boyd, from not exactly stage left.
[tabakrolletjie.git] / tabakrolletjie / scenes / night.py
1 """ In the night, the mould attacks. """
2
3 import pygame.locals as pgl
4
5 import pymunk
6 import time
7
8 from .base import BaseScene
9 from ..lights import BaseLight
10 from ..obstacles import BaseObstacle
11 from ..enemies import Boyd
12 from ..events import SceneChangeEvent
13
14 from ..constants import DEBUG
15
16
17 class NightScene(BaseScene):
18     def enter(self, gamestate):
19         self._space = pymunk.Space()
20         self._obstacles = [
21             BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
22         self._lights = [
23             BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
24         for obs in self._obstacles:
25             obs.add(self._space)
26         for light in self._lights:
27             light.add(self._space)
28
29         self._mould = Boyd(gamestate, self._space)
30
31     def render(self, surface, gamestate):
32         start_time = time.time()
33         surface.fill((0, 0, 155))
34         for light in self._lights:
35             light.render_light(surface)
36         for obs in self._obstacles:
37             obs.render(surface)
38         for light in self._lights:
39             light.render(surface)
40         self._mould.render(surface)
41
42         end_time = time.time()
43         if DEBUG:
44             print "Night Render", end_time - start_time
45
46     def event(self, ev, gamestate):
47         if ev.type == pgl.KEYDOWN:
48             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
49                 from .menu import MenuScene
50                 SceneChangeEvent.post(scene=MenuScene())
51
52     def tick(self, gamestate):
53         start_time = time.time()
54         self._mould.tick(gamestate, self._space)
55         end_time = time.time()
56         if DEBUG:
57             print "Night Tick", end_time - start_time