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