-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforest_fight.py
331 lines (305 loc) · 11 KB
/
forest_fight.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import pygame
import sys
from random import choice
from charclasses import Player, Enemy, Spritesheet
from buttonclass import Button
#------Initializing
pygame.init()
DISPLAY_WIDTH = 512
DISPLAY_HEIGHT = 480
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption('FOREST FIGHT !')
FONT = pygame.font.SysFont('Courier', 20)
fps = pygame.time.Clock()
mouse = pygame.mouse.get_pos()
#------Starting game attributes
turn_counter = []
playerhp = 100
playeratk = 20
stats = [playerhp, playeratk]
#------COLORS
BACKGROUND = (40, 36, 15)
BLACK = (0,0,0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
#------Game loops
start_menu = True
game_run = False
end_screen = False
#---------------------#GAME FUNCTIONS#-------------------------------------------#
#------takes in sprites and doubles size for display
def upres(image):
load_in = pygame.image.load(image)
fullsize = pygame.transform.scale(load_in, (load_in.get_width()*2 , load_in.get_height()*2))
return fullsize
#------all displayed text in the game
def text_gen(text, color, x, y):
phrase = FONT.render(text, False, color)
screen.blit(phrase, (x, y))
#------background setup
def set_background():
screen.blit(forest, (0,0))
screen.blit(menu, (0, 320))
#------combat functions
def enemy_turn():
monster.status = 'attack'
monster.frame_index = 6
monster.update()
hero.status = 'hurt'
hero.frame_index = 18
hero.update()
damagecalc_e = monster.attack(hero)
pygame.mixer.Sound.play(enemyattack)
return damagecalc_e
def attack_turn():
hero.status = 'attack'
hero.frame_index = 6
hero.update()
damagecalc_p = hero.attack(monster)
monster.status = 'hurt'
monster.frame_index = 12
monster.update()
pygame.mixer.Sound.play(sword)
return damagecalc_p
def heal_turn():
pygame.mixer.Sound.play(healsound)
hero.status = 'heal'
hero.frame_index = 12
init_health = hero.health
hero.heal()
amount = hero.health-init_health
hero.update()
return amount
def magic_turn():
pygame.mixer.Sound.play(fire)
hero.status = 'magic'
hero.frame_index = 22
hero.update()
damagecalc_m = hero.magic(monster)
monster.status = 'hurt'
monster.frame_index = 12
monster.update()
return damagecalc_m
#------end of round win/lose
def game_end():
if hero.check_alive():
return 'win'
if not hero.check_alive():
return 'lose'
#---------------------#VISUAL ASSETS#--------------------------------------------#
#------Sprite Setup#
start = upres('sprites\\start.png')
forest = upres('sprites\\forest_bg.png')
menu = upres('sprites\\mainmenu.png')
win_card = upres('sprites\\winframe.png')
lose_card = upres('sprites\\loseframe.png')
#------buttons
st_btn = Button('sprites\\startbutton.png',
'sprites\\startbutton_h.png',
'sprites\\startbutton_p.png', 210, 300)
qt_btn = Button('sprites\\quitbutton.png',
'sprites\\quitbutton_h.png',
'sprites\\quitbutton_p.png', 210, 340)
yes = Button('sprites\\yesbutton.png',
'sprites\\yesbutton_h.png',
'sprites\\yesbutton_p.png', 140, 150)
no = Button('sprites\\nobutton.png',
'sprites\\nobutton_h.png',
'sprites\\nobutton_p.png', 260, 150)
atk_btn = Button('sprites\\atkbutton.png',
'sprites\\atkbutton_h.png',
'sprites\\atkbutton_p.png', 50, 385)
hl_btn = Button('sprites\\healbutton.png',
'sprites\\healbutton_h.png',
'sprites\\healbutton_p.png', 50, 410)
mg_btn = Button('sprites\\magicbutton.png',
'sprites\\magicbutton_h.png',
'sprites\\magicbutton_p.png', 50, 435)
#------heal effect prep
healanim = []
animation_steps = 6
last_update = pygame.time.get_ticks()
frame_duration = 100
healframe = 0
heal = upres('sprites\\hero\\magic\\heal.png')
healsheet = Spritesheet(heal)
for x in range(7):
healanim.append(healsheet.get_sprite(x, 32, 32, 1))
#-------------------------------#AUDIO#-----------------------------------------#
#------Music
pygame.mixer.init()
pygame.mixer.music.load('sound\\song.mid')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
#------SFX
sword = pygame.mixer.Sound('sound\\sword.mp3')
healsound = pygame.mixer.Sound('sound\\heal.mp3')
fire = pygame.mixer.Sound('sound\\fire.mp3')
bite = pygame.mixer.Sound('sound\\bite.mp3')
ghostyell = pygame.mixer.Sound('sound\\yell.mp3')
punch = pygame.mixer.Sound('sound\\punch.mp3')
winsound = pygame.mixer.Sound('sound\\win.mp3')
losesound = pygame.mixer.Sound('sound\\lose.mp3')
#---------------------------#GAME LOOP#---------------------------------------#
while True:
fps.tick(60)
screen.fill(BACKGROUND)
#keep track of rounds
turn_counter.append('x')
#increase player stats for holding more wins
if turn_counter.count('w') > 0:
upgrade = choice(stats)
if upgrade == stats[0]:
playerhp+=10
else:
playeratk+=1
#character setup and placements
hero = Player('hero', 75, 75, 235, playerhp, playeratk)
enemy_types = {
'ghost' : [120, 15, ghostyell],
'hand' : [80, 26, punch],
'dog' : [170, 13, bite],
}
enemy = choice(list(enemy_types))
enemyhp = enemy_types[enemy][0]
enemyatk = enemy_types[enemy][1]
enemyattack = enemy_types[enemy][2]
monster = Enemy(f'{enemy}', 365, 365, 235, enemyhp, enemyatk)
#set turn-based combat variables
player_turn = 'Player'
loop = 0
#------START MENU
while start_menu:
fps.tick(60)
screen.blit(start, (0,0))
if st_btn.display(screen):
start_menu = False
game_run = True
if qt_btn.display(screen):
start_menu = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
start_menu = False
pygame.display.update()
#------CORE GAME LOOP
while game_run:
fps.tick(60)
#set background images
set_background()
#MENU - PLAYER
text_gen(f'HERO', WHITE, 70, 340)
text_gen(f'HP: {hero.health}', WHITE, 60, 360)
#MENU - ENEMY
text_gen(f'MONSTER', WHITE, 350, 340)
text_gen(f'HP: {monster.health}', WHITE, 340, 360)
#display single sprite frame in designated location
hero.update()
hero.display(screen)
monster.update()
monster.display(screen)
#player turn
if player_turn == 'Player':
text_gen(f'Your turn:', WHITE, 185, 338)
if hero.frame_index >= 0 and hero.frame_index <=5:
if atk_btn.display(screen,'Attack with your sword!',180,395):
dealt = attack_turn()
loop+=1
if hl_btn.display(screen,'Concentrate and heal 5-25 HP',180,395):
healed = heal_turn()
loop +=1
if mg_btn.display(screen,'Could do a little or a lot',180,395):
magic_dealt = magic_turn()
loop +=1
if loop > 0:
if hero.frame_index < 12 and hero.frame_index > 5:
text_gen(f'You attack!', WHITE, 170, 390)
text_gen(f'Enemy takes {dealt} damage.', WHITE, 130, 420)
if hero.frame_index > 11 and hero.frame_index <= 17:
text_gen(f'Healed for {healed}.', WHITE, 170, 405)
if hero.frame_index <= 27 and hero.frame_index >= 22:
text_gen(f'You cast fire!', WHITE, 160, 390)
text_gen(f'Enemy takes {magic_dealt} damage.', WHITE, 130, 420)
if hero.frame_index == 11 or hero.frame_index == 17 or hero.frame_index == 27:
last_turn = player_turn
if monster.check_alive():
player_turn = 'wait'
else:
player_turn = 'game_over'
#enemy turn
if player_turn == 'Enemy':
if monster.frame_index == 5:
loop+=1
if loop < 2:
text_gen(f'Enemy\'s turn!', WHITE, 165, 338)
if loop == 3:
damage = enemy_turn()
loop+=1
if loop > 3:
if monster.frame_index <= 11 and monster.frame_index > 5:
text_gen(f'Enemy attacks you!', WHITE, 150, 390)
text_gen(f'You take {damage} damage.', WHITE, 140, 420) #AMOUNT
if monster.frame_index == 11:
last_turn = player_turn
if hero.check_alive():
player_turn = 'wait'
else:
player_turn = 'game_over'
#wait to alternate turns
if player_turn == 'wait':
if hero.status == 'idle' and monster.status == 'idle' and last_turn == 'Player':
player_turn = 'Enemy'
if hero.status == 'idle' and monster.status == 'idle' and last_turn == 'Enemy':
loop = 0
player_turn = 'Player'
#round ends
if player_turn == 'game_over':
if not hero.check_alive():
pygame.mixer.Sound.play(losesound, 0)
game_end()
end_screen = True
game_run = False
elif not monster.check_alive():
pygame.mixer.Sound.play(winsound, 0)
game_end()
end_screen = True
game_run = False
# show heal fx
if hero.frame_index >= 12 and hero.frame_index <= 17:
heal_time = pygame.time.get_ticks()
if heal_time - last_update >= frame_duration:
healframe += 1
last_update = heal_time
if healframe >= len(healanim):
healframe = 0
screen.blit(healanim[healframe], (80,240))
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_run = False
pygame.display.update()
#------END SCREEN
while end_screen:
fps.tick(60)
if game_end() == 'win':
turn_counter[-1] = 'w'
screen.blit(win_card, (0,0))
text_gen('YOU WON!', WHITE, 205, 205)
if game_end() == 'lose':
turn_counter[-1] = 'l'
screen.blit(lose_card, (0,0))
text_gen('YOU DIED...', WHITE, 195, 205)
text_gen(f'Rounds played: {len(turn_counter)}', WHITE, 165, 70)
text_gen('Play again?', WHITE, 185, 100)
text_gen(f'Wins: {turn_counter.count("w")}', WHITE, 145, 0)
text_gen(f'Losses: {turn_counter.count("l")}', WHITE, 245, 0)
if yes.display(screen):
game_run = True
end_screen = False
if no.display(screen):
end_screen = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
end_screen = False
pygame.display.update()
if not start_menu and not game_run and not end_screen:
sys.exit()