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