source: nagslang/engine.py@ 252:73d6abf5ba22

Last change on this file since 252:73d6abf5ba22 was 252:73d6abf5ba22, checked in by Stefano Rivera <stefano@…>, 8 years ago

Save game state

File size: 2.2 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._world.load()
27 self.change_screen('menu', None)
28 # Dummy resize event, to force us to realise our real size
29 # http://stackoverflow.com/q/16442573/8629
30 pygame.event.post(pygame.event.Event(pgl.VIDEORESIZE,
31 size=(0, 0), w=0, h=0))
32
33 def change_screen(self, new_screen, player):
34 if self._current_screen is not None:
35 self._current_screen.teardown()
36 screen_cls = self._screens[new_screen]
37 self._current_screen = screen_cls(new_screen, player, self._world)
38 self._current_screen.setup()
39
40 def run(self):
41 running = True
42 while running:
43 for ev in pygame.event.get():
44 if ev.type == pgl.QUIT:
45 running = False
46 elif ev.type == pgl.VIDEORESIZE:
47 pygame.display.set_mode(ev.size,
48 pgl.SWSURFACE | pgl.RESIZABLE)
49 self._surface = pygame.display.get_surface()
50 elif ScreenChange.matches(ev):
51 self._world.save()
52 self._surface.fill(pygame.color.Color(0, 0, 0))
53 self.change_screen(ev.screen, ev.player)
54 else:
55 self._current_screen.handle_event(ev)
56 self._current_screen.tick(self._dt)
57 self._current_screen.render(self._surface)
58 pygame.display.flip()
59 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.