-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
389 lines (327 loc) · 13.4 KB
/
Copy pathfunctions.py
File metadata and controls
389 lines (327 loc) · 13.4 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from turtle import Screen
import pygame
import random
import os
from button import Button
from constants import WIDTH, HEIGHT, BACKGROUND, PLAYER_VEL, ENEMY_VEL, LASER_VEL_P, LASER_VEL_E, P_LASER_SFX, \
P_DAMAGE_SFX, FPS, FONT_PATH, WHITE, E_EXPLOSION_SFX, SHAKE_INTENSITY, SHAKE_DURATION, COLLISION_DELAY, \
DISPLAY_FONT, MAIN_MENU_FONT, P_MINUS_1_SFX, COOLDOWN, BULLET_COL_SFX, P_L_DAMAGE_SFX, E_LASER_SFX, LOW_HEALTH_SFX
from game_state import game_state
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.transform.scale(pygame.image.load(os.path.join('assets', 'images', 'xwing.png')), (80, 80))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.health = 100
self.max_health = 100
self.score = 0
def health_bar(self, surface):
width = 80
height = 10
bar_x = self.rect.x
bar_y = self.rect.y + 75
pygame.draw.rect(surface, (255, 0, 0), (bar_x, bar_y, width, height))
health_percentage = self.health / self.max_health
pygame.draw.rect(surface, (0, 255, 0), (bar_x, bar_y, width * health_percentage, height))
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.rect.x -= PLAYER_VEL
if self.rect.x < 0:
self.rect.x = 0
if keys[pygame.K_d]:
self.rect.x += PLAYER_VEL
if self.rect.x > WIDTH - self.rect.width:
self.rect.x = WIDTH - self.rect.width
player = Player(WIDTH // 2 - 40, HEIGHT - 100)
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y, enemy_bullets, all_sprites):
super().__init__()
self.image = pygame.transform.rotate(pygame.transform.scale(pygame.image.load(os.path.join('assets', 'images', 'enemy_black.png')), (80, 80)), 270)
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.last_shot_time = 0
self.cooldown = random.randint(2000, 5000)
self.enemy_bullets = enemy_bullets
self.all_sprites = all_sprites
def update(self):
self.rect.y += ENEMY_VEL
if self.rect.y > HEIGHT + 100:
self.rect.x = random.randint(0, WIDTH - 80)
self.rect.y = -100
current_time = pygame.time.get_ticks()
if current_time - self.last_shot_time > self.cooldown:
if player.rect.centerx - 10 <= self.rect.centerx <= player.rect.centerx + 10 and self.rect.top > 0:
e_bullet = EnemyBullet(self.rect.centerx, self.rect.bottom)
self.enemy_bullets.add(e_bullet)
self.all_sprites.add(e_bullet)
E_LASER_SFX.play()
self.last_shot_time = current_time
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.transform.rotate(pygame.transform.scale(pygame.image.load(os.path.join('assets', 'images', 'player_laser.png')), (50, 75)), 90)
self.rect = self.image.get_rect()
self.rect.midbottom = (x, y)
def update(self):
self.rect.y -= LASER_VEL_P
if self.rect.bottom < 0:
self.kill()
class EnemyBullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.transform.rotate(pygame.transform.scale(pygame.image.load(os.path.join('assets', 'images', 'enemy_laser.png')), (50, 75)), 270)
self.rect = self.image.get_rect()
self.rect.midtop = (x, y)
def update(self):
self.rect.y += LASER_VEL_E
if self.rect.top > HEIGHT:
self.kill()
def screen_shake(screen, intensity, duration):
# such a cool effect
original_surface = screen.copy()
shake_frames = int(duration * FPS)
for _ in range(shake_frames):
screen.fill((0, 0, 0))
x_offset = random.randint(-intensity, intensity)
y_offset = random.randint(-intensity, intensity)
screen.blit(original_surface, (x_offset, y_offset))
pygame.display.flip()
pygame.time.delay(int(1000 / FPS))
screen.blit(original_surface, (0, 0))
pygame.display.flip()
def mask(image):
pygame.display.init()
image = pygame.Surface.convert_alpha(image)
return pygame.mask.from_surface(image)
def pixel_collision(obj1, obj2):
mask1 = mask(obj1.image)
mask2 = mask(obj2.image)
offset_x = obj2.rect.left - obj1.rect.left
offset_y = obj2.rect.top - obj1.rect.top
return mask1.overlap(mask2, (offset_x, offset_y)) is not None
def game_over():
pygame.mixer.music.load(os.path.join('assets', 'music', 'kaiba.mp3'))
pygame.mixer.music.play()
screen = pygame.display.get_surface()
text = MAIN_MENU_FONT.render('Game Over', True, (255, 0, 0))
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
#player.score = 0
game_state.difficulty = 3
player.health = 100
screen.blit(text, text_rect)
pygame.display.flip()
pygame.time.wait(5000)
restart()
def win():
pygame.mixer.music.load(os.path.join('assets', 'music', 'victory.mp3'))
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play()
screen = pygame.display.get_surface()
text = FONT_PATH.render(f'Level {game_state.difficulty - 2} Complete!', True, (255, 255, 255))
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
game_state.increase_difficulty()
screen.blit(text, text_rect)
pygame.display.flip()
pygame.time.wait(4200)
pygame.mixer.music.stop()
next_level()
def restart():
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('You Lose!')
final_score = MAIN_MENU_FONT.render(f'Final Score: {player.score}', True, WHITE)
final_score_rect = final_score.get_rect(center=(WIDTH // 2, HEIGHT // 3))
quit_button = Button("QUIT", WIDTH // 2 - 75, HEIGHT // 2 - 25, SCREEN, True)
restart_button = Button("RESTART", WIDTH // 2 - 75, HEIGHT // 2 - 100, SCREEN, True)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if restart_button.check_click():
running = False
break
if quit_button.check_click():
running = False
credits_roll()
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
running = False
break
if event.key == pygame.K_ESCAPE:
running = False
credits_roll()
pygame.quit()
SCREEN.fill((0, 0, 0))
restart_button.draw()
quit_button.draw()
SCREEN.blit(final_score, final_score_rect)
pygame.display.update()
pygame.time.Clock().tick(FPS)
player.rect.midtop = (WIDTH // 2, HEIGHT - 100)
start_game(SCREEN)
def next_level():
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Next Level')
quit_button = Button("QUIT", WIDTH // 2 - 75, HEIGHT // 2 - 25, SCREEN, True)
continue_button = Button("CONTINUE", WIDTH // 2 - 75, HEIGHT // 2 - 100, SCREEN, True)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if continue_button.check_click():
running = False
break
if quit_button.check_click():
running = False
credits_roll()
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
running = False
break
if event.key == pygame.K_ESCAPE:
running = False
credits_roll()
pygame.quit()
SCREEN.fill((0, 0, 0))
continue_button.draw()
quit_button.draw()
pygame.display.update()
pygame.time.Clock().tick(FPS)
player.rect.midtop = (WIDTH // 2, HEIGHT - 100)
start_game(SCREEN)
def credits_roll():
pygame.mixer.music.load(os.path.join('assets', 'music', 'anime-chill.mp3'))
pygame.mixer.music.play(-1)
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Thank You')
text = FONT_PATH.render('Thank You For Playing!', True, WHITE)
text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT + text.get_height() // 2))
scroll_speed = 3.5
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
SCREEN.fill((0, 0, 0))
text_rect.y -= scroll_speed
SCREEN.blit(text, text_rect)
if text_rect.bottom < 0:
running = False
pygame.display.flip()
pygame.time.Clock().tick(60)
pygame.quit()
def start_game(SCREEN):
pygame.mixer.music.load(os.path.join('assets', 'music', 'space_shooter.mp3'))
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
pygame.display.set_caption('Space Invaders')
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
enemy_bullets = pygame.sprite.Group()
player_group = pygame.sprite.Group()
player_group.add(player)
all_sprites.add(player)
for _ in range(game_state.difficulty):
enemy = Enemy(random.randint(0, WIDTH - 80), random.randint(-1500, -100), enemy_bullets, all_sprites)
enemies.add(enemy)
all_sprites.add(enemy)
clock = pygame.time.Clock()
last_shot_time = 0
last_collision_time = 0
alert_start = 0
alert_duration = 3000
alert_playing = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
current_time = pygame.time.get_ticks()
if keys[pygame.K_w]:
if current_time - last_shot_time > COOLDOWN:
bullet = Bullet(player.rect.centerx, player.rect.centery)
bullets.add(bullet)
all_sprites.add(bullet)
P_LASER_SFX.play()
last_shot_time = current_time
# enemy bullet and player bullet collision
for bullet in bullets:
for e_bullets in enemy_bullets:
if pixel_collision(bullet, e_bullets):
BULLET_COL_SFX.play()
bullet.kill()
e_bullets.kill()
# enemy bullet and player collision
for e_bullet in enemy_bullets:
if pixel_collision(player, e_bullet):
current_time = pygame.time.get_ticks()
if current_time - last_collision_time > COLLISION_DELAY:
P_L_DAMAGE_SFX.play()
player.health -= 15
screen_shake(SCREEN, SHAKE_INTENSITY, SHAKE_DURATION)
last_collision_time = current_time
# player and enemy collision
for enemy in enemies:
if pixel_collision(player, enemy):
current_time = pygame.time.get_ticks()
if current_time - last_collision_time > COLLISION_DELAY:
screen_shake(SCREEN, SHAKE_INTENSITY, SHAKE_DURATION)
P_DAMAGE_SFX.play()
player.health -= 10
last_collision_time = current_time
# bullet and enemy collision
for bullet in bullets:
for enemy in enemies:
if pixel_collision(bullet, enemy):
bullet.kill()
enemy.kill()
E_EXPLOSION_SFX.play()
player.score += 1
if len(enemies) == 0:
pygame.time.wait(250)
for enemy in enemies:
if enemy.rect.top >= HEIGHT:
player.score -= 1
enemy.kill()
P_MINUS_1_SFX.play()
new_enemy = Enemy(random.randint(0, WIDTH - 80), -100, enemy_bullets, all_sprites)
enemies.add(new_enemy)
all_sprites.add(new_enemy)
if player.score <= 0:
player.health -= 10
player.score = 0
all_sprites.update()
if player.health <= 15:
if not alert_playing:
LOW_HEALTH_SFX.play()
alert_start = current_time
alert_playing = True
if alert_playing and current_time - alert_start >= alert_duration:
LOW_HEALTH_SFX.stop()
if player.health <= 0:
game_over()
return
if len(enemies) == 0:
win()
return
SCREEN.blit(BACKGROUND, (0, 0))
player.health_bar(SCREEN)
all_sprites.draw(SCREEN)
level_font = DISPLAY_FONT.render(f'Level: {game_state.difficulty - 2}', True, WHITE)
level_rect = level_font.get_rect(topright=(WIDTH - 10, 10))
score_font = DISPLAY_FONT.render(f'Score: {player.score}', True, WHITE)
score_rect = score_font.get_rect(topleft=(10, 10))
SCREEN.blit(score_font, score_rect)
SCREEN.blit(level_font, level_rect)
pygame.display.update()
clock.tick(FPS)
pygame.quit()