source: nagslang/engine.py@ 41:1d087f79ca29

Last change on this file since 41:1d087f79ca29 was 41:1d087f79ca29, checked in by Simon Cross <hodgestar@…>, 8 years ago

Use fixed timestemp because pymunk docs say this is an order of magnitude more efficient.

File size: 1.5 KB
Line 
1"""Top-level event dispatching and area transition."""
2
3import pygame
4
5from nagslang import constants
6from nagslang.screens.menu import MenuScreen
7from nagslang.screens.area import AreaScreen
8from nagslang.events import ScreenChange
9
10
11class Engine(object):
12 def __init__(self, surface):
13 self._surface = surface
14 self._clock = pygame.time.Clock()
15 self._fps = constants.FPS
16 self._dt = 1. / self._fps
17 self._world = None # TODO: create the world
18 self._current_screen = None
19 self._screens = {
20 'menu': MenuScreen,
21 'level1': AreaScreen,
22 }
23 self.change_screen('menu')
24
25 def change_screen(self, new_screen):
26 if self._current_screen is not None:
27 self._current_screen.teardown()
28 screen_cls = self._screens[new_screen]
29 self._current_screen = screen_cls(new_screen, self._world)
30 self._current_screen.setup()
31
32 def run(self):
33 running = True
34 while running:
35 for ev in pygame.event.get():
36 if ev.type == pygame.locals.QUIT:
37 running = False
38 elif ScreenChange.matches(ev):
39 self.change_screen(ev.screen)
40 else:
41 self._current_screen.handle_event(ev)
42 self._current_screen.tick(self._dt)
43 self._current_screen.render(self._surface)
44 pygame.display.flip()
45 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.