Add widgets and skeleton for game scene.
[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):
10         self._surface = surface
11         self._scene = scene
12
13     def run(self):
14         clock = pygame.time.Clock()
15         while True:
16             self._surface.fill((0, 0, 0))
17             for ev in pygame.event.get():
18                 if ev.type == pgl.QUIT or QuitGameEvent.matches(ev):
19                     self.quit_game()
20                     return
21                 elif SceneChangeEvent.matches(ev):
22                     self.change_scene(ev.scene)
23                 else:
24                     self._scene.handle_event(ev)
25             self._scene.render(self._surface)
26             pygame.display.flip()
27             clock.tick(FPS)
28
29     def change_scene(self, scene):
30         self._scene.exit()
31         self._scene = scene
32         self._scene.enter()
33
34     def quit_game(self):
35         self._scene.exit()
36         self._scene = None