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