all repos — Legends-RPG @ 386aef55098a4fc99b56e6fa1223d98bb02761f1

A fantasy mini-RPG built with Python and Pygame.

data/battlegui.py (view raw)

  1"""
  2GUI components for battle states.
  3"""
  4import sys
  5import pygame as pg
  6from . import setup, observer
  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        self.observers = [observer.SoundEffects()]
249
250    def notify(self, event):
251        """
252        Notify all observers of events.
253        """
254        for observer in self.observers:
255            observer.on_notify(event)
256
257    def make_state_dict(self):
258        """Make state dictionary"""
259        state_dict = {'select action': self.select_action,
260                      'select enemy': self.select_enemy,
261                      'select item': self.select_item,
262                      'select magic': self.select_magic,
263                      'invisible': self.become_invisible_surface}
264
265        return state_dict
266
267    def select_action(self, keys):
268        """
269        Select what action the player should take.
270        """
271        self.pos_list = self.make_select_action_pos_list()
272        if self.index > (len(self.pos_list) - 1):
273            print self.pos_list, self.index
274        self.rect.topleft = self.pos_list[self.index]
275
276        self.check_input(keys)
277
278    def make_select_action_pos_list(self):
279        """
280        Make the list of positions the arrow can be in.
281        """
282        pos_list = []
283
284        for i in range(4):
285            x = 590
286            y = (i * 34) + 472
287            pos_list.append((x, y))
288
289        return pos_list
290
291    def select_enemy(self, keys):
292        """
293        Select what enemy you want to take action on.
294        """
295        self.pos_list = self.enemy_pos_list
296
297        if self.pos_list:
298            pos = self.pos_list[self.index]
299            self.rect.x = pos[0] - 60
300            self.rect.y = pos[1] + 20
301
302        self.check_input(keys)
303
304    def check_input(self, keys):
305        if self.allow_input:
306            if keys[pg.K_DOWN] and self.index < (len(self.pos_list) - 1):
307                self.notify(c.CLICK)
308                self.index += 1
309                self.allow_input = False
310            elif keys[pg.K_UP] and self.index > 0:
311                self.notify(c.CLICK)
312                self.index -= 1
313                self.allow_input = False
314
315
316        if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False \
317                and keys[pg.K_RIGHT] == False and keys[pg.K_LEFT] == False:
318            self.allow_input = True
319
320    def select_item(self, keys):
321        """
322        Select item to use.
323        """
324        self.pos_list = self.make_select_item_pos_list()
325
326        pos = self.pos_list[self.index]
327        self.rect.x = pos[0] - 60
328        self.rect.y = pos[1] + 20
329
330        self.check_input(keys)
331
332    def make_select_item_pos_list(self):
333        """
334        Make the coordinates for the arrow for the item select screen.
335        """
336        pos_list = []
337        text_list = self.info_box.make_item_text()
338        text_list = text_list[1:]
339
340        for i in range(len(text_list)):
341            left = 90
342            top = (i * 29) + 488
343            pos_list.append((left, top))
344
345        return pos_list
346
347    def select_magic(self, keys):
348        """
349        Select magic to use.
350        """
351        self.pos_list = self.make_select_magic_pos_list()
352
353        pos = self.pos_list[self.index]
354        self.rect.x = pos[0] - 60
355        self.rect.y = pos[1] + 20
356
357        self.check_input(keys)
358
359    def make_select_magic_pos_list(self):
360        """
361        Make the coordinates for the arrow for the magic select screen.
362        """
363        pos_list = []
364        text_list = self.info_box.make_magic_text()
365        text_list = text_list[1:]
366
367        for i in range(len(text_list)):
368            left = 90
369            top = (i * 29) + 488
370            pos_list.append((left, top))
371
372        return pos_list
373
374
375    def become_invisible_surface(self, *args):
376        """
377        Make image attribute an invisible surface.
378        """
379        self.image = pg.Surface(self.rect.size)
380        self.image.set_colorkey(c.BLACK)
381
382    def become_select_item_state(self):
383        self.index = 0
384        self.state = c.SELECT_ITEM
385
386    def become_select_magic_state(self):
387        self.index = 0
388        self.state = c.SELECT_MAGIC
389
390    def enter_select_action(self):
391        """
392        Assign values for the select action state.
393        """
394        pass
395
396    def enter_select_enemy(self):
397        """
398        Assign values for the select enemy state.
399        """
400        pass
401
402    def update(self, keys):
403        """
404        Update arrow position.
405        """
406        self.image = setup.GFX['smallarrow']
407        state_function = self.state_dict[self.state]
408        state_function(keys)
409
410    def draw(self, surface):
411        """
412        Draw to surface.
413        """
414        surface.blit(self.image, self.rect)
415
416    def remove_pos(self, enemy):
417        enemy_list = self.enemy_pos_list
418        enemy_pos = list(enemy.rect.topleft)
419
420        self.enemy_pos_list = [pos for pos in enemy_list if pos != enemy_pos]
421
422
423class PlayerHealth(object):
424    """
425    Basic health meter for player.
426    """
427    def __init__(self, select_box_rect, game_data):
428        self.health_stats = game_data['player stats']['health']
429        self.magic_stats = game_data['player stats']['magic']
430        self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
431        self.posx = select_box_rect.centerx
432        self.posy = select_box_rect.y - 5
433
434    @property
435    def image(self):
436        """
437        Make the image surface for the player
438        """
439        current_health = str(self.health_stats['current'])
440        max_health = str(self.health_stats['maximum'])
441        if len(current_health) == 2:
442            buffer = '  '
443        elif len(current_health) == 1:
444            buffer = '    '
445        else:
446            buffer = ''
447        health_string = "Health: {}{}/{}".format(buffer, current_health, max_health)
448        health_surface =  self.title_font.render(health_string, True, c.NEAR_BLACK)
449        health_rect = health_surface.get_rect(x=20, y=9)
450
451        current_magic = str(self.magic_stats['current'])
452        if len(current_magic) == 2:
453            buffer = '  '
454        elif len(current_magic) == 1:
455            buffer = '    '
456        else:
457            buffer = ''
458        max_magic = str(self.magic_stats['maximum'])
459        magic_string = "Magic:  {}{}/{}".format(buffer, current_magic, max_magic)
460        magic_surface = self.title_font.render(magic_string, True, c.NEAR_BLACK)
461        magic_rect = magic_surface.get_rect(x=20, top=health_rect.bottom)
462
463        box_surface = setup.GFX['battlestatbox']
464        box_rect = box_surface.get_rect()
465
466        parent_surface = pg.Surface(box_rect.size)
467        parent_surface.blit(box_surface, box_rect)
468        parent_surface.blit(health_surface, health_rect)
469        parent_surface.blit(magic_surface, magic_rect)
470
471        return parent_surface
472
473    @property
474    def rect(self):
475        """
476        Make the rect object for image surface.
477        """
478        return self.image.get_rect(centerx=self.posx, bottom=self.posy)
479
480    def draw(self, surface):
481        """
482        Draw health to surface.
483        """
484        surface.blit(self.image, self.rect)