all repos — Legends-RPG @ 9c32963e4fe38f8522d954c3697c4f05829b5a19

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 random
  5import pygame as pg
  6from .. import tools, battlegui, observer
  7from .. components import person, attack
  8from .. import constants as c
  9
 10
 11
 12class Battle(tools._State):
 13    def __init__(self):
 14        super(Battle, self).__init__()
 15
 16    def startup(self, current_time, game_data):
 17        """Initialize state attributes"""
 18        self.current_time = current_time
 19        self.allow_input = False
 20        self.allow_info_box_change = False
 21        self.game_data = game_data
 22        self.background = self.make_background()
 23        self.enemy_group, self.enemy_pos_list = self.make_enemies()
 24        self.player = self.make_player()
 25        self.attack_animations = pg.sprite.Group()
 26        self.info_box = battlegui.InfoBox(game_data)
 27        self.arrow = battlegui.SelectArrow(self.enemy_pos_list,
 28                                           self.info_box)
 29        self.select_box = battlegui.SelectBox()
 30        self.player_health = battlegui.PlayerHealth(self.select_box.rect,
 31                                                    self.game_data)
 32        self.attacked_enemy = None
 33        self.attacking_enemy = None
 34        self.state = c.SELECT_ACTION
 35        self.select_action_state_dict = self.make_selection_state_dict()
 36        self.name = 'battle'
 37        self.next = game_data['last state']
 38        self.observer = observer.Battle(self)
 39        self.player.observer = self.observer
 40
 41    def make_background(self):
 42        """Make the blue/black background"""
 43        background = pg.sprite.Sprite()
 44        surface = pg.Surface(c.SCREEN_SIZE).convert()
 45        surface.fill(c.BLACK_BLUE)
 46        background.image = surface
 47        background.rect = background.image.get_rect()
 48        background_group = pg.sprite.Group(background)
 49
 50        return background_group
 51
 52    def make_enemies(self):
 53        """Make the enemies for the battle. Return sprite group"""
 54        pos_list  = []
 55
 56        for column in range(3):
 57            for row in range(3):
 58                x = (column * 100) + 100
 59                y = (row * 100) + 100
 60                pos_list.append([x,y])
 61
 62        enemy_group = pg.sprite.Group()
 63
 64        for enemy in range(random.randint(1, 6)):
 65            enemy_group.add(person.Person('devil', 0, 0,
 66                                          'down', 'battle resting'))
 67
 68        for i, enemy in enumerate(enemy_group):
 69            enemy.rect.topleft = pos_list[i]
 70            enemy.image = pg.transform.scale2x(enemy.image)
 71
 72        return enemy_group, pos_list[0:len(enemy_group)]
 73
 74    def make_player(self):
 75        """Make the sprite for the player's character"""
 76        player = person.Player('left', 630, 220, 'battle resting', 1)
 77        player.image = pg.transform.scale2x(player.image)
 78
 79        return player
 80
 81    def make_selection_state_dict(self):
 82        """
 83        Make a dictionary of states with arrow coordinates as keys.
 84        """
 85        pos_list = self.arrow.make_select_action_pos_list()
 86        state_list = [c.SELECT_ENEMY, c.SELECT_ITEM, c.SELECT_MAGIC, c.RUN_AWAY]
 87        return dict(zip(pos_list, state_list))
 88
 89    def update(self, surface, keys, current_time):
 90        """Update the battle state"""
 91        self.check_input(keys)
 92        self.check_if_battle_won()
 93        self.enemy_group.update(current_time)
 94        self.player.update(keys, current_time)
 95        self.attack_animations.update()
 96        self.info_box.update()
 97        self.arrow.update(keys)
 98
 99        self.draw_battle(surface)
100
101    def check_input(self, keys):
102        """
103        Check user input to navigate GUI.
104        """
105        if self.allow_input:
106            if keys[pg.K_RETURN]:
107                self.notify(c.END_BATTLE)
108
109            elif keys[pg.K_SPACE]:
110                if self.state == c.SELECT_ACTION:
111                    self.state = self.select_action_state_dict[
112                        self.arrow.rect.topleft]
113                    self.notify(self.state)
114
115                elif self.state == c.SELECT_ENEMY:
116                    self.state = c.PLAYER_ATTACK
117                    self.notify(self.state)
118
119                elif self.state == c.PLAYER_ATTACK:
120                    self.state = c.ENEMY_DEAD
121                    self.info_box.state = c.ENEMY_DEAD
122
123                elif self.state == c.ENEMY_DEAD:
124                    self.state = c.SELECT_ACTION
125                    self.notify(self.state)
126
127                elif self.state == c.SELECT_ITEM or self.state == c.SELECT_MAGIC:
128                    if self.arrow.index == (len(self.arrow.pos_list) - 1):
129                        self.state = c.SELECT_ACTION
130                        self.notify(self.state)
131
132            self.allow_input = False
133
134        if keys[pg.K_RETURN] == False and keys[pg.K_SPACE] == False:
135            self.allow_input = True
136
137    def check_if_battle_won(self):
138        """
139        Check if state is SELECT_ACTION and there are no enemies left.
140        """
141        if self.state == c.SELECT_ACTION:
142            if len(self.enemy_group) == 0:
143                self.notify(c.BATTLE_WON)
144
145    def notify(self, event):
146        """
147        Notify observer of event.
148        """
149        self.observer.on_notify(event)
150
151    def end_battle(self):
152        """
153        End battle and flip back to previous state.
154        """
155        self.game_data['last state'] = self.name
156        self.done = True
157
158    def attack_enemy(self):
159        enemy = self.player.attacked_enemy
160
161        if enemy:
162            enemy.kill()
163            posx = enemy.rect.x - 32
164            posy = enemy.rect.y - 64
165            fire_sprite = attack.Fire(posx, posy)
166            self.attack_animations.add(fire_sprite)
167            self.player.attacked_enemy = None
168
169    def draw_battle(self, surface):
170        """Draw all elements of battle state"""
171        self.background.draw(surface)
172        self.enemy_group.draw(surface)
173        self.attack_animations.draw(surface)
174        surface.blit(self.player.image, self.player.rect)
175        surface.blit(self.info_box.image, self.info_box.rect)
176        surface.blit(self.select_box.image, self.select_box.rect)
177        surface.blit(self.arrow.image, self.arrow.rect)
178        self.player_health.draw(surface)
179
180