all repos — Legends-RPG @ 7a753ab2f83fd4731001576934389ad9ff0e5e75

A fantasy mini-RPG built with Python and Pygame.

data/states/battle.py (view raw)

  1"""This is the state that handles battles against
  2monsters"""
  3
  4import pygame as pg
  5from .. import tools, setup
  6from .. components import person
  7from .. import constants as c
  8
  9
 10
 11
 12
 13class Battle(tools._State):
 14    def __init__(self):
 15        super(Battle, self).__init__()
 16
 17    def startup(self, current_time, game_data):
 18        """Initialize state attributes"""
 19        self.current_time = current_time
 20        self.game_data = game_data
 21        self.background = self.make_background()
 22        self.enemy_group = self.make_enemies()
 23        self.player_group = self.make_player()
 24        self.battle_info = BattleInfo()
 25        self.select_box = SelectBox()
 26        self.state_dict = self.make_state_dict()
 27        self.name = 'battle'
 28        self.next = game_data['last state']
 29
 30    def make_background(self):
 31        """Make the blue/black background"""
 32        background = pg.sprite.Sprite()
 33        surface = pg.Surface(c.SCREEN_SIZE).convert()
 34        surface.fill(c.BLACK_BLUE)
 35        background.image = surface
 36        background.rect = background.image.get_rect()
 37        background_group = pg.sprite.Group(background)
 38
 39        return background_group
 40
 41    def make_enemies(self):
 42        """Make the enemies for the battle. Return sprite group"""
 43        enemy = person.Person('devil', 100, 220, 'down', 'battle resting')
 44        enemy.image = pg.transform.scale2x(enemy.image)
 45        group = pg.sprite.Group(enemy)
 46
 47        return group
 48
 49    def make_player(self):
 50        """Make the sprite for the player's character"""
 51        player = person.Player('left', 630, 220, 'battle resting', 1)
 52        player.image = pg.transform.scale2x(player.image)
 53        player_group = pg.sprite.Group(player)
 54
 55        return player_group
 56
 57    def make_state_dict(self):
 58        """
 59        Make the dictionary of states the battle can be in.
 60        """
 61        state_dict = {'select action': self.select_action,
 62                      'select enemy': self.select_enemy,
 63                      'enemy attack': self.enemy_attack,
 64                      'player attack': self.player_attack,
 65                      'run away': self.run_away}
 66
 67        return state_dict
 68
 69    def select_action(self):
 70        """
 71        Select player action, of either attack, item, magic, run away.
 72        """
 73        pass
 74
 75    def select_enemy(self):
 76        """
 77        Select enemy you wish to attack.
 78        """
 79        pass
 80
 81    def enemy_attack(self):
 82        """
 83        Enemies, each in turn, attack the player.
 84        """
 85        pass
 86
 87    def player_attack(self):
 88        """
 89        Player attacks enemy
 90        """
 91        pass
 92
 93    def run_away(self):
 94        """
 95        Player attempts to run away.
 96        """
 97        pass
 98
 99    def update(self, surface, keys, current_time):
100        """Update the battle state"""
101        self.check_input(keys)
102        self.enemy_group.update(current_time)
103        self.player_group.update(keys, current_time)
104        self.select_box.update(keys)
105        self.draw_battle(surface)
106
107    def check_input(self, keys):
108        """
109        Check user input to navigate GUI.
110        """
111        if keys[pg.K_SPACE]:
112            self.game_data['last state'] = self.name
113            self.done = True
114
115    def draw_battle(self, surface):
116        """Draw all elements of battle state"""
117        self.background.draw(surface)
118        self.enemy_group.draw(surface)
119        self.player_group.draw(surface)
120        surface.blit(self.battle_info.image, self.battle_info.rect)
121        surface.blit(self.select_box.image, self.select_box.rect)
122
123
124class BattleInfo(object):
125    """
126    Info box that describes attack damage and other battle
127    related information.
128    """
129    def __init__(self):
130        self.image = setup.GFX['shopbox']
131        self.rect = self.image.get_rect(bottom=608)
132
133
134class SelectBox(object):
135    """
136    Box to select whether to attack, use item, use magic or run away.
137    """
138    def __init__(self):
139        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
140        self.slots = self.make_slots()
141        self.arrow = SelectArrow()
142        self.image = self.make_image()
143        self.rect = self.image.get_rect(bottom=608,
144                                        right=800)
145
146    def make_image(self):
147        """
148        Make the box image for
149        """
150        image = setup.GFX['goldbox']
151        rect = image.get_rect(bottom=608)
152        surface = pg.Surface(rect.size)
153        surface.set_colorkey(c.BLACK)
154        surface.blit(image, (0, 0))
155
156        for text in self.slots:
157            text_surface = self.font.render(text, True, c.NEAR_BLACK)
158            text_rect = text_surface.get_rect(x=self.slots[text]['x'],
159                                              y=self.slots[text]['y'])
160            surface.blit(text_surface, text_rect)
161
162        surface.blit(self.arrow.image, self.arrow.rect)
163
164        return surface
165
166    def make_slots(self):
167        """
168        Make the slots that hold the text selections, and locations.
169        """
170        slot_dict = {}
171        selections = ['Attack', 'Magic', 'Items', 'Run']
172
173        for i, text in enumerate(selections):
174            slot_dict[text] = {'x': 150,
175                               'y': (i*34)+10}
176
177        return slot_dict
178
179    def update(self, keys):
180        """Update components.
181        """
182        self.arrow.update(keys)
183        self.image = self.make_image()
184
185
186class SelectArrow(object):
187    """Small arrow for menu"""
188    def __init__(self):
189        self.image = setup.GFX['smallarrow']
190        self.rect = self.image.get_rect()
191        self.state = 'select action'
192        self.state_dict = self.make_state_dict()
193        self.pos_list = self.make_select_action_pos_list()
194        self.index = 0
195        self.rect.topleft = self.pos_list[self.index]
196        self.allow_input = False
197
198    def make_state_dict(self):
199        """Make state dictionary"""
200        state_dict = {'select action': self.select_action,
201                      'select enemy': self.select_enemy}
202
203        return state_dict
204
205    def select_action(self, keys):
206        """
207        Select what action the player should take.
208        """
209        self.pos_list = self.make_select_action_pos_list()
210        self.rect.topleft = self.pos_list[self.index]
211
212        if self.allow_input:
213            if keys[pg.K_DOWN] and self.index < (len(self.pos_list) - 1):
214                self.index += 1
215                self.allow_input = False
216            elif keys[pg.K_UP] and self.index > 0:
217                self.index -= 1
218                self.allow_input = False
219
220        if keys[pg.K_DOWN] == False and keys[pg.K_UP] == False:
221            self.allow_input = True
222
223
224    def make_select_action_pos_list(self):
225        """
226        Make the list of positions the arrow can be in.
227        """
228        pos_list = []
229
230        for i in range(4):
231            x = 50
232            y = (i * 34) + 12
233            pos_list.append((x, y))
234
235        return pos_list
236
237    def select_enemy(self):
238        """
239        Select what enemy you want to take action on.
240        """
241        pass
242
243    def enter_select_action(self):
244        """
245        Assign values for the select action state.
246        """
247        pass
248
249    def enter_select_enemy(self):
250        """
251        Assign values for the select enemy state.
252        """
253        pass
254
255    def update(self, keys):
256        """
257        Update arrow position.
258        """
259        state_function = self.state_dict[self.state]
260        state_function(keys)
261
262    def draw(self, surface):
263        """
264        Draw to surface.
265        """
266        surface.blit(self.image, self.rect)
267