Add instructions to the readme as well
[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
8 from naja.events import SceneChangeEvent, QuitGameEvent
9 from naja.scenes.scene import Scene
10 from naja.scenes.credits import CreditsScene
11 from naja.scenes.game import GameScene
12 from naja.scenes.howto import HowtoScene
13 from naja.scenes.load_save import LoadGameScene, SaveGameScene
14 from naja.scenes.new_game import NewGameScene
15 from naja.widgets.selector import SelectorWidget
16 from naja.widgets.text import TextWidget
17
18
19 class MenuScene(Scene):
20     def __init__(self, state):
21         super(MenuScene, self).__init__(state)
22         selector = SelectorWidget()
23         self.add(selector)
24         y_offset = 0
25
26         y_offset += 50
27         resume = TextWidget(
28             (100, y_offset), 'Resume Game', colour=PALETTE.WHITE,
29             unselectable_colour=PALETTE.GREY)
30         resume.add_callback('click', self.scene_callback(GameScene))
31         resume.set_selectable_callback(lambda: state is not None)
32         selector.add(resume)
33
34         y_offset += 50
35         new = TextWidget((100, y_offset), 'New Game', colour=PALETTE.WHITE)
36         new.add_callback('click', self.scene_callback(NewGameScene))
37         selector.add(new)
38
39         y_offset += 50
40         load = TextWidget((100, y_offset), 'Load', colour=PALETTE.WHITE)
41         load.add_callback('click', self.scene_callback(LoadGameScene))
42         selector.add(load)
43
44         y_offset += 50
45         save = TextWidget(
46             (100, y_offset), 'Save', colour=PALETTE.WHITE,
47             unselectable_colour=PALETTE.GREY)
48         save.add_callback('click', self.scene_callback(SaveGameScene))
49         save.set_selectable_callback(lambda: state is not None)
50         selector.add(save)
51
52         y_offset += 50
53         howto = TextWidget(
54             (100, y_offset), 'How To Play', fontsize=32, colour='white')
55         howto.add_callback(
56             'click', lambda event: SceneChangeEvent.post(HowtoScene))
57         selector.add(howto)
58
59         y_offset += 50
60         credits = TextWidget((100, y_offset), 'Credits', colour=PALETTE.WHITE)
61         credits.add_callback('click', self.scene_callback(CreditsScene))
62         selector.add(credits)
63
64         y_offset += 50
65         quit = TextWidget((100, y_offset), 'Quit', colour=PALETTE.WHITE)
66         quit.add_callback('click', lambda event: QuitGameEvent.post())
67         selector.add(quit)
68
69     def scene_callback(self, scene_class):
70         return lambda event: SceneChangeEvent.post(scene_class)
71
72     def handle_scene_event(self, ev):
73         if ev.type == pgl.KEYDOWN:
74             if ev.key in KEYS.QUIT:
75                 QuitGameEvent.post()
76                 return