-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathPart-16-Music.py
309 lines (272 loc) · 10.4 KB
/
Part-16-Music.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
import pygame
import os
# Init and Create Window (win)
pygame.init()
win_height = 400
win_width = 800
win = pygame.display.set_mode((win_width, win_height))
# Load and Size Images
# Hero (Player)
left = [pygame.image.load(os.path.join("Assets/Hero", "L1.png")),
pygame.image.load(os.path.join("Assets/Hero", "L2.png")),
pygame.image.load(os.path.join("Assets/Hero", "L3.png")),
pygame.image.load(os.path.join("Assets/Hero", "L4.png")),
pygame.image.load(os.path.join("Assets/Hero", "L5.png")),
pygame.image.load(os.path.join("Assets/Hero", "L6.png")),
pygame.image.load(os.path.join("Assets/Hero", "L7.png")),
pygame.image.load(os.path.join("Assets/Hero", "L8.png")),
pygame.image.load(os.path.join("Assets/Hero", "L9.png"))
]
right =[pygame.image.load(os.path.join("Assets/Hero", "R1.png")),
pygame.image.load(os.path.join("Assets/Hero", "R2.png")),
pygame.image.load(os.path.join("Assets/Hero", "R3.png")),
pygame.image.load(os.path.join("Assets/Hero", "R4.png")),
pygame.image.load(os.path.join("Assets/Hero", "R5.png")),
pygame.image.load(os.path.join("Assets/Hero", "R6.png")),
pygame.image.load(os.path.join("Assets/Hero", "R7.png")),
pygame.image.load(os.path.join("Assets/Hero", "R8.png")),
pygame.image.load(os.path.join("Assets/Hero", "R9.png"))
]
# Enemy
left_enemy = [pygame.image.load(os.path.join("Assets/Enemy", "L1E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L2E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L3E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L4E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L5E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L6E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L7E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L8E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L9P.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L10P.png")),
pygame.image.load(os.path.join("Assets/Enemy", "L11P.png"))
]
right_enemy = [pygame.image.load(os.path.join("Assets/Enemy", "R1E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R2E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R3E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R4E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R5E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R6E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R7E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R8E.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R9P.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R10P.png")),
pygame.image.load(os.path.join("Assets/Enemy", "R11P.png"))
]
# Bullet
bullet_img = pygame.transform.scale(pygame.image.load(os.path.join("Assets/Bullets", "light_bullet.png")), (10, 10))
# Background
background = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "Background.png")), (win_width, win_height))
# Tower
tower = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "Tower.png")), (200,200))
# Music/Sounds
music = pygame.mixer.music.load(os.path.join("Assets/Audio", "music.ogg"))
pop_sound = pygame.mixer.Sound(os.path.join("Assets/Audio", "pop.ogg"))
pygame.mixer.music.play(-1)
class Hero:
def __init__(self, x, y):
# Walk
self.x = x
self.y = y
self.velx = 10
self.vely = 6
self.face_right = True
self.face_left = False
self.stepIndex = 0
# Jump
self.jump = False
# Bullet
self.bullets = []
self.cool_down_count = 0
# Health
self.hitbox = (self.x, self.y, 64, 64)
self.health = 30
self.lives = 1
self.alive = True
def move_hero(self, userInput):
if userInput[pygame.K_RIGHT] and self.x <= win_width - 62:
self.x += self.velx
self.face_right = True
self.face_left = False
elif userInput[pygame.K_LEFT] and self.x >= 0:
self.x -= self.velx
self.face_right = False
self.face_left = True
else:
self.stepIndex = 0
def draw(self, win):
self.hitbox = (self.x + 15, self.y + 15, 30, 40)
pygame.draw.rect(win, (255, 0, 0), (self.x + 15, self.y, 30, 10))
if self.health >= 0:
pygame.draw.rect(win, (0, 255, 0), (self.x + 15, self.y, self.health, 10))
if self.stepIndex >= 9:
self.stepIndex = 0
if self.face_left:
win.blit(left[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
if self.face_right:
win.blit(right[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
def jump_motion(self, userInput):
if userInput[pygame.K_SPACE] and self.jump is False:
self.jump = True
if self.jump:
self.y -= self.vely * 4
self.vely -= 1
if self.vely < -6:
self.jump = False
self.vely = 6
def direction(self):
if self.face_right:
return 1
if self.face_left:
return -1
def cooldown(self):
if self.cool_down_count >= 10:
self.cool_down_count = 0
elif self.cool_down_count > 0:
self.cool_down_count += 1
def shoot(self):
self.hit()
self.cooldown()
if (userInput[pygame.K_f] and self.cool_down_count == 0):
pop_sound.play()
bullet = Bullet(self.x, self.y, self.direction())
self.bullets.append(bullet)
self.cool_down_count = 1
for bullet in self.bullets:
bullet.move()
if bullet.off_screen():
self.bullets.remove(bullet)
def hit(self):
for enemy in enemies:
for bullet in self.bullets:
if enemy.hitbox[0] < bullet.x < enemy.hitbox[0] + enemy.hitbox[2] and enemy.hitbox[1] < bullet.y < \
enemy.hitbox[1] + enemy.hitbox[3]:
enemy.health -= 5
player.bullets.remove(bullet)
class Bullet:
def __init__(self, x, y, direction):
self.x = x + 15
self.y = y + 25
self.direction = direction
def draw_bullet(self):
win.blit(bullet_img, (self.x, self.y))
def move(self):
if self.direction == 1:
self.x += 15
if self.direction == -1:
self.x -= 15
def off_screen(self):
return not (self.x >= 0 and self.x <= win_width)
class Enemy:
def __init__(self, x, y, speed):
self.x = x
self.y = y
self.speed = speed
self.stepIndex = 0
# Health
self.hitbox = (self.x, self.y, 64, 64)
self.health = 30
def step(self):
if self.stepIndex >= 33:
self.stepIndex = 0
def draw(self, win):
self.hitbox = (self.x + 20, self.y + 10, 30, 45)
pygame.draw.rect(win, (255, 0, 0), (self.x + 15, self.y, 30, 10))
if self.health >= 0:
pygame.draw.rect(win, (0, 255, 0), (self.x + 15, self.y, self.health, 10))
self.step()
win.blit(left_enemy[self.stepIndex // 3], (self.x, self.y))
self.stepIndex += 1
def move(self):
self.hit()
self.x -= speed
def hit(self):
if player.hitbox[0] < enemy.x + 32 < player.hitbox[0] + player.hitbox[2] and player.hitbox[1] < enemy.y + 32 < \
player.hitbox[1] + player.hitbox[3]:
if player.health > 0:
player.health -= 1
if player.health == 0 and player.lives > 0:
player.lives -= 1
player.health = 30
elif player.health == 0 and player.lives == 0:
player.alive = False
def off_screen(self):
return not (self.x >= -50 and self.x <= win_width + 50)
# Draw Game
def draw_game():
global tower_health, speed
win.fill((0, 0, 0))
win.blit(background, (0, 0))
# Draw Player
player.draw(win)
# Draw Bullets
for bullet in player.bullets:
bullet.draw_bullet()
# Draw Enemies
for enemy in enemies:
enemy.draw(win)
# Draw Tower
win.blit(tower, (-50, 170))
# Player Health
if player.alive == False:
win.fill((0, 0, 0))
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('You Died! Press R to restart', True, (255, 255, 255))
textRect = text.get_rect()
textRect.center = (win_width // 2, win_height // 2)
win.blit(text, textRect)
if userInput[pygame.K_r]:
player.alive = True
player.lives = 1
player.health = 30
tower_health = 2
speed = 2
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('Lives: ' + str(player.lives) + ' | Tower Health: '+ str(tower_health) + ' |Kills: '+ str(kills), True, (0, 0, 0))
win.blit(text, (150, 20))
# Delay and Update
pygame.time.delay(30)
pygame.display.update()
# Instance of Hero-Class
player = Hero(250, 290)
# Instance of Enemy-Class
enemies = []
speed = 2
kills = 0
# Tower
tower_health = 2
# Mainloop
run = True
while run:
# Quit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Input
userInput = pygame.key.get_pressed()
# Shoot
player.shoot()
# Movement
player.move_hero(userInput)
player.jump_motion(userInput)
# Tower Health
if tower_health == 0:
player.alive = False
# Enemy
if len(enemies) == 0:
enemy = Enemy(750, 300, speed)
enemies.append(enemy)
if speed <= 10:
speed += 0.25
for enemy in enemies:
enemy.move()
if enemy.off_screen() or enemy.health == 0:
enemies.remove(enemy)
if enemy.x < 50:
enemies.remove(enemy)
tower_health -= 1
if enemy.health == 0:
kills +=1
# Draw Game in Window
draw_game()