source: nagslang/engine.py@ 91:5c31b4d1851c

Last change on this file since 91:5c31b4d1851c was 85:c56eeaabd850, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Make the window resizable

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