data/menugui.py (view raw)
1"""
2This class controls all the GUI for the player
3menu screen.
4"""
5import pygame as pg
6from . import setup
7from . import constants as c
8
9
10class SmallArrow(pg.sprite.Sprite):
11 """Small arrow for menu"""
12 def __init__(self):
13 super(SmallArrow, self).__init__()
14 self.image = setup.GFX['smallarrow']
15 self.rect = self.image.get_rect()
16 self.pos_list = self.make_pos_list()
17
18
19 def make_pos_list(self):
20 """Make the list of possible arrow positions"""
21 pos_list = []
22
23 for i in range(4):
24 pos = (35, 356 + (i * 50))
25 pos_list.append(pos)
26
27 return pos_list
28
29
30 def update(self, pos_index):
31 """Update arrow position"""
32 self.rect.topleft = self.pos_list[pos_index]
33
34
35 def draw(self, surface):
36 """Draw to surface"""
37 surface.blit(self.image, self.rect)
38
39
40
41class GoldBox(pg.sprite.Sprite):
42 def __init__(self, inventory):
43 self.inventory = inventory
44 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
45 self.image, self.rect = self.make_image()
46
47 def make_image(self):
48 """Make the surface for the gold box"""
49 image = setup.GFX['goldbox2']
50 rect = image.get_rect(left=10, top=234)
51
52 surface = pg.Surface(rect.size)
53 surface.set_colorkey(c.BLACK)
54 surface.blit(image, (0, 0))
55
56 text = "Gold: " + str(self.inventory['gold'])
57 text_render = self.font.render(text, True, c.NEAR_BLACK)
58 text_rect = text_render.get_rect(centerx=130,
59 centery=35)
60 surface.blit(text_render, text_rect)
61
62 return surface, rect
63
64
65 def draw(self, surface):
66 """Draw to surface"""
67 surface.blit(self.image, self.rect)
68
69
70
71class InfoBox(pg.sprite.Sprite):
72 def __init__(self, inventory, player_stats):
73 super(InfoBox, self).__init__()
74 self.inventory = inventory
75 self.player_stats = player_stats
76 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
77 self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 24)
78 self.title_font.set_underline(True)
79 self.possible_items = ['Healing Potion', 'Chain Mail',
80 'Wooden Shield', 'Long Sword',
81 'Rapier']
82 self.possible_magic = ['Fire Blast', 'Cure']
83 self.quantity_items = ['Healing Potion']
84 self.state_dict = self.make_state_dict()
85
86
87 def make_state_dict(self):
88 """Make the dictionary of state methods"""
89 state_dict = {'stats': self.show_player_stats,
90 'items': self.show_items,
91 'magic': self.show_magic}
92
93 return state_dict
94
95
96 def show_player_stats(self):
97 """Show the player's main stats"""
98 title = 'STATS'
99 stat_list = ['Level', 'Health',
100 'Magic Points', 'Experience to next level']
101 surface, rect = self.make_blank_info_box(title)
102
103 for i, stat in enumerate(stat_list):
104 if stat == 'Health' or stat == 'Magic Points':
105 text = (stat + ": " + str(self.player_stats[stat]['current']) +
106 " / " + str(self.player_stats[stat]['maximum']))
107 else:
108 text = stat + ": " + str(self.player_stats[stat])
109 text_image = self.font.render(text, True, c.NEAR_BLACK)
110 text_rect = text_image.get_rect(x=50, y=80+(i*50))
111 surface.blit(text_image, text_rect)
112
113 self.image = surface
114 self.rect = rect
115
116
117 def show_items(self):
118 """Show list of items the player has"""
119 title = 'ITEMS'
120 item_list = []
121 for item in self.inventory:
122 if item in self.possible_items:
123 item_list.append(item)
124
125 surface, rect = self.make_blank_info_box(title)
126
127 for i, item in enumerate(item_list):
128 if item in self.quantity_items:
129 text = item + ": " + str(self.inventory[item]['quantity'])
130 else:
131 text = item
132 text_image = self.font.render(text, True, c.NEAR_BLACK)
133 text_rect = text_image.get_rect(x=50, y=80+(i*50))
134 surface.blit(text_image, text_rect)
135
136 self.image = surface
137 self.rect = rect
138
139
140 def show_magic(self):
141 """Show list of magic spells the player knows"""
142 title = 'MAGIC'
143 item_list = []
144 for item in self.inventory:
145 if item in self.possible_magic:
146 item_list.append(item)
147 item_list = sorted(item_list)
148
149 surface, rect = self.make_blank_info_box(title)
150
151 for i, item in enumerate(item_list):
152 text_image = self.font.render(item, True, c.NEAR_BLACK)
153 text_rect = text_image.get_rect(x=50, y=80+(i*50))
154 surface.blit(text_image, text_rect)
155
156 self.image = surface
157 self.rect = rect
158
159
160 def make_blank_info_box(self, title):
161 """Make an info box with title, otherwise blank"""
162 image = setup.GFX['playerstatsbox']
163 rect = image.get_rect(left=285, top=35)
164 centerx = rect.width / 2
165
166 surface = pg.Surface(rect.size)
167 surface.set_colorkey(c.BLACK)
168 surface.blit(image, (0,0))
169
170 title_image = self.title_font.render(title, True, c.NEAR_BLACK)
171 title_rect = title_image.get_rect(centerx=centerx, y=30)
172 surface.blit(title_image, title_rect)
173
174 return surface, rect
175
176
177 def update(self, state):
178 state_function = self.state_dict[state]
179 state_function()
180
181
182 def draw(self, surface):
183 """Draw to surface"""
184 surface.blit(self.image, self.rect)
185
186
187
188
189class SelectionBox(pg.sprite.Sprite):
190 def __init__(self):
191 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
192 self.image, self.rect = self.make_image()
193
194
195 def make_image(self):
196 choices = ['Stats', 'Items', 'Magic', 'Exit']
197 image = setup.GFX['selectionbox']
198 rect = image.get_rect(left=10, top=330)
199
200 surface = pg.Surface(rect.size)
201 surface.set_colorkey(c.BLACK)
202 surface.blit(image, (0, 0))
203
204 for i, choice in enumerate(choices):
205 choice_image = self.font.render(choice, True, c.NEAR_BLACK)
206 choice_rect = choice_image.get_rect(x=100, y=(25 + (i * 50)))
207 surface.blit(choice_image, choice_rect)
208
209 return surface, rect
210
211
212 def draw(self, surface):
213 """Draw to surface"""
214 surface.blit(self.image, self.rect)
215
216
217
218
219class MenuGui(object):
220 def __init__(self, level, inventory, stats):
221 self.level = level
222 self.inventory = inventory
223 self.stats = stats
224 self.info_box = InfoBox(inventory, stats)
225 self.gold_box = GoldBox(inventory)
226 self.selection_box = SelectionBox()
227 self.arrow = SmallArrow()
228 self.state_dict = self.make_state_dict()
229 self.arrow_index = 0
230 self.allow_input = False
231 self.state = 'stats'
232
233
234 def make_arrow_pos_list(self):
235 """Make the list of possible arrow positions"""
236 pos_list = []
237
238 for i in range(4):
239 pos = (35, 356 + (i * 50))
240 pos_list.append(pos)
241
242 return pos_list
243
244
245
246 def make_state_dict(self):
247 """Make the dictionary of all menu states"""
248 state_dict = {'stats': self.select_main_options,
249 'items': self.select_item,
250 'magic': self.select_item}
251
252 return state_dict
253
254
255 def select_main_options(self, keys):
256 """Allow player to select items, magic, weapons and armor"""
257 self.info_box.update(self.state)
258 self.arrow.update(self.arrow_index)
259 self.check_for_input(keys)
260
261
262 def check_for_input(self, keys):
263 """Check for input"""
264 if self.allow_input:
265 if keys[pg.K_DOWN]:
266 if self.arrow_index < 3:
267 self.arrow_index += 1
268 self.allow_input = False
269 elif keys[pg.K_UP]:
270 if self.arrow_index > 0:
271 self.arrow_index -= 1
272 self.allow_input = False
273 elif keys[pg.K_SPACE]:
274 if self.arrow_index == 0:
275 self.state = 'stats'
276
277 elif self.arrow_index == 1:
278 self.state = 'items'
279
280 elif self.arrow_index == 2:
281 self.state = 'magic'
282
283 elif self.arrow_index == 3:
284 self.level.state = 'normal'
285 self.arrow_index = 0
286 self.state = 'stats'
287
288
289 self.allow_input = False
290 elif keys[pg.K_RETURN]:
291 self.level.state = 'normal'
292 self.state = 'stats'
293 self.allow_input = False
294 self.arrow_index = 0
295
296 if (not keys[pg.K_DOWN]
297 and not keys[pg.K_UP]
298 and not keys[pg.K_RETURN]
299 and not keys[pg.K_SPACE]):
300 self.allow_input = True
301
302
303 def select_item(self, keys):
304 """Select item from list"""
305 self.info_box.update(self.state)
306 self.arrow.update(self.arrow_index)
307 self.check_for_input(keys)
308
309
310 def update(self, keys):
311 state_function = self.state_dict[self.state]
312 state_function(keys)
313
314
315 def draw(self, surface):
316 self.gold_box.draw(surface)
317 self.info_box.draw(surface)
318 self.selection_box.draw(surface)
319 self.arrow.draw(surface)