source: nagslang/engine.py@ 437:d087dfb10896

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

Replace menu and death screen with the starting level

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.area import AreaScreen
8from nagslang.events import ScreenChange
9from nagslang.world import World
10from nagslang.level import Level
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
22 self._screens = {}
23 for area_name in AreaScreen.list_areas():
24 self._screens[area_name] = AreaScreen
25 starting_area = Level.game_starting_point()[0]
26
27 self._world.load()
28 self.change_screen(starting_area)
29 # Dummy resize event, to force us to realise our real size
30 # http://stackoverflow.com/q/16442573/8629
31 pygame.event.post(pygame.event.Event(pgl.VIDEORESIZE,
32 size=(0, 0), w=0, h=0))
33
34 def change_screen(self, new_screen):
35 if self._current_screen is not None:
36 self._current_screen.teardown()
37 screen_cls = self._screens[new_screen]
38 self._current_screen = screen_cls(new_screen, self._world)
39 self._current_screen.setup()
40
41 def run(self):
42 running = True
43 while running:
44 for ev in pygame.event.get():
45 if ev.type == pgl.QUIT:
46 running = False
47 elif ev.type == pgl.VIDEORESIZE:
48 pygame.display.set_mode(ev.size,
49 pgl.SWSURFACE | pgl.RESIZABLE)
50 self._surface = pygame.display.get_surface()
51 elif ScreenChange.matches(ev):
52 self._surface.fill(pygame.color.Color(0, 0, 0))
53 self.change_screen(ev.screen)
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.