source: nagslang/engine.py@ 505:80c452f348dc

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

Rearrange progress saving, to happen on room entrance

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