source: nagslang/engine.py@ 285:ef4f6375c0b5

Last change on this file since 285:ef4f6375c0b5 was 285:ef4f6375c0b5, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Basic death

File size: 2.2 KB
RevLine 
[15]1"""Top-level event dispatching and area transition."""
2
[18]3import pygame
[85]4import pygame.locals as pgl
[15]5
6from nagslang import constants
[18]7from nagslang.screens.menu import MenuScreen
[20]8from nagslang.screens.area import AreaScreen
[285]9from nagslang.screens.playerdied import PlayerDiedScreen
[20]10from nagslang.events import ScreenChange
[238]11from nagslang.world import World
[15]12
13
14class Engine(object):
[18]15 def __init__(self, surface):
16 self._surface = surface
17 self._clock = pygame.time.Clock()
[15]18 self._fps = constants.FPS
[41]19 self._dt = 1. / self._fps
[238]20 self._world = World()
[20]21 self._current_screen = None
22 self._screens = {
23 'menu': MenuScreen,
[285]24 'dead': PlayerDiedScreen,
[20]25 'level1': AreaScreen,
[175]26 'level2': AreaScreen,
[20]27 }
[252]28 self._world.load()
[276]29 self.change_screen('menu')
[142]30 # Dummy resize event, to force us to realise our real size
31 # http://stackoverflow.com/q/16442573/8629
32 pygame.event.post(pygame.event.Event(pgl.VIDEORESIZE,
33 size=(0, 0), w=0, h=0))
[20]34
[276]35 def change_screen(self, new_screen):
[20]36 if self._current_screen is not None:
37 self._current_screen.teardown()
[37]38 screen_cls = self._screens[new_screen]
[276]39 self._current_screen = screen_cls(new_screen, self._world)
[37]40 self._current_screen.setup()
[15]41
42 def run(self):
43 running = True
44 while running:
[18]45 for ev in pygame.event.get():
[85]46 if ev.type == pgl.QUIT:
[15]47 running = False
[85]48 elif ev.type == pgl.VIDEORESIZE:
49 pygame.display.set_mode(ev.size,
[128]50 pgl.SWSURFACE | pgl.RESIZABLE)
[85]51 self._surface = pygame.display.get_surface()
[20]52 elif ScreenChange.matches(ev):
[252]53 self._world.save()
[128]54 self._surface.fill(pygame.color.Color(0, 0, 0))
[276]55 self.change_screen(ev.screen)
[18]56 else:
[20]57 self._current_screen.handle_event(ev)
[41]58 self._current_screen.tick(self._dt)
[20]59 self._current_screen.render(self._surface)
[18]60 pygame.display.flip()
[41]61 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.