all repos — Legends-RPG @ 107c8c01760c5aab0de838393c6aceca60c3b1dc

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, info_box):
161        self.info_box = info_box
162        self.image = setup.GFX['smallarrow']
163        self.rect = self.image.get_rect()
164        self.state = 'select action'
165        self.state_dict = self.make_state_dict()
166        self.pos_list = self.make_select_action_pos_list()
167        self.index = 0
168        self.rect.topleft = self.pos_list[self.index]
169        self.allow_input = False
170        self.enemy_pos_list = enemy_pos_list
171
172    def make_state_dict(self):
173        """Make state dictionary"""
174        state_dict = {'select action': self.select_action,
175                      'select enemy': self.select_enemy,
176                      'select item': self.select_item}
177
178        return state_dict
179
180    def select_action(self, keys):
181        """
182        Select what action the player should take.
183        """
184        self.pos_list = self.make_select_action_pos_list()
185        self.rect.topleft = self.pos_list[self.index]
186
187        self.check_input(keys)
188
189    def make_select_action_pos_list(self):
190        """
191        Make the list of positions the arrow can be in.
192        """
193        pos_list = []
194
195        for i in range(4):
196            x = 590
197            y = (i * 34) + 472
198            pos_list.append((x, y))
199
200        return pos_list
201
202    def select_enemy(self, keys):
203        """
204        Select what enemy you want to take action on.
205        """
206        self.pos_list = self.enemy_pos_list
207
208        if self.pos_list:
209            pos = self.pos_list[self.index]
210            self.rect.x = pos[0] - 60
211            self.rect.y = pos[1] + 20
212
213        self.check_input(keys)
214
215    def check_input(self, keys):
216        if self.allow_input:
217            if keys[pg.K_DOWN] and self.index < (len(self.pos_list) - 1):
218                self.index += 1
219                self.allow_input = False
220            elif keys[pg.K_UP] and self.index > 0:
221                self.index -= 1
222                self.allow_input = False
223
224
225        if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False \
226                and keys[pg.K_RIGHT] == False and keys[pg.K_LEFT] == False:
227            self.allow_input = True
228
229    def select_item(self, keys):
230        """
231        Select item to use.
232        """
233        self.pos_list = self.make_select_item_pos_list()
234
235        pos = self.pos_list[self.index]
236        self.rect.x = pos[0] - 60
237        self.rect.y = pos[1] + 20
238
239        self.check_input(keys)
240
241    def make_select_item_pos_list(self):
242        """
243        Make the coordinates for the arrow for the item select screen.
244        """
245        pos_list = []
246        text_list = self.info_box.make_item_text()
247        text_list = text_list[1:]
248
249        for i in range(len(text_list)):
250            left = 90
251            top = (i * 29) + 488
252            pos_list.append((left, top))
253
254        return pos_list
255
256
257    def become_invisible_surface(self):
258        """
259        Make image attribute an invisible surface.
260        """
261        self.image = pg.Surface((32, 32))
262        self.image.set_colorkey(c.BLACK)
263
264    def become_select_item_state(self):
265        self.index = 0
266        self.state = c.SELECT_ITEM
267
268    def enter_select_action(self):
269        """
270        Assign values for the select action state.
271        """
272        pass
273
274    def enter_select_enemy(self):
275        """
276        Assign values for the select enemy state.
277        """
278        pass
279
280    def update(self, keys):
281        """
282        Update arrow position.
283        """
284        state_function = self.state_dict[self.state]
285        state_function(keys)
286
287    def draw(self, surface):
288        """
289        Draw to surface.
290        """
291        surface.blit(self.image, self.rect)
292
293    def remove_pos(self, enemy):
294        enemy_list = self.enemy_pos_list
295        enemy_pos = list(enemy.rect.topleft)
296
297        self.enemy_pos_list = [pos for pos in enemy_list if pos != enemy_pos]
298
299