f9c51c1fa13b1a0dbddf5bc9126247ae1a7e1adb
[naja.git] / naja / scenes / puzzlelist.py
1 """
2 Load and save scenes.
3 """
4
5 import functools
6 import pygame.locals as pgl
7
8 from naja.constants import KEYS, PUZZLES
9 from naja.events import SceneChangeEvent, LoadGameEvent
10 from naja.gamestate import GameState, load_location_deck
11 from naja.scenes.scene import Scene
12 from naja.widgets.image_box import ImageBox
13 from naja.widgets.selector import SelectorWidget
14 from naja.widgets.text import TextWidget
15
16
17 class PuzzleListScene(Scene):
18     def __init__(self, state):
19         super(PuzzleListScene, self).__init__(state)
20
21         background = ImageBox(
22             (0, 0), "screens/splash.png")
23         self.add(background)
24
25         selector = SelectorWidget()
26         selector.position = 1
27         self.add(selector)
28
29         y_offset, y_diff = 270, 36
30         x_offset = 400
31
32         y_offset += y_diff
33         title = TextWidget(
34             (x_offset, y_offset), 'CHOOSE PUZZLE', colour='white',
35             centre=True)
36         self.add(title)
37
38         for puzzle in PUZZLES:
39             y_offset += y_diff
40             deck = load_location_deck('puzzles/%s' % puzzle)
41             puzzle_but = TextWidget(
42                (x_offset, y_offset), deck['description'], fontsize=32,
43                colour='white', centre=True)
44             callback = functools.partial(self.start_puzzle_game,
45                                          puzzle=puzzle, deck=deck)
46             puzzle_but.add_callback('click', callback)
47             selector.add(puzzle_but)
48
49     def start_puzzle_game(self, event, puzzle=None, deck=None):
50         from naja.scenes.game import GameScene
51         max_health = deck.get('max_health', 4)
52         wins_required = deck.get('wins_required', 4)
53         level = 'puzzles/%s' % puzzle
54         state = GameState.new(deck=level, max_health=max_health,
55                               wins_required=wins_required)
56         LoadGameEvent.post(state=state)
57         SceneChangeEvent.post(GameScene)
58
59     def handle_scene_event(self, ev):
60         if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT:
61             from naja.scenes.menu import MenuScene
62             SceneChangeEvent.post(MenuScene)
63             return