card names should not be lists
[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, LoadGameEvent
6 from naja.sound import sound
7
8
9 class Engine(object):
10     def __init__(self, surface, scene, state):
11         self._surface = surface
12         self._scene = scene
13         self._state = state
14
15     def run(self):
16         clock = pygame.time.Clock()
17         while True:
18             self._surface.fill((0, 0, 0))
19             for ev in pygame.event.get():
20                 if ev.type == pgl.QUIT or QuitGameEvent.matches(ev):
21                     self.quit_game()
22                     return
23                 elif SceneChangeEvent.matches(ev):
24                     self.change_scene(ev.scene_cls)
25                 elif LoadGameEvent.matches(ev):
26                     self._state = ev.state
27                 else:
28                     self._scene.handle_event(ev)
29             self._scene.render(self._surface)
30             pygame.display.flip()
31             clock.tick(FPS)
32
33     def change_scene(self, scene_cls):
34         self._scene.exit()
35         self._scene = scene_cls(self._state)
36         self._scene.enter()
37
38     def quit_game(self):
39         self._scene.exit()
40         sound.play_sound('shutdown.ogg', foreground=True)
41         self._scene = None