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