-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrols.py
More file actions
433 lines (330 loc) · 15 KB
/
controls.py
File metadata and controls
433 lines (330 loc) · 15 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import random
import time
import pygame
import sys
from bullet import Bullet
from shooter_bullet import Shooter_bullet
from invader import Invader
from wall import Wall
from superpower import MachineGun, Invincibility, Freeze
def print_text(message, x, y, screen, font_color = (100, 186, 158), font_size=36):
font = pygame.font.SysFont('arial', font_size)
text = font.render(message, True, font_color)
screen.blit(text, (x, y))
def pause(screen, score, statistics, gun, bullets, shooter_bullets, invaders, walls, shift, levelinfo, mistery):
paused = True
print_text('Paused. Press Esc to continue', 200, 300, screen)
pygame.display.update()
time.sleep(0.2)
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
save_scores(score.player_name, statistics.score, levelinfo)
sys.exit()
keys = pygame.key.get_pressed()
print_text('Paused. Press Esc to continue', 200, 300, screen)
print_text('Press S to save the game', 200, 350, screen)
print_text('Press M to return to menu', 200, 400, screen)
if keys[pygame.K_ESCAPE]:
paused = False
print_text("The game is saved", 700, 580, screen, "black", 20)
if keys[pygame.K_s]:
print_text("The game is saved", 600, 550, screen, (200, 200, 200), 20)
save_game(gun, bullets, shooter_bullets, invaders, walls, shift, levelinfo, statistics, mistery)
if keys[pygame.K_m]:
statistics.is_pressed_return_to_menu = True
return
pygame.display.update()
is_pressed_SPACE = False
def events(screen, score, statistics, gun, bullets, shooter_bullets, invaders, walls, shift, level_info, mistery):
global is_pressed_SPACE
for event in pygame.event.get():
if event.type == pygame.QUIT:
save_scores(score.player_name, statistics.score, level_info)
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause(screen, score, statistics, gun, bullets, shooter_bullets, invaders, walls, shift, level_info, mistery)
gun.move_right = False
gun.move_left = False
if event.key == pygame.K_d:
gun.move_right = True
if event.key == pygame.K_a:
gun.move_left = True
if event.key == pygame.K_SPACE:
is_pressed_SPACE = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
gun.move_right = False
if event.key == pygame.K_a:
gun.move_left = False
if event.key == pygame.K_SPACE:
is_pressed_SPACE = False
if is_pressed_SPACE and (gun.machine_gun or len(bullets) == 0):
new_bullet = Bullet(screen, gun)
bullets.add(new_bullet)
def update_screen(
screen, score, gun, invaders, color, bullets, shooter_bullets, walls, clock, mistery, statistics, machine_guns, invincibilities, freezes
):
screen.fill(color)
for machine_gune in machine_guns:
machine_gune.print()
machine_gune.update()
for invincibility in invincibilities:
invincibility.print()
invincibility.update()
for freeze in freezes:
freeze.print()
freeze.update()
score.print()
score.print_superpower(gun)
for wall in walls:
wall.print()
for bullet in bullets.sprites():
bullet.print_bullet()
for shooter_bullet in shooter_bullets.sprites():
shooter_bullet.print_bullet()
gun.print()
for invader in invaders.sprites():
invader.print()
mistery.update()
if mistery.is_appear:
mistery.print()
pygame.display.flip()
clock.tick(60)
def update_bullets(
screen, statistics, score, bullets, invaders, shooter_bullets, walls, shift, level_info, mistery, machine_guns,
gun, invincibilities, freezes
):
bullets.update()
shooter_bullets.update()
for bullet in bullets:
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
for shooter_bullet in shooter_bullets:
if shooter_bullet.rect.bottom >= screen.get_height():
shooter_bullets.remove(shooter_bullet)
invader_bullet_collisions = pygame.sprite.groupcollide(bullets, invaders, True, True)
bullet_wall_collisions = pygame.sprite.groupcollide(
bullets, walls, True, False
)
shooter_bullet_wall_collisions = pygame.sprite.groupcollide(
shooter_bullets, walls, True, False
)
machine_gun_bullet_collisions = pygame.sprite.groupcollide(
bullets, machine_guns, True, True
)
invincibilities_bullet_collisions = pygame.sprite.groupcollide(
bullets, invincibilities, True, True
)
freezes_bullet_collisions = pygame.sprite.groupcollide(bullets, freezes, True, True)
if freezes_bullet_collisions:
gun.activate_freeze()
if machine_gun_bullet_collisions:
gun.activate_machine_gun()
if invincibilities_bullet_collisions:
gun.activate_invincibility()
if len(invaders) == 0:
create_invaders(screen, invaders, shooter_bullets, shift, level_info)
if invader_bullet_collisions:
for invaders in invader_bullet_collisions.values():
for invader in invaders:
activate_superpowers(invader, invincibilities, machine_guns, screen, gun, freezes)
statistics.score += 10 * invader.number
print_score_with_lives(score, statistics)
check_walls_collision(bullet_wall_collisions, screen, machine_guns, invincibilities, gun, freezes)
check_walls_collision(shooter_bullet_wall_collisions, screen, machine_guns, invincibilities, gun, freezes)
if (pygame.sprite.spritecollideany(mistery, bullets)):
mistery.is_appear = False
for bullet in bullets:
bullets.remove(bullet)
statistics.score += random.randint(1, 5) * 100
print_score_with_lives(score, statistics)
def check_walls_collision(shooter_bullet_wall_collisions, screen, machine_guns, invincibilities, gun, freezes):
if shooter_bullet_wall_collisions:
for collision_walls in shooter_bullet_wall_collisions.values():
for collision_wall in collision_walls:
if collision_wall.health == 1:
activate_superpowers(collision_wall, invincibilities, machine_guns, screen, gun, freezes)
collision_wall.update()
def activate_superpowers(collision_wall, invincibilities, machine_guns, screen, gun, freezes):
if random.randint(1, 10) == 1 and not(gun.invincibility or gun.machine_gun or gun.freeze):
number = random.randint(0, 100)
if number < 30:
machine_gun = MachineGun(screen, collision_wall)
machine_guns.add(machine_gun)
elif number > 70:
invincibility = Invincibility(screen, collision_wall)
invincibilities.add(invincibility)
else:
freeze = Freeze(screen, collision_wall)
freezes.add(freeze)
def create_invaders(screen, invaders, shooter_bullets, shift, level_info):
invader = Invader(screen, 1)
invader_width = invader.rect.width + 30
invader_height = invader.rect.height
number_invader_x = int((800 - 2 * invader_width) / invader_width)
if level_info == "easy":
build_invaders(invaders, screen, shift, shooter_bullets,
invader_width, invader_height, number_invader_x,
1, 1, 1)
elif level_info == "middle":
build_invaders(invaders, screen, shift, shooter_bullets,
invader_width, invader_height, number_invader_x,
2, 1, 2)
elif level_info == "hard":
build_invaders(invaders, screen, shift, shooter_bullets,
invader_width, invader_height, number_invader_x,
4, 1, 1)
def build_invaders(invaders, screen, shift, shooter_bullets,
invader_width, invader_height, number_invader_x,
rows_shooters, rows_horned, rows_simple):
for j in range(rows_shooters - 1, -1, -1):
for i in range(0, number_invader_x):
shooter = Invader(screen, 3, shooter_bullets)
shooter.x = invader_width + (i * invader_width)
shooter.y = shift.shift + (j * invader_height)
shooter.rect.x = shooter.x
shooter.rect.y = shooter.y
invaders.add(shooter)
for j in range(rows_shooters, rows_shooters + rows_horned):
for i in range(0, number_invader_x):
horned_invader = Invader(screen, 2)
horned_invader.x = invader_width + (i * invader_width)
horned_invader.y = shift.shift + (j * invader_height)
horned_invader.rect.x = horned_invader.x
horned_invader.rect.y = horned_invader.y
invaders.add(horned_invader)
for j in range(rows_shooters + rows_horned, rows_shooters + rows_horned + rows_simple):
for i in range(0, number_invader_x):
invader = Invader(screen, 1)
invader.x = invader_width + (i * invader_width)
invader.y = shift.shift + (j * invader_height)
invader.rect.x = invader.x
invader.rect.y = invader.y
invaders.add(invader)
def check_death(
statistics, screen, score, gun, invaders, bullets, shooter_bullets, shift, level_info, walls
):
if not(gun.freeze):
invaders.update(shift)
if not(gun.invincibility) and (pygame.sprite.spritecollideany(gun, invaders)
or invaders_are_reached(screen, invaders)
or pygame.sprite.spritecollideany(gun, shooter_bullets)):
do_kill(statistics, screen, score, gun, invaders, bullets,
shooter_bullets, shift, level_info, walls)
def do_kill(
statistics, screen, score, gun, invaders, bullets, shooter_bullets, shift, level_info, walls
):
statistics.lives -= 1
if statistics.lives == 0:
save_scores(score.player_name, statistics.score, level_info)
return
score.print_lives()
bullets.empty()
shooter_bullets.empty()
gun.start()
time.sleep(2)
def invaders_are_reached(screen, invaders):
screen_rect = screen.get_rect()
for invader in invaders:
if invader.rect.bottom >= screen_rect.bottom:
return True
return False
def check_top_score(statistics, score):
if statistics.score > statistics.top_score:
statistics.top_score = statistics.score
score.print_top_score()
with open("top_score.txt", "w") as f:
f.write(str(statistics.top_score))
def create_walls(screen, walls):
wall = Wall(screen)
wall_width = wall.rect.width - 30
wall_height = wall.rect.height - 30
shift_x = 0
for k in range(8):
wall = Wall(screen)
wall.rect.x = shift_x + wall_width
wall.rect.y = 500 + wall_height
walls.add(wall)
shift_x += 100
def save_scores(player_name, player_score, level_info):
with open('scores.txt', 'a') as f:
f.write(f'{player_name}:{player_score}:{level_info}\n')
def save_game(gun, bullets, shooter_bullets, invaders, walls, shift, level_info, statistics, mistery):
with open("save.txt", 'w') as f:
f.write(f"{level_info}\n")
f.write(f"Gun {gun.center} {gun.machine_gun} {gun.machine_gun_timer} "
f"{gun.invincibility} {gun.invincibility_timer} "
f"{gun.freeze} {gun.freeze_timer}\n")
for bullet in bullets:
f.write(f"Bullet {bullet.rect.centerx} {bullet.y}\n")
for shooter_bullet in shooter_bullets:
f.write(f"Shooter_bullet {shooter_bullet.rect.centerx} {shooter_bullet.y}\n")
for invader in invaders:
f.write(f"Invader {invader.number} {invader.x} {invader.y} {invader.count_move} {invader.move_x}\n")
for wall in walls:
f.write(f"Wall {wall.health} {wall.rect.x} {wall.rect.y}\n")
f.write(f"Shift {shift.shift}\n")
f.write(f"Lives {statistics.lives}\n")
f.write(f"Score {statistics.score}\n")
f.write(f"Mistery {mistery.x} {mistery.y} {mistery.move_x} {mistery.is_appear}")
def start_saved_game(statistics, score, screen, gun, invaders, bullets, shooter_bullets, shift, walls, mistery):
with open("save.txt", 'r') as f:
entities = f.read().split('\n')
level_info = entities[0]
for entity in entities[1:]:
features = entity.split(' ')
name = features[0]
if name == "Gun":
gun.center = float(features[1])
gun.machine_gun = features[2] == "True"
gun.machine_gun_timer = float(features[3])
gun.invincibility = features[4] == "True"
gun.invincibility_timer = float(features[5])
gun.freeze = features[6] == "True"
gun.freeze_timer = float(features[7])
if name == "Bullet":
bullet = Bullet(screen, gun)
bullet.rect.centerx = float(features[1])
bullet.y = float(features[2])
bullets.add(bullet)
if name == "Shooter_bullet":
shooter_bullet = Shooter_bullet(screen, gun)
shooter_bullet.rect.centerx = float(features[1])
shooter_bullet.y = float(features[2])
shooter_bullets.add(shooter_bullet)
if name == 'Invader':
invader = Invader(screen, int(features[1]), shooter_bullets)
invader.x = float(features[2])
invader.y = float(features[3])
invader.count_move = float(features[4])
invader.move_x = float(features[5])
invaders.add(invader)
if name == 'Wall':
wall = Wall(screen, int(features[1]))
wall.rect.x = float(features[2])
wall.rect.y = float(features[3])
walls.add(wall)
if name == 'Shift':
shift.shift = float(features[1])
if name == "Lives":
statistics.lives = int(features[1])
if name == "Score":
statistics.score = int(features[1])
if name == "Mistery":
mistery.x = float(features[1])
mistery.y = float(features[2])
mistery.move_x = float(features[3])
mistery.is_appear = (features[4] == 'True')
print_score_with_lives(score, statistics)
return level_info
def print_score_with_lives(score, statistics):
score.image_score()
check_top_score(statistics, score)
score.print_lives()
def start_game(screen, gun, invaders, shooter_bullets, shift, level_info, walls):
gun.start()
create_invaders(screen, invaders, shooter_bullets, shift,
level_info)
create_walls(screen, walls)