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