source: nagslang/engine.py

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

Show splash image on startup

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 self._current_screen.splash()
41
42 def change_screen(self, new_screen):
43 if self._current_screen is not None:
44 self._current_screen.teardown()
45 screen_cls = self._screens[new_screen]
46 self._current_screen = screen_cls(new_screen, self._world)
47 self._current_screen.setup()
48
49 def run(self):
50 running = True
51 while running:
52 for ev in pygame.event.get():
53 if ev.type == pgl.QUIT:
54 running = False
55 elif ev.type == pgl.VIDEORESIZE:
56 pygame.display.set_mode(ev.size,
57 pgl.SWSURFACE | pgl.RESIZABLE)
58 self._surface = pygame.display.get_surface()
59 elif ScreenChange.matches(ev):
60 self._surface.fill(pygame.color.Color(0, 0, 0))
61 self.change_screen(ev.screen)
62 else:
63 self._current_screen.handle_event(ev)
64 self._current_screen.tick(self._dt)
65 self._current_screen.render(self._surface)
66 pygame.display.flip()
67 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.