We set defaults in new_game() don't pass parameters that aren't needed
[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.howto import HowtoScene
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         resume = TextWidget(
38             (x_offset, y_offset), 'Resume Game', colour=PALETTE.WHITE,
39             unselectable_colour=PALETTE.GREY, centre=True)
40         resume.add_callback('click', self.scene_callback(GameScene))
41         resume.set_selectable_callback(lambda: state is not None)
42         selector.add(resume)
43
44         y_offset += y_diff
45         new = TextWidget(
46             (x_offset, y_offset), 'New Random Game', colour=PALETTE.WHITE,
47             centre=True)
48         new.add_callback('click', self.scene_callback(NewGameScene))
49         selector.add(new)
50
51         y_offset += y_diff
52         new = TextWidget(
53             (x_offset, y_offset), 'New Puzzle', colour=PALETTE.WHITE,
54             centre=True)
55         new.add_callback('click', self.scene_callback(PuzzleListScene))
56         selector.add(new)
57
58         y_offset += y_diff
59         load = TextWidget(
60             (x_offset, y_offset), 'Load Game', colour=PALETTE.WHITE,
61             centre=True)
62         load.add_callback('click', self.scene_callback(LoadGameScene))
63         selector.add(load)
64
65         y_offset += y_diff
66         save = TextWidget(
67             (x_offset, y_offset), 'Save Game', colour=PALETTE.WHITE,
68             unselectable_colour=PALETTE.GREY, centre=True)
69         save.add_callback('click', self.scene_callback(SaveGameScene))
70         save.set_selectable_callback(lambda: state is not None)
71         selector.add(save)
72
73         y_offset += y_diff
74         howto = TextWidget(
75             (x_offset, y_offset), 'How To Play', colour=PALETTE.WHITE,
76             centre=True)
77         howto.add_callback('click', self.scene_callback(HowtoScene))
78         selector.add(howto)
79
80         y_offset += y_diff
81         credits = TextWidget(
82             (x_offset, y_offset), 'Credits', colour=PALETTE.WHITE,
83             centre=True)
84         credits.add_callback('click', self.scene_callback(CreditsScene))
85         selector.add(credits)
86
87         y_offset += y_diff
88         quit = TextWidget((x_offset, y_offset), 'Quit', colour=PALETTE.WHITE,
89                           centre=True)
90         quit.add_callback('click', lambda event: QuitGameEvent.post())
91         selector.add(quit)
92
93         self.konami = []
94         if not sound.playing_music:
95             sound.play_music('scape.ogg', 0.25)
96
97     def scene_callback(self, scene_class):
98         return lambda event: SceneChangeEvent.post(scene_class)
99
100     def handle_event(self, ev):
101         if ev.type == pgl.KEYDOWN and self.konami is not None:
102             if ev.key in KEYS.KONAMI:
103                 self.konami.append(ev.key)
104                 if self.konami == KONAMI_CODE:
105                     options['cheat_enabled'] = True
106                     print "Cheats enabled."
107                     print "  Try hitting some number keys."
108                     self.konami = None
109                 elif self.konami != KONAMI_CODE[:len(self.konami)]:
110                     self.konami = None
111         return super(MenuScene, self).handle_event(ev)
112
113     def handle_scene_event(self, ev):
114         if ev.type == pgl.KEYDOWN:
115             if ev.key in KEYS.QUIT:
116                 QuitGameEvent.post()
117                 return