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