source: nagslang/engine.py @ 235:831e4f6b3d18

Last change on this file since 235:831e4f6b3d18 was 180:026297a03963, checked in by Neil Muller <drnlmuller@…>, 7 years ago

Add DoorEvent? and tweak ScreenChange? to keep more state when the player goes through a door

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