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