all repos — Legends-RPG @ 83af8e19d1806b1256852088fd7dc4ecbfddca80

A fantasy mini-RPG built with Python and Pygame.

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, 110)
 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_text_sprites(self, text_list):
 64        """
 65        Make sprites out of text.
 66        """
 67        sprite_group = pg.sprite.Group()
 68
 69        for i, text in enumerate(text_list):
 70            sprite = pg.sprite.Sprite()
 71
 72            if i == 0:
 73                x = 195
 74                y = 10
 75                surface = self.title_font.render(text, True, c.NEAR_BLACK)
 76                rect = surface.get_rect(x=x, y=y)
 77            else:
 78                x = 100
 79                y = (i * 30) + 20
 80                surface = self.font.render(text, True, c.NEAR_BLACK)
 81                rect = surface.get_rect(x=x, y=y)
 82            sprite.image = surface
 83            sprite.rect = rect
 84            sprite_group.add(sprite)
 85
 86        return sprite_group
 87
 88    def make_image(self):
 89        """
 90        Make image out of box and message.
 91        """
 92        image = setup.GFX['shopbox']
 93        rect = image.get_rect(bottom=608)
 94        surface = pg.Surface(rect.size)
 95        surface.set_colorkey(c.BLACK)
 96        surface.blit(image, (0, 0))
 97
 98        if self.state == c.SELECT_ITEM:
 99            text_sprites = self.make_text_sprites(self.make_item_text())
100            text_sprites.draw(surface)
101        else:
102            text_surface = self.font.render(self.state_dict[self.state], True, c.NEAR_BLACK)
103            text_rect = text_surface.get_rect(x=50, y=50)
104            surface.blit(text_surface, text_rect)
105            if self.state == c.ENEMY_HIT or self.state == c.ENEMY_DEAD:
106                surface.blit(self.arrow.image, self.arrow.rect)
107
108        return surface
109
110    def update(self):
111        """Updates info box"""
112        self.image = self.make_image()
113
114
115class SelectBox(object):
116    """
117    Box to select whether to attack, use item, use magic or run away.
118    """
119    def __init__(self):
120        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
121        self.slots = self.make_slots()
122        self.image = self.make_image()
123        self.rect = self.image.get_rect(bottom=608,
124                                        right=800)
125
126    def make_image(self):
127        """
128        Make the box image for
129        """
130        image = setup.GFX['goldbox']
131        rect = image.get_rect(bottom=608)
132        surface = pg.Surface(rect.size)
133        surface.set_colorkey(c.BLACK)
134        surface.blit(image, (0, 0))
135
136        for text in self.slots:
137            text_surface = self.font.render(text, True, c.NEAR_BLACK)
138            text_rect = text_surface.get_rect(x=self.slots[text]['x'],
139                                              y=self.slots[text]['y'])
140            surface.blit(text_surface, text_rect)
141
142        return surface
143
144    def make_slots(self):
145        """
146        Make the slots that hold the text selections, and locations.
147        """
148        slot_dict = {}
149        selections = ['Attack', 'Items', 'Magic', 'Run']
150
151        for i, text in enumerate(selections):
152            slot_dict[text] = {'x': 150,
153                               'y': (i*34)+10}
154
155        return slot_dict
156
157
158class SelectArrow(object):
159    """Small arrow for menu"""
160    def __init__(self, enemy_pos_list):
161        self.image = setup.GFX['smallarrow']
162        self.rect = self.image.get_rect()
163        self.state = 'select action'
164        self.state_dict = self.make_state_dict()
165        self.pos_list = self.make_select_action_pos_list()
166        self.index = 0
167        self.rect.topleft = self.pos_list[self.index]
168        self.allow_input = False
169        self.enemy_pos_list = enemy_pos_list
170
171    def make_state_dict(self):
172        """Make state dictionary"""
173        state_dict = {'select action': self.select_action,
174                      'select enemy': self.select_enemy}
175
176        return state_dict
177
178    def select_action(self, keys):
179        """
180        Select what action the player should take.
181        """
182        self.pos_list = self.make_select_action_pos_list()
183        self.rect.topleft = self.pos_list[self.index]
184
185        if self.allow_input:
186            if keys[pg.K_DOWN] and self.index < (len(self.pos_list) - 1):
187                self.index += 1
188                self.allow_input = False
189            elif keys[pg.K_UP] and self.index > 0:
190                self.index -= 1
191                self.allow_input = False
192
193        if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False:
194            self.allow_input = True
195
196
197    def make_select_action_pos_list(self):
198        """
199        Make the list of positions the arrow can be in.
200        """
201        pos_list = []
202
203        for i in range(4):
204            x = 590
205            y = (i * 34) + 472
206            pos_list.append((x, y))
207
208        return pos_list
209
210    def select_enemy(self, keys):
211        """
212        Select what enemy you want to take action on.
213        """
214        self.pos_list = self.enemy_pos_list
215        pos = self.pos_list[self.index]
216        self.rect.x = pos[0] - 60
217        self.rect.y = pos[1] + 20
218
219        if self.allow_input:
220            if keys[pg.K_DOWN] and self.index < (len(self.pos_list) - 1):
221                self.index += 1
222                self.allow_input = False
223            elif keys[pg.K_UP] and self.index > 0:
224                self.index -= 1
225                self.allow_input = False
226
227
228        if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False \
229                and keys[pg.K_RIGHT] == False and keys[pg.K_LEFT] == False:
230            self.allow_input = True
231
232    def become_invisible_surface(self):
233        """
234        Make image attribute an invisible surface.
235        """
236        self.image = pg.Surface((32, 32))
237        self.image.set_colorkey(c.BLACK)
238
239
240    def enter_select_action(self):
241        """
242        Assign values for the select action state.
243        """
244        pass
245
246    def enter_select_enemy(self):
247        """
248        Assign values for the select enemy state.
249        """
250        pass
251
252    def update(self, keys):
253        """
254        Update arrow position.
255        """
256        state_function = self.state_dict[self.state]
257        state_function(keys)
258
259    def draw(self, surface):
260        """
261        Draw to surface.
262        """
263        surface.blit(self.image, self.rect)
264