From 428f0c89073e5518f563a86c8bd42f6073af0007 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 9 Sep 2016 21:05:03 +0200 Subject: [PATCH] Add load level scene --- tabakrolletjie/scenes/load_level.py | 65 +++++++++++++++++++++++++++++ tabakrolletjie/scenes/menu.py | 10 +++-- 2 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 tabakrolletjie/scenes/load_level.py diff --git a/tabakrolletjie/scenes/load_level.py b/tabakrolletjie/scenes/load_level.py new file mode 100644 index 0000000..7b34789 --- /dev/null +++ b/tabakrolletjie/scenes/load_level.py @@ -0,0 +1,65 @@ +""" Menu scene. """ + +import os + +import pygame.locals as pgl + +from .base import BaseScene +from ..events import SceneChangeEvent +from ..widgets import TextButton +from ..loader import loader + + +class LoadLevelScene(BaseScene): + + def _list_stations(self): + station_path = loader.full_path("stations") + files = os.listdir(station_path) + result = [] + for f in files: + name, ext = os.path.splitext(f) + if ext == '.json': + result.append((name, f)) + result.sort() + return result + + def enter(self, gamestate): + """Construct list of stations""" + height = 50 + self._buttons = [] + width = 30 + for station, filename in self._list_stations(): + pos = (width, height) + button = TextButton(station, (255, 255, 255), filename, pos) + if width < 400: + width += 350 + else: + width = 30 + height += button.get_height() + 20 + self._buttons.append(button) + + def render(self, surface, gamestate): + surface.fill((0, 128, 128)) + for button in self._buttons: + button.render(surface) + + def _get_pressed(self, ev): + for button in self._buttons: + if button.pressed(ev): + return button.name + return None + + def _do_load(self, name, gamestate): + from .day import DayScene + print "Loading station", name + gamestate.load_station(name) + SceneChangeEvent.post(scene=DayScene()) + + def event(self, ev, gamestate): + if ev.type == pgl.KEYDOWN: + if ev.key == pgl.K_l: + self._do_load("station_alpha.json", gamestate) + elif ev.type == pgl.MOUSEBUTTONDOWN: + pressed = self._get_pressed(ev) + if pressed: + self._do_load(pressed, gamestate) diff --git a/tabakrolletjie/scenes/menu.py b/tabakrolletjie/scenes/menu.py index 1d14abc..7a4366a 100644 --- a/tabakrolletjie/scenes/menu.py +++ b/tabakrolletjie/scenes/menu.py @@ -47,6 +47,10 @@ class MenuScene(BaseScene): from .day import DayScene SceneChangeEvent.post(scene=DayScene()) + def _do_load_level(self): + from .load_level import LoadLevelScene + SceneChangeEvent.post(scene=LoadLevelScene()) + def event(self, ev, gamestate): if ev.type == pgl.KEYDOWN: if ev.key in (pgl.K_q, pgl.K_ESCAPE): @@ -57,13 +61,11 @@ class MenuScene(BaseScene): elif ev.key == pgl.K_d: self._do_day() elif ev.key == pgl.K_l: - print "Loading Station Alpha ..." - gamestate.load_station("station-alpha.json") + self._do_load_level() elif ev.type == pgl.MOUSEBUTTONDOWN: pressed = self._get_pressed(ev) if pressed == 'load level': - print "Loading Station Alpha ..." - gamestate.load_station("station-alpha.json") + self._do_load_level() elif pressed == 'start game': self._do_day() elif pressed: -- 2.34.1