ddaab48d284096320093ae20983f94705d509658
[naja.git] / naja / scenes / introduction.py
1 """
2 Load and save scenes.
3 """
4
5 import pygame.locals as pgl
6 import pygame
7
8 from naja import constants
9 from naja.constants import KEYS, SCREEN, PALETTE
10 from naja.events import SceneChangeEvent, LoadGameEvent
11 from naja.gamestate import GameState
12 from naja.scenes.scene import Scene
13 from naja.scenes.howto import HowtoScene
14 from naja.widgets.image_box import ImageBox
15 from naja.widgets.selector import SelectorWidget
16 from naja.widgets.text import TextWidget
17 from naja.widgets.text import TextBoxWidget
18 from naja.resources import resources
19 from naja.scenes.dummygame import DummyGameScene
20 from naja.widgets.image_box import PreRenderedImageBox
21
22 class IntroductionScene(Scene):
23     def __init__(self, state):
24         super(IntroductionScene, self).__init__(state)
25
26         background = ImageBox(
27             (0, 0), "screens/splash.png")
28         self.add(background)
29
30         self.intro = 0
31
32         selector = SelectorWidget()
33         self.add(selector)
34
35         y_offset, y_diff = 270, 40
36         x_offset = 400
37
38         y_offset += y_diff
39         title = TextWidget(
40             (x_offset, y_offset), 'Getting started', colour='white',
41             centre=True)
42         self.add(title)
43
44         y_offset += y_diff
45         howto = TextWidget(
46             (x_offset, y_offset), 'How to play', colour='white', centre=True)
47         howto.add_callback('click', self.howto_scene)
48         selector.add(howto)
49
50         y_offset += y_diff
51         intro = TextWidget(
52             (x_offset, y_offset), 'Introductory Level', colour='white',
53             centre=True)
54         intro.add_callback('click', self.introduction_to_screen_1)
55         selector.add(intro)
56
57         y_offset += 2*y_diff
58         back = TextWidget(
59             (x_offset, y_offset), 'Press ESC to return to the main menu.',
60             colour='white', centre=True)
61         self.add(back)
62
63     def howto_scene(self, event):
64         SceneChangeEvent.post(HowtoScene)
65
66     def introduction_level(self, event):
67         from naja.scenes.game import GameScene
68         LoadGameEvent.post(state=GameState.new(deck='introduction'))
69         SceneChangeEvent.post(GameScene)
70
71     def make_game_surface(self):
72         self.container.widgets = []
73         state = resources.get_json('tutorial/tutorial_screen_1.json')
74         dummy_game = DummyGameScene(state)
75         game_surface = pygame.surface.Surface(SCREEN)
76         dummy_game.render(game_surface)
77         self.add(PreRenderedImageBox((0, 0), game_surface))
78         return game_surface
79
80     def introduction_to_screen_1(self, event):
81         game_surface = self.make_game_surface()
82         self.intro = 1
83
84         self.add(TextBoxWidget(
85             (24, 72), '\n'.join([
86                 "These are your robot's status bits. On the left we have "
87                 "the Most Signiticant Bit (MSB, currently set), then Red, "
88                 "Green (set) and Blue (set) key bits, and finally the four "
89                 "bits which control movement options, of which only left is "
90                 "set. You can only move in directions which are set. \n"
91                 "Enter to continue.",
92             ]), fontsize=32,
93             colour=PALETTE.GREEN, padding=12, border=8,
94             bg_colour=PALETTE.BLACK, border_colour=PALETTE.GREEN,
95             box_width=400, view_port=(500, 800)))
96
97         pygame.draw.rect(game_surface, PALETTE.GREEN, (0, 0, 480, 60), 8)
98
99     def introduction_to_screen_2(self, event):
100         game_surface = self.make_game_surface()
101         self.intro = 2
102
103         self.add(TextBoxWidget(
104             (24, 172), '\n'.join([
105                 "These are your health and score. On the left we have four "
106                 "health bits. The robot has taken one damage, so only three "
107                 "are lit. The stars are points you have scored. Once you have "
108                 "all of them you win the game! Total health and required "
109                 "points vary according to the game. \n"
110                 "Enter to continue.",
111             ]), fontsize=32,
112             colour=PALETTE.GREEN, padding=12, border=8,
113             bg_colour=PALETTE.BLACK, border_colour=PALETTE.GREEN,
114             box_width=400, view_port=(500, 800)))
115
116         pygame.draw.rect(game_surface, PALETTE.GREEN, (0, 540, 480, 60), 8)
117
118     def introduction_to_screen_3(self, event):
119         game_surface = self.make_game_surface()
120         self.intro = 3
121
122         self.add(TextBoxWidget(
123             (24, 132), '\n'.join([
124                 "This text box describes the current game mode. The game is "
125                 "divided into movements and actions, and each mode has "
126                 "different options, shown here.\n"
127                 "During movement, you can examine the entire board, but you "
128                 "can only move to hi-lighted tiles (including the one you are "
129                 "on. Actions come next... \n"
130                 "Enter to continue.",
131             ]), fontsize=32,
132             colour=PALETTE.GREEN, padding=12, border=8,
133             bg_colour=PALETTE.BLACK, border_colour=PALETTE.GREEN,
134             box_width=400, view_port=(500, 800)))
135
136         pygame.draw.rect(game_surface, PALETTE.GREEN, (488, 416, 304, 132), 8)
137
138     def introduction_to_screen_4(self, event):
139         game_surface = self.make_game_surface()
140         self.intro = 4
141
142         self.add(TextBoxWidget(
143             (24, 72), '\n'.join([
144                 "This text box shows actions available on the tile. "
145                 "Some actions have requirements and are only selectable if "
146                 "you have the correct bits set (unavailable actions are "
147                 "greyed out). Actions usually change the bits set on "
148                 "your robot. This is often not beneficial.\n"
149                 "Some actions have additional effects if the MSB is "
150                 "set.\n"
151                 "Enter to continue.",
152             ]), fontsize=32,
153             colour=PALETTE.GREEN, padding=12, border=8,
154             bg_colour=PALETTE.BLACK, border_colour=PALETTE.GREEN,
155             box_width=400, view_port=(500, 800)))
156
157         pygame.draw.rect(game_surface, PALETTE.GREEN, (488, 52, 304, 240), 8)
158
159
160     def handle_scene_event(self, ev):
161         if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT:
162             from naja.scenes.menu import MenuScene
163             SceneChangeEvent.post(MenuScene)
164             return
165         if ev.type == pgl.KEYDOWN and ev.key in KEYS.SELECT:
166             if 0 < self.intro < 5:
167                 if self.intro == 1:
168                     self.introduction_to_screen_2(ev)
169                 elif self.intro == 2:
170                     self.introduction_to_screen_3(ev)
171                 elif self.intro == 3:
172                     self.introduction_to_screen_4(ev)
173                 else:
174                     self.introduction_level(ev)
175             return