Add exmine mode
[naja.git] / naja / engine.py
1 import pygame
2 import pygame.locals as pgl
3
4 from naja.constants import FPS
5 from naja.events import SceneChangeEvent, QuitGameEvent
6
7
8 class Engine(object):
9     def __init__(self, surface, scene, state):
10         self._surface = surface
11         self._scene = scene
12         self._state = state
13
14     def run(self):
15         clock = pygame.time.Clock()
16         while True:
17             self._surface.fill((0, 0, 0))
18             for ev in pygame.event.get():
19                 if ev.type == pgl.QUIT or QuitGameEvent.matches(ev):
20                     self.quit_game()
21                     return
22                 elif SceneChangeEvent.matches(ev):
23                     self.change_scene(ev.scene_cls)
24                 else:
25                     self._scene.handle_event(ev)
26             self._scene.render(self._surface)
27             pygame.display.flip()
28             clock.tick(FPS)
29
30     def change_scene(self, scene_cls):
31         self._scene.exit()
32         self._scene = scene_cls(self._state)
33         self._scene.enter()
34
35     def quit_game(self):
36         self._scene.exit()
37         self._scene = None