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