Optional sanity check for actions.
[naja.git] / naja / scenes / menu.py
1 """
2 Main menu scene.
3 """
4
5 import pygame.locals as pgl
6
7 from naja.constants import KEYS, PALETTE, KONAMI_CODE
8 from naja.events import SceneChangeEvent, QuitGameEvent
9 from naja.options import options
10 from naja.scenes.scene import Scene
11 from naja.scenes.credits import CreditsScene
12 from naja.scenes.game import GameScene
13 from naja.scenes.introduction import IntroductionScene
14 from naja.scenes.load_save import LoadGameScene, SaveGameScene
15 from naja.scenes.new_game import NewGameScene
16 from naja.scenes.puzzlelist import PuzzleListScene
17 from naja.widgets.selector import SelectorWidget
18 from naja.widgets.text import TextWidget
19 from naja.widgets.image_box import ImageBox
20 from naja.sound import sound
21
22
23 class MenuScene(Scene):
24     def __init__(self, state):
25         super(MenuScene, self).__init__(state)
26
27         background = ImageBox(
28             (0, 0), "screens/splash.png")
29         self.add(background)
30
31         selector = SelectorWidget()
32         self.add(selector)
33         y_offset, y_diff = 270, 36
34         x_offset = 400
35
36         y_offset += y_diff
37         intro = TextWidget(
38             (x_offset, y_offset), 'Introduction', colour=PALETTE.WHITE,
39             centre=True)
40         intro.add_callback('click', self.scene_callback(IntroductionScene))
41         selector.add(intro)
42
43         y_offset += y_diff
44         resume = TextWidget(
45             (x_offset, y_offset), 'Resume Game', colour=PALETTE.WHITE,
46             unselectable_colour=PALETTE.GREY, centre=True)
47         resume.add_callback('click', self.scene_callback(GameScene))
48         resume.set_selectable_callback(lambda: state is not None)
49         selector.add(resume)
50
51         if state is None:
52             selector.position = 2
53         else:
54             selector.position = 1
55
56         y_offset += y_diff
57         new = TextWidget(
58             (x_offset, y_offset), 'New Random Game', colour=PALETTE.WHITE,
59             centre=True)
60         new.add_callback('click', self.scene_callback(NewGameScene))
61         selector.add(new)
62
63         y_offset += y_diff
64         new = TextWidget(
65             (x_offset, y_offset), 'New Puzzle', colour=PALETTE.WHITE,
66             centre=True)
67         new.add_callback('click', self.scene_callback(PuzzleListScene))
68         selector.add(new)
69
70         y_offset += y_diff
71         load = TextWidget(
72             (x_offset, y_offset), 'Load Game', colour=PALETTE.WHITE,
73             centre=True)
74         load.add_callback('click', self.scene_callback(LoadGameScene))
75         selector.add(load)
76
77         y_offset += y_diff
78         save = TextWidget(
79             (x_offset, y_offset), 'Save Game', colour=PALETTE.WHITE,
80             unselectable_colour=PALETTE.GREY, centre=True)
81         save.add_callback('click', self.scene_callback(SaveGameScene))
82         save.set_selectable_callback(lambda: state is not None)
83         selector.add(save)
84
85         y_offset += y_diff
86         credits = TextWidget(
87             (x_offset, y_offset), 'Credits', colour=PALETTE.WHITE,
88             centre=True)
89         credits.add_callback('click', self.scene_callback(CreditsScene))
90         selector.add(credits)
91
92         y_offset += y_diff
93         quit = TextWidget((x_offset, y_offset), 'Quit', colour=PALETTE.WHITE,
94                           centre=True)
95         quit.add_callback('click', lambda event: QuitGameEvent.post())
96         selector.add(quit)
97
98         self.konami = []
99         if not sound.playing_music:
100             sound.play_music('scape.ogg', 0.25)
101
102     def scene_callback(self, scene_class):
103         return lambda event: SceneChangeEvent.post(scene_class)
104
105     def handle_event(self, ev):
106         if ev.type == pgl.KEYDOWN and self.konami is not None:
107             if ev.key in KEYS.KONAMI:
108                 self.konami.append(ev.key)
109                 if self.konami == KONAMI_CODE:
110                     options['cheat_enabled'] = True
111                     print "Cheats enabled."
112                     print "  Try hitting some number keys."
113                     self.konami = None
114                 elif self.konami != KONAMI_CODE[:len(self.konami)]:
115                     self.konami = None
116         return super(MenuScene, self).handle_event(ev)
117
118     def handle_scene_event(self, ev):
119         if ev.type == pgl.KEYDOWN:
120             if ev.key in KEYS.QUIT:
121                 QuitGameEvent.post()
122                 return