source: nagslang/engine.py@ 251:611370331bd1

Last change on this file since 251:611370331bd1 was 238:28d906fc2ab1, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Add a world object to collect some stats

File size: 2.1 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
10from nagslang.world import World
11
12
13class Engine(object):
14 def __init__(self, surface):
15 self._surface = surface
16 self._clock = pygame.time.Clock()
17 self._fps = constants.FPS
18 self._dt = 1. / self._fps
19 self._world = World()
20 self._current_screen = None
21 self._screens = {
22 'menu': MenuScreen,
23 'level1': AreaScreen,
24 'level2': AreaScreen,
25 }
26 self.change_screen('menu', None)
27 # Dummy resize event, to force us to realise our real size
28 # http://stackoverflow.com/q/16442573/8629
29 pygame.event.post(pygame.event.Event(pgl.VIDEORESIZE,
30 size=(0, 0), w=0, h=0))
31
32 def change_screen(self, new_screen, player):
33 if self._current_screen is not None:
34 self._current_screen.teardown()
35 screen_cls = self._screens[new_screen]
36 self._current_screen = screen_cls(new_screen, player, self._world)
37 self._current_screen.setup()
38
39 def run(self):
40 running = True
41 while running:
42 for ev in pygame.event.get():
43 if ev.type == pgl.QUIT:
44 running = False
45 elif ev.type == pgl.VIDEORESIZE:
46 pygame.display.set_mode(ev.size,
47 pgl.SWSURFACE | pgl.RESIZABLE)
48 self._surface = pygame.display.get_surface()
49 elif ScreenChange.matches(ev):
50 self._surface.fill(pygame.color.Color(0, 0, 0))
51 self.change_screen(ev.screen, ev.player)
52 else:
53 self._current_screen.handle_event(ev)
54 self._current_screen.tick(self._dt)
55 self._current_screen.render(self._surface)
56 pygame.display.flip()
57 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.