data/battlegui.py (view raw)
1"""
2GUI components for battle states.
3"""
4import pygame as pg
5from . import setup
6from . import constants as c
7from .components import textbox
8
9
10class InfoBox(object):
11 """
12 Info box that describes attack damage and other battle
13 related information.
14 """
15 def __init__(self, game_data):
16 self.game_data = game_data
17 self.state = c.SELECT_ACTION
18 self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
19 self.title_font.set_underline(True)
20 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 18)
21 self.state_dict = self.make_state_dict()
22 self.image = self.make_image()
23 self.rect = self.image.get_rect(bottom=608)
24 self.arrow = textbox.NextArrow()
25 self.arrow.rect.topleft = (380, 55)
26
27
28 def make_state_dict(self):
29 """
30 Make dictionary of states Battle info can be in.
31 """
32 state_dict = {c.SELECT_ACTION: 'Select an action.',
33 c.SELECT_MAGIC: 'Select a magic spell.',
34 c.SELECT_ITEM: 'Select an item.',
35 c.SELECT_ENEMY: 'Select an enemy.',
36 c.ENEMY_ATTACK: 'The enemy attacks player!',
37 c.PLAYER_ATTACK: 'Player attacks enemy.',
38 c.RUN_AWAY: 'Run away',
39 c.ENEMY_HIT: 'Enemy hit with 20 damage.',
40 c.ENEMY_DEAD: 'Enemy killed.'}
41
42 return state_dict
43
44
45 def make_item_text(self):
46 """
47 Make the text for when the player selects items.
48 """
49 inventory = self.game_data['player inventory']
50 allowed_item_list = ['Healing Potion']
51 title = 'SELECT ITEM'
52 item_text_list = [title]
53
54 for item in inventory:
55 if item in allowed_item_list:
56 text = item + ": " + str(inventory[item]['quantity'])
57 item_text_list.append(text)
58
59 item_text_list.append('BACK')
60
61 return item_text_list
62
63 def make_magic_text(self):
64 """
65 Make the text for when the player selects magic.
66 """
67 inventory = self.game_data['player inventory']
68 allowed_item_list = ['Fire Blast', 'Cure']
69 title = 'SELECT MAGIC SPELL'
70 magic_text_list = [title]
71 spell_list = [item for item in inventory if item in allowed_item_list]
72 magic_text_list.extend(spell_list)
73 magic_text_list.append('BACK')
74
75 return magic_text_list
76
77 def make_text_sprites(self, text_list):
78 """
79 Make sprites out of text.
80 """
81 sprite_group = pg.sprite.Group()
82
83 for i, text in enumerate(text_list):
84 sprite = pg.sprite.Sprite()
85
86 if i == 0:
87 x = 195
88 y = 10
89 surface = self.title_font.render(text, True, c.NEAR_BLACK)
90 rect = surface.get_rect(x=x, y=y)
91 else:
92 x = 100
93 y = (i * 30) + 20
94 surface = self.font.render(text, True, c.NEAR_BLACK)
95 rect = surface.get_rect(x=x, y=y)
96 sprite.image = surface
97 sprite.rect = rect
98 sprite_group.add(sprite)
99
100 return sprite_group
101
102 def make_image(self):
103 """
104 Make image out of box and message.
105 """
106 image = setup.GFX['shopbox']
107 rect = image.get_rect(bottom=608)
108 surface = pg.Surface(rect.size)
109 surface.set_colorkey(c.BLACK)
110 surface.blit(image, (0, 0))
111
112 if self.state == c.SELECT_ITEM:
113 text_sprites = self.make_text_sprites(self.make_item_text())
114 text_sprites.draw(surface)
115 elif self.state == c.SELECT_MAGIC:
116 text_sprites = self.make_text_sprites(self.make_magic_text())
117 text_sprites.draw(surface)
118 else:
119 text_surface = self.font.render(self.state_dict[self.state], True, c.NEAR_BLACK)
120 text_rect = text_surface.get_rect(x=50, y=50)
121 surface.blit(text_surface, text_rect)
122 if self.state == c.ENEMY_HIT or self.state == c.ENEMY_DEAD:
123 surface.blit(self.arrow.image, self.arrow.rect)
124
125 return surface
126
127 def update(self):
128 """Updates info box"""
129 self.image = self.make_image()
130
131
132class SelectBox(object):
133 """
134 Box to select whether to attack, use item, use magic or run away.
135 """
136 def __init__(self):
137 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
138 self.slots = self.make_slots()
139 self.image = self.make_image()
140 self.rect = self.image.get_rect(bottom=608,
141 right=800)
142
143 def make_image(self):
144 """
145 Make the box image for
146 """
147 image = setup.GFX['goldbox']
148 rect = image.get_rect(bottom=608)
149 surface = pg.Surface(rect.size)
150 surface.set_colorkey(c.BLACK)
151 surface.blit(image, (0, 0))
152
153 for text in self.slots:
154 text_surface = self.font.render(text, True, c.NEAR_BLACK)
155 text_rect = text_surface.get_rect(x=self.slots[text]['x'],
156 y=self.slots[text]['y'])
157 surface.blit(text_surface, text_rect)
158
159 return surface
160
161 def make_slots(self):
162 """
163 Make the slots that hold the text selections, and locations.
164 """
165 slot_dict = {}
166 selections = ['Attack', 'Items', 'Magic', 'Run']
167
168 for i, text in enumerate(selections):
169 slot_dict[text] = {'x': 150,
170 'y': (i*34)+10}
171
172 return slot_dict
173
174
175class SelectArrow(object):
176 """Small arrow for menu"""
177 def __init__(self, enemy_pos_list, info_box):
178 self.info_box = info_box
179 self.image = setup.GFX['smallarrow']
180 self.rect = self.image.get_rect()
181 self.state = 'select action'
182 self.state_dict = self.make_state_dict()
183 self.pos_list = self.make_select_action_pos_list()
184 self.index = 0
185 self.rect.topleft = self.pos_list[self.index]
186 self.allow_input = False
187 self.enemy_pos_list = enemy_pos_list
188
189 def make_state_dict(self):
190 """Make state dictionary"""
191 state_dict = {'select action': self.select_action,
192 'select enemy': self.select_enemy,
193 'select item': self.select_item,
194 'select magic': self.select_magic}
195
196 return state_dict
197
198 def select_action(self, keys):
199 """
200 Select what action the player should take.
201 """
202 self.pos_list = self.make_select_action_pos_list()
203 self.rect.topleft = self.pos_list[self.index]
204
205 self.check_input(keys)
206
207 def make_select_action_pos_list(self):
208 """
209 Make the list of positions the arrow can be in.
210 """
211 pos_list = []
212
213 for i in range(4):
214 x = 590
215 y = (i * 34) + 472
216 pos_list.append((x, y))
217
218 return pos_list
219
220 def select_enemy(self, keys):
221 """
222 Select what enemy you want to take action on.
223 """
224 self.pos_list = self.enemy_pos_list
225
226 if self.pos_list:
227 pos = self.pos_list[self.index]
228 self.rect.x = pos[0] - 60
229 self.rect.y = pos[1] + 20
230
231 self.check_input(keys)
232
233 def check_input(self, keys):
234 if self.allow_input:
235 if keys[pg.K_DOWN] and self.index < (len(self.pos_list) - 1):
236 self.index += 1
237 self.allow_input = False
238 elif keys[pg.K_UP] and self.index > 0:
239 self.index -= 1
240 self.allow_input = False
241
242
243 if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False \
244 and keys[pg.K_RIGHT] == False and keys[pg.K_LEFT] == False:
245 self.allow_input = True
246
247 def select_item(self, keys):
248 """
249 Select item to use.
250 """
251 self.pos_list = self.make_select_item_pos_list()
252
253 pos = self.pos_list[self.index]
254 self.rect.x = pos[0] - 60
255 self.rect.y = pos[1] + 20
256
257 self.check_input(keys)
258
259 def make_select_item_pos_list(self):
260 """
261 Make the coordinates for the arrow for the item select screen.
262 """
263 pos_list = []
264 text_list = self.info_box.make_item_text()
265 text_list = text_list[1:]
266
267 for i in range(len(text_list)):
268 left = 90
269 top = (i * 29) + 488
270 pos_list.append((left, top))
271
272 return pos_list
273
274 def select_magic(self, keys):
275 """
276 Select magic to use.
277 """
278 self.pos_list = self.make_select_magic_pos_list()
279
280 pos = self.pos_list[self.index]
281 self.rect.x = pos[0] - 60
282 self.rect.y = pos[1] + 20
283
284 self.check_input(keys)
285
286 def make_select_magic_pos_list(self):
287 """
288 Make the coordinates for the arrow for the magic select screen.
289 """
290 pos_list = []
291 text_list = self.info_box.make_magic_text()
292 text_list = text_list[1:]
293
294 for i in range(len(text_list)):
295 left = 90
296 top = (i * 29) + 488
297 pos_list.append((left, top))
298
299 return pos_list
300
301
302 def become_invisible_surface(self):
303 """
304 Make image attribute an invisible surface.
305 """
306 self.image = pg.Surface((32, 32))
307 self.image.set_colorkey(c.BLACK)
308
309 def become_select_item_state(self):
310 self.index = 0
311 self.state = c.SELECT_ITEM
312
313 def become_select_magic_state(self):
314 self.index = 0
315 self.state = c.SELECT_MAGIC
316
317 def enter_select_action(self):
318 """
319 Assign values for the select action state.
320 """
321 pass
322
323 def enter_select_enemy(self):
324 """
325 Assign values for the select enemy state.
326 """
327 pass
328
329 def update(self, keys):
330 """
331 Update arrow position.
332 """
333 state_function = self.state_dict[self.state]
334 state_function(keys)
335
336 def draw(self, surface):
337 """
338 Draw to surface.
339 """
340 surface.blit(self.image, self.rect)
341
342 def remove_pos(self, enemy):
343 enemy_list = self.enemy_pos_list
344 enemy_pos = list(enemy.rect.topleft)
345
346 self.enemy_pos_list = [pos for pos in enemy_list if pos != enemy_pos]
347
348
349class PlayerHealth(object):
350 """
351 Basic health meter for player.
352 """
353 def __init__(self, select_box_rect, game_data):
354 self.health_stats = game_data['player stats']['Health']
355 self.magic_stats = game_data['player stats']['Magic Points']
356 self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
357 self.posx = select_box_rect.centerx
358 self.posy = select_box_rect.y - 5
359
360 @property
361 def image(self):
362 """
363 Make the image surface for the player
364 """
365 current_health = str(self.health_stats['current'])
366 max_health = str(self.health_stats['maximum'])
367 health_string = "Health: " + current_health + "/" + max_health
368 health_surface = self.title_font.render(health_string, True, c.NEAR_BLACK)
369 health_rect = health_surface.get_rect(x=20, y=9)
370
371 current_magic = str(self.magic_stats['current'])
372 max_magic = str(self.magic_stats['maximum'])
373 magic_string = "Magic: " + current_magic + "/" + max_magic
374 magic_surface = self.title_font.render(magic_string, True, c.NEAR_BLACK)
375 magic_rect = magic_surface.get_rect(x=20, top=health_rect.bottom)
376
377 box_surface = setup.GFX['battlestatbox']
378 box_rect = box_surface.get_rect()
379
380 parent_surface = pg.Surface(box_rect.size)
381 parent_surface.blit(box_surface, box_rect)
382 parent_surface.blit(health_surface, health_rect)
383 parent_surface.blit(magic_surface, magic_rect)
384
385 return parent_surface
386
387 @property
388 def rect(self):
389 """
390 Make the rect object for image surface.
391 """
392 return self.image.get_rect(centerx=self.posx, bottom=self.posy)
393
394 def draw(self, surface):
395 """
396 Draw health to surface.
397 """
398 surface.blit(self.image, self.rect)