-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.py
More file actions
377 lines (353 loc) · 12.9 KB
/
Copy pathmain.py
File metadata and controls
377 lines (353 loc) · 12.9 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
# Python Arcade Shooting Gallery!
import pygame
import math
pygame.init()
fps = 60
timer = pygame.time.Clock()
font = pygame.font.Font('assets/font/myFont.ttf', 32)
big_font = pygame.font.Font('assets/font/myFont.ttf', 60)
WIDTH = 900
HEIGHT = 800
screen = pygame.display.set_mode([WIDTH, HEIGHT])
bgs = []
banners = []
guns = []
target_images = [[], [], []]
targets = {1: [10, 5, 3],
2: [12, 8, 5],
3: [15, 12, 8, 3]}
level = 0
points = 0
total_shots = 0
# 0 = freeplay, 1 - accuracy, 2 - timed
mode = 0
ammo = 0
time_passed = 0
time_remaining = 0
counter = 1
best_freeplay = 0
best_ammo = 0
best_timed = 0
shot = False
menu = True
game_over = False
pause = False
clicked = False
write_values = False
new_coords = True
one_coords = [[], [], []]
two_coords = [[], [], []]
three_coords = [[], [], [], []]
menu_img = pygame.image.load(f'assets/menus/mainMenu.png')
game_over_img = pygame.image.load(f'assets/menus/gameOver.png')
pause_img = pygame.image.load(f'assets/menus/pause.png')
for i in range(1, 4):
bgs.append(pygame.image.load(f'assets/bgs/{i}.png'))
banners.append(pygame.image.load(f'assets/banners/{i}.png'))
guns.append(pygame.transform.scale(pygame.image.load(f'assets/guns/{i}.png'), (100, 100)))
if i < 3:
for j in range(1, 4):
target_images[i - 1].append(pygame.transform.scale(
pygame.image.load(f'assets/targets/{i}/{j}.png'), (120 - (j * 18), 80 - (j * 12))))
else:
for j in range(1, 5):
target_images[i - 1].append(pygame.transform.scale(
pygame.image.load(f'assets/targets/{i}/{j}.png'), (120 - (j * 18), 80 - (j * 12))))
file = open('high_scores.txt', 'r')
read_file = file.readlines()
file.close()
best_freeplay = int(read_file[0])
best_ammo = int(read_file[1])
best_timed = int(read_file[2])
pygame.mixer.init()
pygame.mixer.music.load('assets/sounds/bg_music.mp3')
plate_sound = pygame.mixer.Sound('assets/sounds/Broken plates.wav')
plate_sound.set_volume(.2)
bird_sound = pygame.mixer.Sound('assets/sounds/Drill Gear.mp3')
bird_sound.set_volume(.2)
laser_sound = pygame.mixer.Sound('assets/sounds/Laser Gun.wav')
laser_sound.set_volume(.3)
pygame.mixer.music.play()
def draw_score():
points_text = font.render(f'Points: {points}', True, 'black')
screen.blit(points_text, (320, 660))
shots_text = font.render(f'Total Shots: {total_shots}', True, 'black')
screen.blit(shots_text, (320, 687))
time_text = font.render(f'Time Elapsed: {time_passed}', True, 'black')
screen.blit(time_text, (320, 714))
if mode == 0:
mode_text = font.render(f'Freeplay!', True, 'black')
if mode == 1:
mode_text = font.render(f'Ammo Remaining: {ammo}', True, 'black')
if mode == 2:
mode_text = font.render(f'Time Remaining {time_remaining}', True, 'black')
screen.blit(mode_text, (320, 741))
def draw_gun():
mouse_pos = pygame.mouse.get_pos()
gun_point = (WIDTH / 2, HEIGHT - 200)
lasers = ['red', 'purple', 'green']
clicks = pygame.mouse.get_pressed()
if mouse_pos[0] != gun_point[0]:
slope = (mouse_pos[1] - gun_point[1]) / (mouse_pos[0] - gun_point[0])
else:
slope = -100000
angle = math.atan(slope)
rotation = math.degrees(angle)
if mouse_pos[0] < WIDTH / 2:
gun = pygame.transform.flip(guns[level - 1], True, False)
if mouse_pos[1] < 600:
screen.blit(pygame.transform.rotate(gun, 90 - rotation), (WIDTH / 2 - 90, HEIGHT - 250))
if clicks[0]:
pygame.draw.circle(screen, lasers[level - 1], mouse_pos, 5)
else:
gun = guns[level - 1]
if mouse_pos[1] < 600:
screen.blit(pygame.transform.rotate(gun, 270 - rotation), (WIDTH / 2 - 30, HEIGHT - 250))
if clicks[0]:
pygame.draw.circle(screen, lasers[level - 1], mouse_pos, 5)
def move_level(coords):
if level == 1 or level == 2:
max_val = 3
else:
max_val = 4
for i in range(max_val):
for j in range(len(coords[i])):
my_coords = coords[i][j]
if my_coords[0] < -150:
coords[i][j] = (WIDTH, my_coords[1])
else:
coords[i][j] = (my_coords[0] - 2 ** i, my_coords[1])
return coords
def draw_level(coords):
if level == 1 or level == 2:
target_rects = [[], [], []]
else:
target_rects = [[], [], [], []]
for i in range(len(coords)):
for j in range(len(coords[i])):
target_rects[i].append(pygame.rect.Rect((coords[i][j][0] + 20, coords[i][j][1]),
(60 - i * 12, 60 - i * 12)))
screen.blit(target_images[level - 1][i], coords[i][j])
return target_rects
def check_shot(targets, coords):
global points
mouse_pos = pygame.mouse.get_pos()
for i in range(len(targets)):
for j in range(len(targets[i])):
if targets[i][j].collidepoint(mouse_pos):
coords[i].pop(j)
points += 10 + 10 * (i ** 2)
if level == 1:
bird_sound.play()
elif level == 2:
plate_sound.play()
elif level == 3:
laser_sound.play()
return coords
def draw_menu():
global game_over, pause, mode, level, menu, time_passed, total_shots, points, ammo
global time_remaining, best_freeplay, best_ammo, best_timed, write_values, clicked, new_coords
game_over = False
pause = False
screen.blit(menu_img, (0, 0))
mouse_pos = pygame.mouse.get_pos()
clicks = pygame.mouse.get_pressed()
freeplay_button = pygame.rect.Rect((170, 524), (260, 100))
screen.blit(font.render(f'{best_freeplay}', True, 'black'), (340, 580))
ammo_button = pygame.rect.Rect((475, 524), (260, 100))
screen.blit(font.render(f'{best_ammo}', True, 'black'), (650, 580))
timed_button = pygame.rect.Rect((170, 661), (260, 100))
screen.blit(font.render(f'{best_timed}', True, 'black'), (350, 710))
reset_button = pygame.rect.Rect((475, 661), (260, 100))
if freeplay_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
mode = 0
level = 1
menu = False
time_passed = 0
total_shots = 0
points = 0
clicked = True
new_coords = True
if ammo_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
mode = 1
level = 1
menu = False
time_passed = 0
ammo = 81
total_shots = 0
points = 0
clicked = True
new_coords = True
if timed_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
mode = 2
level = 1
menu = False
time_remaining = 30
time_passed = 0
total_shots = 0
points = 0
clicked = True
new_coords = True
if reset_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
best_freeplay = 0
best_ammo = 0
best_timed = 0
clicked = True
write_values = True
def draw_game_over():
global clicked, level, pause, game_over, menu, points, total_shots, time_passed, time_remaining
if mode == 0:
display_score = time_passed
else:
display_score = points
screen.blit(game_over_img, (0, 0))
mouse_pos = pygame.mouse.get_pos()
clicks = pygame.mouse.get_pressed()
exit_button = pygame.rect.Rect((170, 661), (260, 100))
menu_button = pygame.rect.Rect((475, 661), (260, 100))
screen.blit(big_font.render(f'{display_score}', True, 'black'), (650, 570))
if menu_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
clicked = True
level = 0
pause = False
game_over = False
menu = True
points = 0
total_shots = 0
time_passed = 0
time_remaining = 0
if exit_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
global run
run = False
def draw_pause():
global level, pause, menu, points, total_shots, time_passed, time_remaining, clicked, new_coords
screen.blit(pause_img, (0, 0))
mouse_pos = pygame.mouse.get_pos()
clicks = pygame.mouse.get_pressed()
resume_button = pygame.rect.Rect((170, 661), (260, 100))
menu_button = pygame.rect.Rect((475, 661), (260, 100))
if resume_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
level = resume_level
pause = False
clicked = True
if menu_button.collidepoint(mouse_pos) and clicks[0] and not clicked:
pygame.mixer.music.play()
level = 0
pause = False
menu = True
points = 0
total_shots = 0
time_passed = 0
time_remaining = 0
clicked = True
new_coords = True
run = True
while run:
timer.tick(fps)
if level != 0:
if counter < 60:
counter += 1
else:
counter = 1
time_passed += 1
if mode == 2:
time_remaining -= 1
if new_coords:
# initialize enemy coordinates
one_coords = [[], [], []]
two_coords = [[], [], []]
three_coords = [[], [], [], []]
for i in range(3):
my_list = targets[1]
for j in range(my_list[i]):
one_coords[i].append((WIDTH // (my_list[i]) * j, 300 - (i * 150) + 30 * (j % 2)))
for i in range(3):
my_list = targets[2]
for j in range(my_list[i]):
two_coords[i].append((WIDTH // (my_list[i]) * j, 300 - (i * 150) + 30 * (j % 2)))
for i in range(4):
my_list = targets[3]
for j in range(my_list[i]):
three_coords[i].append((WIDTH // (my_list[i]) * j, 300 - (i * 100) + 30 * (j % 2)))
new_coords = False
screen.fill('black')
screen.blit(bgs[level - 1], (0, 0))
screen.blit(banners[level - 1], (0, HEIGHT - 200))
if menu:
level = 0
draw_menu()
if game_over:
level = 0
draw_game_over()
if pause:
level = 0
draw_pause()
if level == 1:
target_boxes = draw_level(one_coords)
one_coords = move_level(one_coords)
if shot:
one_coords = check_shot(target_boxes, one_coords)
shot = False
elif level == 2:
target_boxes = draw_level(two_coords)
two_coords = move_level(two_coords)
if shot:
two_coords = check_shot(target_boxes, two_coords)
shot = False
elif level == 3:
target_boxes = draw_level(three_coords)
three_coords = move_level(three_coords)
if shot:
three_coords = check_shot(target_boxes, three_coords)
shot = False
if level > 0:
draw_gun()
draw_score()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse_position = pygame.mouse.get_pos()
if (0 < mouse_position[0] < WIDTH) and (0 < mouse_position[1] < HEIGHT - 200):
shot = True
total_shots += 1
if mode == 1:
ammo -= 1
if (670 < mouse_position[0] < 860) and (660 < mouse_position[1] < 715):
resume_level = level
pause = True
clicked = True
if (670 < mouse_position[0] < 860) and (715 < mouse_position[1] < 760):
menu = True
pygame.mixer.music.play()
clicked = True
new_coords = True
if event.type == pygame.MOUSEBUTTONUP and event.button == 1 and clicked:
clicked = False
if level > 0:
if target_boxes == [[], [], []] and level < 3:
level += 1
if (level == 3 and target_boxes == [[], [], [], []]) or (mode == 1 and ammo == 0) or (
mode == 2 and time_remaining == 0):
new_coords = True
pygame.mixer.music.play()
if mode == 0:
if time_passed < best_freeplay or best_freeplay == 0:
best_freeplay = time_passed
write_values = True
if mode == 1:
if points > best_ammo:
best_ammo = points
write_values = True
if mode == 2:
if points > best_timed:
best_timed = points
write_values = True
game_over = True
if write_values:
file = open('high_scores.txt', 'w')
file.write(f'{best_freeplay}\n{best_ammo}\n{best_timed}')
file.close()
write_values = False
pygame.display.flip()
pygame.quit()