data/states/main_menu.py (view raw)
1import pickle, sys, os
2import pygame as pg
3from .. import setup, tools, tilerender
4from .. import observer
5from .. import constants as c
6from . import death
7
8class Menu(tools._State):
9 def __init__(self):
10 super(Menu, self).__init__()
11 self.music = setup.MUSIC['kings_theme']
12 self.music_title = 'kings_theme'
13 self.volume = 0.4
14 self.next = c.INSTRUCTIONS
15 self.tmx_map = setup.TMX['title']
16 self.name = c.MAIN_MENU
17 self.startup(0, 0)
18
19 def startup(self, *args):
20 self.renderer = tilerender.Renderer(self.tmx_map)
21 self.map_image = self.renderer.make_2x_map()
22 self.map_rect = self.map_image.get_rect()
23 self.viewport = self.make_viewport(self.map_image)
24 self.level_surface = pg.Surface(self.map_rect.size)
25 self.title_box = setup.GFX['title_box']
26 self.title_rect = self.title_box.get_rect()
27 self.title_rect.midbottom = self.viewport.midbottom
28 self.title_rect.y -= 30
29 self.state_dict = self.make_state_dict()
30 self.state = c.TRANSITION_IN
31 self.alpha = 255
32 self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
33 self.transition_surface.fill(c.BLACK_BLUE)
34 self.transition_surface.set_alpha(self.alpha)
35
36 def make_viewport(self, map_image):
37 """
38 Create the viewport to view the level through.
39 """
40 map_rect = map_image.get_rect()
41 return setup.SCREEN.get_rect(bottomright=map_rect.bottomright)
42
43 def make_state_dict(self):
44 """
45 Make the dictionary of state methods for the level.
46 """
47 state_dict = {c.TRANSITION_IN: self.transition_in,
48 c.TRANSITION_OUT: self.transition_out,
49 c.NORMAL: self.normal_update}
50
51 return state_dict
52
53 def update(self, surface, *args):
54 """
55 Update scene.
56 """
57 update_level = self.state_dict[self.state]
58 update_level()
59 self.draw_level(surface)
60
61 def draw_level(self, surface):
62 """
63 Blit tmx map and title box onto screen.
64 """
65 self.level_surface.blit(self.map_image, self.viewport, self.viewport)
66 self.level_surface.blit(self.title_box, self.title_rect)
67 surface.blit(self.level_surface, (0,0), self.viewport)
68 surface.blit(self.transition_surface, (0,0))
69
70 def get_event(self, event):
71 if event.type == pg.KEYDOWN:
72 self.state = c.TRANSITION_OUT
73
74 def transition_in(self):
75 """
76 Transition into scene with a fade.
77 """
78 self.transition_surface.set_alpha(self.alpha)
79 self.alpha -= c.TRANSITION_SPEED
80 if self.alpha <= 0:
81 self.alpha = 0
82 self.state = c.NORMAL
83
84
85 def transition_out(self):
86 """
87 Transition out of scene with a fade.
88 """
89 self.transition_surface.set_alpha(self.alpha)
90 self.alpha += c.TRANSITION_SPEED
91 if self.alpha >= 255:
92 self.done = True
93
94 def normal_update(self):
95 pass
96
97
98class Instructions(tools._State):
99 """
100 Instructions page.
101 """
102 def __init__(self):
103 super(Instructions, self).__init__()
104 self.tmx_map = setup.TMX['title']
105 self.music = None
106 self.music_title = None
107
108 def startup(self, *args):
109 self.renderer = tilerender.Renderer(self.tmx_map)
110 self.map_image = self.renderer.make_2x_map()
111 self.map_rect = self.map_image.get_rect()
112 self.viewport = self.make_viewport(self.map_image)
113 self.level_surface = pg.Surface(self.map_rect.size)
114 self.title_box = self.set_image()
115 self.title_rect = self.title_box.get_rect()
116 self.title_rect.midbottom = self.viewport.midbottom
117 self.title_rect.y -= 30
118 self.game_data = tools.create_game_data_dict()
119 self.next = self.set_next_scene()
120 self.state_dict = self.make_state_dict()
121 self.name = c.MAIN_MENU
122 self.state = c.TRANSITION_IN
123 self.alpha = 255
124 self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
125 self.transition_surface.fill(c.BLACK_BLUE)
126 self.transition_surface.set_alpha(self.alpha)
127 self.observers = [observer.SoundEffects()]
128
129 def notify(self, event):
130 """
131 Notify all observers of event.
132 """
133 for observer in self.observers:
134 observer.on_notify(event)
135
136 def set_next_scene(self):
137 """
138 Check if there is a saved game. If not, start
139 game at begining. Otherwise go to load game scene.
140 """
141 if not os.path.isfile("save.p"):
142 next_scene = c.OVERWORLD
143 else:
144 next_scene = c.LOADGAME
145
146 return next_scene
147
148 def set_image(self):
149 """
150 Set image for message box.
151 """
152 return setup.GFX['instructions_box']
153
154 def make_viewport(self, map_image):
155 """
156 Create the viewport to view the level through.
157 """
158 map_rect = map_image.get_rect()
159 return setup.SCREEN.get_rect(bottomright=map_rect.bottomright)
160
161 def make_state_dict(self):
162 """
163 Make the dictionary of state methods for the level.
164 """
165 state_dict = {c.TRANSITION_IN: self.transition_in,
166 c.TRANSITION_OUT: self.transition_out,
167 c.NORMAL: self.normal_update}
168
169 return state_dict
170
171 def update(self, surface, keys, *args):
172 """
173 Update scene.
174 """
175 update_level = self.state_dict[self.state]
176 update_level(keys)
177 self.draw_level(surface)
178
179 def draw_level(self, surface):
180 """
181 Blit tmx map and title box onto screen.
182 """
183 self.level_surface.blit(self.map_image, self.viewport, self.viewport)
184 self.level_surface.blit(self.title_box, self.title_rect)
185 self.draw_arrow()
186 surface.blit(self.level_surface, (0,0), self.viewport)
187 surface.blit(self.transition_surface, (0,0))
188
189 def draw_arrow(self):
190 pass
191
192 def get_event(self, event):
193 if event.type == pg.KEYDOWN:
194 self.state = c.TRANSITION_OUT
195
196 def transition_in(self, *args):
197 """
198 Transition into scene with a fade.
199 """
200 self.transition_surface.set_alpha(self.alpha)
201 self.alpha -= c.TRANSITION_SPEED
202 if self.alpha <= 0:
203 self.alpha = 0
204 self.state = c.NORMAL
205
206 def transition_out(self, *args):
207 """
208 Transition out of scene with a fade.
209 """
210 self.transition_surface.set_alpha(self.alpha)
211 self.alpha += c.TRANSITION_SPEED
212 if self.alpha >= 255:
213 self.done = True
214
215 def normal_update(self, *args):
216 pass
217
218
219class LoadGame(Instructions):
220 def __init__(self):
221 super(LoadGame, self).__init__()
222 self.arrow = death.Arrow(200, 260)
223 self.arrow.pos_list[1] += 34
224 self.allow_input = False
225
226 def set_image(self):
227 """
228 Set image for message box.
229 """
230 return setup.GFX['loadgamebox']
231
232 def draw_arrow(self):
233 self.level_surface.blit(self.arrow.image, self.arrow.rect)
234
235 def get_event(self, event):
236 pass
237
238 def normal_update(self, keys):
239 if self.allow_input:
240 if keys[pg.K_DOWN] and self.arrow.index == 0:
241 self.arrow.index = 1
242 self.notify(c.CLICK)
243 self.allow_input = False
244 elif keys[pg.K_UP] and self.arrow.index == 1:
245 self.arrow.index = 0
246 self.notify(c.CLICK)
247 self.allow_input = False
248 elif keys[pg.K_SPACE]:
249 if self.arrow.index == 0:
250 self.game_data = pickle.load(open("save.p", "rb"))
251 self.next = c.TOWN
252 self.state = c.TRANSITION_OUT
253 else:
254 self.next = c.OVERWORLD
255 self.state = c.TRANSITION_OUT
256 self.notify(c.CLICK2)
257
258 self.arrow.rect.y = self.arrow.pos_list[self.arrow.index]
259
260 if not keys[pg.K_DOWN] and not keys[pg.K_UP]:
261 self.allow_input = True
262
263
264
265