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 tools
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 = tools.get_tile
123 self.sword = self.get_tile(48, 0, setup.GFX['shopsigns'], 16, 16, 2)
124 self.shield = self.get_tile(32, 0, setup.GFX['shopsigns'], 16, 16, 2)
125 self.potion = self.get_tile(16, 0, setup.GFX['shopsigns'], 16, 16, 2)
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[0].upper() + stat[1:] + ": " +
156 str(self.player_stats[stat]['current']) +
157 " / " + str(self.player_stats[stat]['maximum']))
158 else:
159 text = stat + ": " + str(self.player_stats[stat])
160 text_image = self.font.render(text, True, c.NEAR_BLACK)
161 text_rect = text_image.get_rect(x=50, y=80+(i*50))
162 surface.blit(text_image, text_rect)
163
164 self.image = surface
165 self.rect = rect
166
167
168 def show_items(self):
169 """Show list of items the player has"""
170 title = 'ITEMS'
171 potions = ['POTIONS']
172 weapons = ['WEAPONS']
173 armor = ['ARMOR']
174 for i, item in enumerate(self.inventory):
175 if item in self.possible_weapons:
176 weapons.append(item)
177 elif item in self.possible_armor:
178 armor.append(item)
179 elif item in self.possible_potions:
180 potions.append(item)
181
182 self.assign_slots(weapons, armor, potions)
183
184 surface, rect = self.make_blank_info_box(title)
185
186 surface = self.blit_item_list(surface, weapons, 85)
187 surface = self.blit_item_list(surface, armor, 235)
188 surface = self.blit_item_list(surface, potions, 390)
189
190 self.sword['rect'].topleft = 50, 80
191 self.shield['rect'].topleft = 50, 230
192 self.potion['rect'].topleft = 50, 385
193 surface.blit(self.sword['surface'], self.sword['rect'])
194 surface.blit(self.shield['surface'], self.shield['rect'])
195 surface.blit(self.potion['surface'], self.potion['rect'])
196
197 self.image = surface
198 self.rect = rect
199
200
201 def assign_slots(self, weapons, armor, potions):
202 """Assign each item to a slot in the menu"""
203 if len(weapons) == 2:
204 self.slots[0] = weapons[1]
205 elif len(weapons) == 3:
206 self.slots[0] = weapons[1]
207 self.slots[1] = weapons[2]
208
209 if len(armor) == 2:
210 self.slots[2] = armor[1]
211 elif len(armor) == 3:
212 self.slots[2] = armor[1]
213 self.slots[3] = armor[2]
214
215 if len(potions) == 2:
216 self.slots[4] = potions[1]
217 elif len(potions) == 3:
218 self.slots[4] = potions[1]
219 self.slots[5] = potions[2]
220
221
222
223
224 def blit_item_list(self, surface, item_list, starty):
225 """Blit item list to info box surface"""
226 for i, item in enumerate(item_list):
227 if item in self.quantity_items:
228 text = item + ": " + str(self.inventory[item]['quantity'])
229 text_image = self.font.render(text, True, c.NEAR_BLACK)
230 text_rect = text_image.get_rect(x=90, y=starty+(i*50))
231 surface.blit(text_image, text_rect)
232 elif i == 0:
233 text = item
234 text_image = self.big_font.render(text, True, c.NEAR_BLACK)
235 text_rect = text_image.get_rect(x=90, y=starty)
236 surface.blit(text_image, text_rect)
237 else:
238 text = item
239 text_image = self.font.render(text, True, c.NEAR_BLACK)
240 text_rect = text_image.get_rect(x=90, y=starty+(i*50))
241 surface.blit(text_image, text_rect)
242
243 return surface
244
245
246 def show_magic(self):
247 """Show list of magic spells the player knows"""
248 title = 'MAGIC'
249 item_list = []
250 for item in self.inventory:
251 if item in self.possible_magic:
252 item_list.append(item)
253 item_list = sorted(item_list)
254
255 surface, rect = self.make_blank_info_box(title)
256
257 for i, item in enumerate(item_list):
258 text_image = self.font.render(item, True, c.NEAR_BLACK)
259 text_rect = text_image.get_rect(x=50, y=80+(i*50))
260 surface.blit(text_image, text_rect)
261
262 self.image = surface
263 self.rect = rect
264
265
266 def make_blank_info_box(self, title):
267 """Make an info box with title, otherwise blank"""
268 image = setup.GFX['playerstatsbox']
269 rect = image.get_rect(left=285, top=35)
270 centerx = rect.width / 2
271
272 surface = pg.Surface(rect.size)
273 surface.set_colorkey(c.BLACK)
274 surface.blit(image, (0,0))
275
276 title_image = self.title_font.render(title, True, c.NEAR_BLACK)
277 title_rect = title_image.get_rect(centerx=centerx, y=30)
278 surface.blit(title_image, title_rect)
279
280 return surface, rect
281
282
283 def update(self):
284 state_function = self.state_dict[self.state]
285 state_function()
286
287
288 def draw(self, surface):
289 """Draw to surface"""
290 surface.blit(self.image, self.rect)
291
292
293class SelectionBox(pg.sprite.Sprite):
294 def __init__(self):
295 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
296 self.image, self.rect = self.make_image()
297
298
299 def make_image(self):
300 choices = ['Stats', 'Items', 'Magic', 'Exit']
301 image = setup.GFX['selectionbox']
302 rect = image.get_rect(left=10, top=330)
303
304 surface = pg.Surface(rect.size)
305 surface.set_colorkey(c.BLACK)
306 surface.blit(image, (0, 0))
307
308 for i, choice in enumerate(choices):
309 choice_image = self.font.render(choice, True, c.NEAR_BLACK)
310 choice_rect = choice_image.get_rect(x=100, y=(25 + (i * 50)))
311 surface.blit(choice_image, choice_rect)
312
313 return surface, rect
314
315
316 def draw(self, surface):
317 """Draw to surface"""
318 surface.blit(self.image, self.rect)
319
320
321
322
323class MenuGui(object):
324 def __init__(self, level, inventory, stats):
325 self.level = level
326 self.inventory = inventory
327 self.stats = stats
328 self.info_box = InfoBox(inventory, stats)
329 self.gold_box = GoldBox(inventory)
330 self.selection_box = SelectionBox()
331 self.arrow = SmallArrow()
332 self.arrow_index = 0
333 self.allow_input = False
334 self.state = 'stats'
335
336
337 def check_for_input(self, keys):
338 """Check for input"""
339 if self.allow_input:
340 if keys[pg.K_DOWN]:
341 if self.arrow_index < len(self.arrow.pos_list) - 1:
342 self.arrow_index += 1
343 self.allow_input = False
344 elif keys[pg.K_UP]:
345 if self.arrow_index > 0:
346 self.arrow_index -= 1
347 self.allow_input = False
348 elif keys[pg.K_RIGHT]:
349 if self.info_box.state == 'items':
350 if not self.arrow.state == 'itemsubmenu':
351 self.arrow_index = 0
352 self.arrow.state = 'itemsubmenu'
353
354 elif keys[pg.K_LEFT]:
355 self.arrow.state = 'selectmenu'
356 self.arrow_index = 0
357 elif keys[pg.K_SPACE]:
358 if self.arrow.state == 'selectmenu':
359 if self.arrow_index == 0:
360 self.info_box.state = 'stats'
361
362 elif self.arrow_index == 1:
363 self.info_box.state = 'items'
364
365 elif self.arrow_index == 2:
366 self.info_box.state = 'magic'
367
368 elif self.arrow_index == 3:
369 self.level.state = 'normal'
370 self.arrow_index = 0
371 self.info_box.state = 'stats'
372
373
374 self.allow_input = False
375 elif keys[pg.K_RETURN]:
376 self.level.state = 'normal'
377 self.info_box.state = 'stats'
378 self.allow_input = False
379 self.arrow_index = 0
380
381 if (not keys[pg.K_DOWN]
382 and not keys[pg.K_UP]
383 and not keys[pg.K_RETURN]
384 and not keys[pg.K_SPACE]):
385 self.allow_input = True
386
387
388 def update(self, keys):
389 self.info_box.update()
390 self.gold_box.update()
391 self.arrow.update(self.arrow_index)
392 self.check_for_input(keys)
393
394
395 def draw(self, surface):
396 self.gold_box.draw(surface)
397 self.info_box.draw(surface)
398 self.selection_box.draw(surface)
399 self.arrow.draw(surface)