5 import pygame.locals as pgl
7 from .base import BaseScene
8 from ..constants import SCREEN_SIZE
9 from ..events import SceneChangeEvent
10 from ..widgets import TextButton, ImageButton
11 from ..loader import loader
14 class LoadLevelScene(BaseScene):
16 def _list_stations(self):
17 station_path = loader.full_path("stations")
18 files = [f for f in os.listdir(station_path) if f.endswith(".json")]
20 return [loader.load_station(f) for f in files]
22 def enter(self, gamestate):
23 """Construct list of stations"""
24 width = SCREEN_SIZE[0] / 2
25 self._title = TextButton(
26 'Select a planet:', (255, 255, 255), pos=(width, 75),
27 size=32, font='bold', centre=True)
30 for station in self._list_stations():
31 title = station["config"]["name"]
33 button = TextButton(title, (255, 255, 255), pos=pos, centre=True)
34 button.station = station
35 height += button.get_height() + 20
36 self._buttons.append(button)
38 self._tools = self.create_tools(gamestate)
40 def create_tools(self, gamestate):
42 tools.append(ImageButton(
43 '32', 'exit.png', name='exit',
44 pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)))
47 def render(self, surface, gamestate):
48 surface.fill((0, 128, 128))
49 self._title.render(surface)
50 for button in self._buttons:
51 button.render(surface)
53 for tool in self._tools:
56 def _get_pressed(self, ev):
57 for button in self._buttons:
58 if button.pressed(ev):
62 def _do_load(self, station, gamestate):
63 from .day import DayScene
64 print "Loading station", station["config"]["name"]
65 gamestate.set_station(station)
66 SceneChangeEvent.post(scene=DayScene())
68 def event(self, ev, gamestate):
69 if ev.type == pgl.KEYDOWN:
70 if ev.key in (pgl.K_q, pgl.K_ESCAPE):
71 from .menu import MenuScene
72 SceneChangeEvent.post(scene=MenuScene())
73 elif ev.type == pgl.MOUSEBUTTONDOWN:
74 pressed = self._get_pressed(ev)
76 self._do_load(pressed.station, gamestate)
79 for tool in self._tools:
81 if tool.name == 'exit':
82 from .menu import MenuScene
83 SceneChangeEvent.post(scene=MenuScene())