-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattle.py
287 lines (245 loc) · 8.94 KB
/
battle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import pygame
# import fighter
import random
import button
pygame.init()
clock = pygame.time.Clock()
fps = 60
# game window
bottom_panel = 150
screen_width = 800
screen_height = 400 + bottom_panel
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Battle RPG")
# game variables
current_fighter = 1
total_fighters = 3
action_cooldown = 0
action_wait_time = 90
attack = False
potion = False
potion_effect = 15
clicked = False
# define fonts
font = pygame.font.SysFont('Times New Roman', 26)
# define colors
red = (255, 0, 0)
green = (0, 255, 0)
# load images
# - background image
background_img = pygame.image.load('img/Background/background.png').convert_alpha()
# - panel image
panel_img = pygame.image.load('img/Icons/panel.png').convert_alpha()
# - button images
potion_img = pygame.image.load('img/Icons/potion.png').convert_alpha()
# - sword image
sword_img = pygame.image.load('img/Icons/sword.png').convert_alpha()
# draw text on panel function
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
# draw background function
def draw_bg():
screen.blit(background_img, (0, 0))
# draw panel function
def draw_panel():
# draw panel background panel
screen.blit(panel_img, (0, screen_height - bottom_panel))
# show knight stats
draw_text(f'{knight.name} HP: {knight.hp}', font, red, 100, screen_height - bottom_panel + 10)
for count, i in enumerate(bandit_list):
# show name and health
draw_text(f'{i.name} HP: {i.hp}', font, red, 550, (screen_height - bottom_panel + 10) + count * 60)
# fighter class
class Fighter():
def __init__(self, x, y, name, max_hp, strength, potions):
self.name = name
self.max_hp = max_hp
self.hp = max_hp
self.strength = strength
self.start_potions = potions
self.potions = potions
self.alive = True
self.animation_list = []
self.frame_index = 0
self.action = 0 # 0: idle, 1: attack, 2: hurt, 3: death
self.update_time = pygame.time.get_ticks()
# load idle images
temp_list = []
for i in range(8):
img = pygame.image.load(f'img/{self.name}/idle/{i}.png')
img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
temp_list.append(img)
self.animation_list.append(temp_list)
# load attack images
temp_list = []
for i in range(8):
img = pygame.image.load(f'img/{self.name}/attack/{i}.png')
img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
temp_list.append(img)
self.animation_list.append(temp_list)
self.image = self.animation_list[self.action][self.frame_index]
self.rect = self.image.get_rect()
self.rect.center = (x, y)
def draw(self):
screen.blit(self.image, self.rect)
def update(self):
animation_cooldown = 100
# handle animation
# update image
self.image = self.animation_list[self.action][self.frame_index]
# check if enough time has passed since last update
if pygame.time.get_ticks() - self.update_time > animation_cooldown:
self.update_time = pygame.time.get_ticks()
self.frame_index += 1
# if animation runs out, reset back to start
if self.frame_index >= len(self.animation_list[self.action]):
self.idle()
def idle(self):
self.action = 0
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
def attack(self, target):
# deal damage to enemy
rand = random.randint(-5, 5)
damage = self.strength + rand
target.hp -= damage
# check if target has died
if target.hp < 1:
target.hp = 0
target.alive = False
damage_text = DamageText(target.rect.centerx, target.rect.y, str(damage), red)
damage_text_group.add(damage_text)
# set variables to attack animation
self.action = 1
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
class HealthBar():
def __init__(self, x, y, hp, max_hp):
self.x = x
self.y = y
self.hp = hp
self.max_hp = max_hp
def draw(self, hp):
self.hp = hp
ratio = self.hp / self.max_hp
# max hp
pygame.draw.rect(screen, red, (self.x, self.y, 150, 20))
# actual hp
pygame.draw.rect(screen, green, (self.x, self.y, ratio * 150, 20))
class DamageText(pygame.sprite.Sprite):
def __init__(self, x, y, damage, color):
pygame.sprite.Sprite.__init__(self)
self.image = font.render(damage, True, color)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
damage_text_group = pygame.sprite.Group()
knight = Fighter(200, 260, 'Knight', 30, 10, 3)
bandit1 = Fighter(550, 270, 'Bandit', 20, 6, 1)
bandit2 = Fighter(700, 270, 'Bandit', 20, 6, 1)
bandit_list = []
bandit_list.append(bandit1)
bandit_list.append(bandit2)
knight_health_bar = HealthBar(100, screen_height - bottom_panel + 40, knight.hp, knight.max_hp)
bandit1_health_bar = HealthBar(550, screen_height - bottom_panel + 40, bandit1.hp, bandit1.max_hp)
bandit2_health_bar = HealthBar(550, screen_height - bottom_panel + 100, bandit2.hp, bandit2.max_hp)
# create buttons
potion_button = button.Button(screen, 100, screen_height - bottom_panel + 70, potion_img, 64, 64)
# game run loop
run = True
while run:
clock.tick(fps)
# draw background & panel
draw_bg()
draw_panel()
knight_health_bar.draw(knight.hp)
bandit1_health_bar.draw(bandit1.hp)
bandit2_health_bar.draw(bandit2.hp)
# draw fighters
knight.update()
knight.draw()
for bandit in bandit_list:
bandit.update()
bandit.draw()
# draw damage text
damage_text_group.update()
damage_text_group.draw(screen)
# control player actions
# - reset action variables
attack = False
potion = False
target = None
# - make sure mouse visible
pygame.mouse.set_visible(True)
pos = pygame.mouse.get_pos()
for count, bandit in enumerate(bandit_list):
if bandit.rect.collidepoint(pos):
# hide mouse
pygame.mouse.set_visible(False)
# show sword
screen.blit(sword_img, pos)
if clicked == True:
attack = True
target = bandit_list[count]
if potion_button.draw():
potion = True
# show number of potions remaining
draw_text(str(knight.potions), font, red, 150, screen_height - bottom_panel + 70)
# player action
if knight.alive == True:
if current_fighter == 1:
action_cooldown += 1
if action_cooldown >= action_wait_time:
# look for player action
# attack
if attack == True and target != None:
knight.attack(target)
current_fighter += 1
action_cooldown = 0
# potion
if potion == True:
if knight.potions > 0:
# check if potion would heal player beyond max health
if knight.max_hp - knight.hp > potion_effect:
heal_amount = potion_effect
else:
heal_amount = knight.max_hp - knight.hp
knight.hp += heal_amount
knight.potions -= 1
current_fighter += 1
action_cooldown = 0
# enemy action
for count, bandit in enumerate(bandit_list):
if current_fighter == 2 + count:
if bandit.alive == True:
action_cooldown += 1
if action_cooldown >= action_wait_time:
# check if bandit requires healing
if (bandit.hp / bandit.max_hp) < 0.5 and bandit.potions > 0:
if bandit.max_hp - bandit.hp > potion_effect:
heal_amount = potion_effect
else:
heal_amount = bandit.max_hp - bandit.hp
bandit.hp += heal_amount
bandit.potions -= 1
current_fighter += 1
action_cooldown = 0
# attack
else:
bandit.attack(knight)
current_fighter += 1
action_cooldown = 0
else:
current_fighter += 1
# if all fighters had turn, reset
if current_fighter > total_fighters:
current_fighter = 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
clicked = True
else:
clicked = False
pygame.display.update()
pygame.quit()