source: nagslang/engine.py@ 82:11b0017b5e4b

Last change on this file since 82:11b0017b5e4b 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
RevLine 
[15]1"""Top-level event dispatching and area transition."""
2
[18]3import pygame
[15]4
5from nagslang import constants
[18]6from nagslang.screens.menu import MenuScreen
[20]7from nagslang.screens.area import AreaScreen
8from nagslang.events import ScreenChange
[15]9
10
11class Engine(object):
[18]12 def __init__(self, surface):
13 self._surface = surface
14 self._clock = pygame.time.Clock()
[15]15 self._fps = constants.FPS
[41]16 self._dt = 1. / self._fps
[20]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()
[37]28 screen_cls = self._screens[new_screen]
29 self._current_screen = screen_cls(new_screen, self._world)
30 self._current_screen.setup()
[15]31
32 def run(self):
33 running = True
34 while running:
[18]35 for ev in pygame.event.get():
36 if ev.type == pygame.locals.QUIT:
[15]37 running = False
[20]38 elif ScreenChange.matches(ev):
39 self.change_screen(ev.screen)
[18]40 else:
[20]41 self._current_screen.handle_event(ev)
[41]42 self._current_screen.tick(self._dt)
[20]43 self._current_screen.render(self._surface)
[18]44 pygame.display.flip()
[41]45 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.