+"""
+Load and save scenes.
+"""
+
+import functools
+import pygame.locals as pgl
+
+from naja.constants import KEYS, PUZZLES
+from naja.events import SceneChangeEvent, LoadGameEvent
+from naja.gamestate import GameState, load_location_deck
+from naja.scenes.scene import Scene
+from naja.widgets.image_box import ImageBox
+from naja.widgets.selector import SelectorWidget
+from naja.widgets.text import TextWidget
+
+
+class PuzzleListScene(Scene):
+ def __init__(self, state):
+ super(PuzzleListScene, self).__init__(state)
+
+ background = ImageBox(
+ (0, 0), "screens/splash.png")
+ self.add(background)
+
+ selector = SelectorWidget()
+ selector.position = 1
+ self.add(selector)
+
+ y_offset, y_diff = 270, 36
+ x_offset = 400
+
+ y_offset += y_diff
+ title = TextWidget(
+ (x_offset, y_offset), 'CHOOSE PUZZLE', colour='white',
+ centre=True)
+ self.add(title)
+
+ for puzzle in PUZZLES:
+ y_offset += y_diff
+ deck = load_location_deck('puzzles/%s' % puzzle)
+ puzzle_but = TextWidget(
+ (x_offset, y_offset), deck['description'], fontsize=32,
+ colour='white', centre=True)
+ callback = functools.partial(self.start_puzzle_game,
+ puzzle=puzzle, deck=deck)
+ puzzle_but.add_callback('click', callback)
+ selector.add(puzzle_but)
+
+ def start_puzzle_game(self, event, puzzle=None, deck=None):
+ from naja.scenes.game import GameScene
+ max_health = deck.get('max_health', 4)
+ wins_required = deck.get('wins_required', 4)
+ level = 'puzzles/%s' % puzzle
+ state = GameState.new(deck=level, max_health=max_health,
+ wins_required=wins_required)
+ LoadGameEvent.post(state=state)
+ SceneChangeEvent.post(GameScene)
+
+ def handle_scene_event(self, ev):
+ if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT:
+ from naja.scenes.menu import MenuScene
+ SceneChangeEvent.post(MenuScene)
+ return