source: nagslang/engine.py@ 37:4140780c21bc

Last change on this file since 37:4140780c21bc was 37:4140780c21bc, checked in by Simon Cross <hodgestar@…>, 8 years ago

Give screens a name and a world.

File size: 1.4 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._world = None # TODO: create the world
17 self._current_screen = None
18 self._screens = {
19 'menu': MenuScreen,
20 'level1': AreaScreen,
21 }
22 self.change_screen('menu')
23
24 def change_screen(self, new_screen):
25 if self._current_screen is not None:
26 self._current_screen.teardown()
27 screen_cls = self._screens[new_screen]
28 self._current_screen = screen_cls(new_screen, self._world)
29 self._current_screen.setup()
30
31 def run(self):
32 running = True
33 while running:
34 for ev in pygame.event.get():
35 if ev.type == pygame.locals.QUIT:
36 running = False
37 elif ScreenChange.matches(ev):
38 self.change_screen(ev.screen)
39 else:
40 self._current_screen.handle_event(ev)
41 self._current_screen.render(self._surface)
42 pygame.display.flip()
43 ms = self._clock.tick(self._fps)
44 self._current_screen.tick(ms / 1000.)
Note: See TracBrowser for help on using the repository browser.