-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1147 lines (1009 loc) · 41.2 KB
/
main.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Основной файл проекта
import random
from source.init import *
# Создание экрана паузы
def pause(chosed=None, moved=None):
screen.fill(pygame.Color('black'))
tile_group.draw(screen)
player_group.draw(screen)
enemy_group.draw(screen)
player_damageable_group.draw(screen)
enemy_damageable_group.draw(screen)
temporary_group.draw(screen)
solid_group.draw(screen)
image = pygame.Surface([WIDTH // 3, HEIGHT])
image.fill(pygame.Color("black"))
screen.blit(image, (WIDTH // 3, 0))
font = pygame.font.Font(None, 50)
texts = ["Продолжить", "Настройки", "Выйти в меню", "Выход"]
for i in range(4): # Создание кнопок
width = 1
cr = (100, 255, 100)
if i == chosed:
cr = (50, 175, 100)
width = 2
elif i == moved:
cr = (180, 255, 180)
text = font.render(texts[i], 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_x = WIDTH // 2 - text.get_width() // 2
text_y = (HEIGHT // 5) * i + text_h // 2 + HEIGHT // 10
screen.blit(text, (text_x, text_y))
pygame.draw.rect(screen, (0, 255, 0), (text_x - 10 - width // 2, text_y - 10,
text_w + 20 - width // 2, text_h + 20), width)
MENUBTTNS[i] = ((text_x - 10 - width // 2, text_y - 10,
text_w + 20 - width // 2, text_h + 20), cr)
# Создание экрана настроек
def settings(chosed=None, moved=None):
screen.fill((0, 0, 0))
font = pygame.font.Font(None, 50)
texts = ['Игрок 1', 'Игрок 2', 'Громкость', 'НАЗАД']
for i in range(4):
cr = (100, 255, 100)
width = 1
if chosed == i == 3:
cr = (100, 175, 100)
width = 2
elif moved == i == 3:
cr = (170, 255, 170)
text = font.render(texts[i], 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_y = HEIGHT // 5 * (i + 1) - text_h // 2
if i != 3:
text_x = WIDTH // 8 - text_w // 2
else:
text_x = WIDTH // 2 - text_w // 2
screen.blit(text, (text_x, text_y))
if i != 3:
text_x = WIDTH // 3 * 2
text_w = WIDTH // 4
pygame.draw.rect(screen, (0, 255, 0), (text_x - 10 - width // 2, text_y - 10 - width // 2,
text_w + 20, text_h + 20), width)
if i == 2:
pygame.draw.rect(screen, (0, 255, 0), (text_x - 10 - width // 2,
text_y - 10 - width // 2,
(text_w + 20) * (VOLUME / 100), text_h + 20))
vol = font.render(str(VOLUME), 1, cr)
screen.blit(vol, (text_x + 10 - width // 2 + text_w, text_y - 10 - width // 2))
elif i == 0:
if PLAYRSKEYS[0] == WASDBTTNS:
screen.blit(load_image('WASD_picture.png'), (text_x - 10 - width // 2,
text_y - 10 - width // 2))
else:
screen.blit(load_image('Arrs_picture.png'), (text_x - 10 - width // 2,
text_y - 10 - width // 2))
elif i == 1:
if PLAYRSKEYS[1] == WASDBTTNS:
screen.blit(load_image('WASD_picture.png'), (text_x - 10 - width // 2,
text_y - 10 - width // 2))
else:
screen.blit(load_image('Arrs_picture.png'), (text_x - 10 - width // 2,
text_y - 10 - width // 2))
STNGBTTNS[i] = (((text_x - 10 - width // 2, text_y - 10 - width // 2,
text_w + 20, text_h + 20)), cr)
# Создание экрана выбора режима игры
def choose_mode(chosed=None, moved=None):
screen.fill((0, 0, 0))
font = pygame.font.Font(None, 50)
texts = ['1 игрок', '2 игрока', 'СТАРТ', 'НАЗАД']
for i in range(4): # Создание кнопок
cr = (100, 255, 100)
width = 1
if chosed == i:
cr = (100, 175, 100)
width = 2
elif moved == i:
cr = (170, 255, 170)
if PLAYRSCOUNT - 1 == i:
cr = (50, 205, 50)
width = 4
text = font.render(texts[i], 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_y = HEIGHT // 4 - text_h // 2
text_x = WIDTH // 3 * i + WIDTH // 4
if i == 2:
text_y = HEIGHT // 4 * 2.5 - text_h // 2
text_x = WIDTH // 2 - text_w // 2
elif i == 3:
text_y = HEIGHT // 4 * 3.5 - text_h // 2
text_x = WIDTH // 2 - text_w // 2
screen.blit(text, (text_x, text_y))
pygame.draw.rect(screen, (0, 255, 0), (text_x - 10 - width // 2, text_y - 10 - width // 2,
text_w + 20, text_h + 20), width)
MODEBTTNS[i] = ((text_x - 10 - width // 2, text_y - 10,
text_w + 20 - width // 2, text_h + 20), cr)
# Создание экрана главного меню
def main_menu(pressed=None, moved=None):
screen.fill((0, 0, 0))
font = pygame.font.Font(None, 50)
texts = ["Новая игра", "Продолжить", "Настройки", "Выход"]
for i in range(4):
width = 1
cr = (100, 255, 100)
if i == pressed:
cr = (50, 175, 100)
width = 2
elif i == moved:
cr = (180, 255, 180)
text = font.render(texts[i], 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_x = WIDTH // 2 - text.get_width() // 2
text_y = (HEIGHT // 5) * i + text_h // 2 + HEIGHT // 10
screen.blit(text, (text_x, text_y))
pygame.draw.rect(screen, (0, 255, 0), (text_x - 10 - width // 2, text_y - 10,
text_w + 20 - width // 2, text_h + 20), width)
MENUBTTNS[i] = ((text_x - 10 - width // 2, text_y - 10,
text_w + 20 - width // 2, text_h + 20), cr)
# Функция, указывающая, на какой из кнопок сейчас мышь
def what_is_pressed(buttons, pos):
x, y = pos
for it, i in enumerate(buttons):
if i[0][2] + i[0][0] >= x >= i[0][0] and i[0][1] <= y <= i[0][1] + i[0][3]:
return it
# Обработчик главного меню
def start_screen():
main_menu()
in_menu = True
flag = None
btn = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
btn = what_is_pressed(MENUBTTNS, (x, y))
if btn == 3:
flag = terminate
elif btn == 2:
flag = settings_screen
elif btn == 1:
flag = continue_screen
elif btn == 0:
flag = choose_mode_screen
main_menu(btn)
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if flag is not None:
it = what_is_pressed(MENUBTTNS, event.pos)
if it == btn:
in_menu = False
break
else:
flag = None
main_menu()
elif event.type == pygame.MOUSEMOTION:
btn2 = what_is_pressed(MENUBTTNS, event.pos)
if 1 in event.buttons:
main_menu(btn, btn2)
else:
main_menu(moved=btn2)
if not in_menu:
break
pygame.display.flip()
clock.tick(FPS)
flag()
# Обработчик экрана паузы
def pause_screen():
pause()
in_menu = True
flag = None
btn = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
btn = what_is_pressed(MENUBTTNS, (x, y))
flag = btn
pause(btn)
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if flag is not None:
it = what_is_pressed(MENUBTTNS, event.pos)
if it == btn:
if it == 0:
return
elif it == 1:
settings_screen(True)
elif it == 2:
return start_screen
elif it == 3:
terminate()
break
else:
flag = None
pause()
elif event.type == pygame.MOUSEMOTION:
btn2 = what_is_pressed(MENUBTTNS, event.pos)
if 1 in event.buttons:
pause(btn, btn2)
else:
pause(moved=btn2)
if not in_menu:
break
pygame.display.flip()
clock.tick(FPS)
# Обработчик экрана настроек
def settings_screen(on_pause=False):
flag = None
global VOLUME
settings()
in_menu = True
btn = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
btn = what_is_pressed(STNGBTTNS, (x, y))
if btn == 3:
flag = start_screen
elif btn == 2:
flag = 2
elif btn == 1:
flag = 1
elif btn == 0:
flag = 0
settings(btn)
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if flag is not None:
it = what_is_pressed(STNGBTTNS, event.pos)
if it == btn:
if it == 3:
if on_pause:
return
in_menu = False
break
elif it == 0:
if PLAYRSKEYS[0] == WASDBTTNS:
PLAYRSKEYS[0] = ARRWBTTNS
else:
PLAYRSKEYS[0] = WASDBTTNS
elif it == 1:
if PLAYRSKEYS[1] == WASDBTTNS:
PLAYRSKEYS[1] = ARRWBTTNS
else:
PLAYRSKEYS[1] = WASDBTTNS
else:
flag = None
settings()
elif event.type == pygame.MOUSEMOTION:
btn2 = what_is_pressed(STNGBTTNS, event.pos)
if 1 in event.buttons:
x, y = event.pos
if flag == 2:
VOLUME = int(((x - STNGBTTNS[2][0][0]) * 100 / STNGBTTNS[2][0][2]))
if VOLUME > 100:
VOLUME = 100
elif VOLUME < 0:
VOLUME = 0
tank_shot.set_volume(VOLUME / 100)
for i in sounds:
i.set_volume(VOLUME / 100)
settings(btn, btn2)
else:
settings(moved=btn2)
if not in_menu:
break
pygame.display.flip()
clock.tick(FPS)
flag()
# Загрузка игры с файла (при нажатии "Продолжить" в главном меню)
def continue_screen():
global PLAYRSCOUNT, SCORE
try:
with open('data/cont.txt', 'r') as f:
level, players, score = map(int, f.read().split())
if 0 > level > LEVELS or players not in (1, 2):
raise ValueError
PLAYRSCOUNT = players
SCORE = score
game(level, True)
except ValueError: # Если битый файл загрузки или какие-то ещё беды
start_screen()
# Создание и обработка экрана перехода между уровнями
def new_level_screen(level):
time = 60
flags = []
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN:
flags.append(event.button)
elif event.type == pygame.MOUSEBUTTONUP:
if event.button in flags:
return
elif event.type == pygame.KEYDOWN:
flags.append(event.key)
elif event.type == pygame.KEYUP:
if event.key in flags:
return
screen.fill((0, 0, 0))
texts = [f'Уровень {level}', f'Счёт {SCORE}', 'нажмите любую кнопку, чтобы начать']
cr = (100, 255, 100)
if time < 0:
for i in range(3):
font = pygame.font.Font(None, 50)
if i in (2, 1):
font = pygame.font.Font(None, 30)
text = font.render(texts[i], 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_x = WIDTH // 2 - text_w // 2
text_y = (HEIGHT // 5) * 2 - text_h // 2
if i == 1:
text_y = (HEIGHT // 5) * 3 - text_h // 2
if i == 2:
text_y = (HEIGHT // 5) * 4 - text_h // 2
screen.blit(text, (text_x, text_y))
if time < 0:
pygame.display.flip()
time -= 1
clock.tick(FPS)
# Создание и обработка финального экрана(после прохождения всех уровней)
def look_at_score():
flags = []
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN:
flags.append(event.button)
elif event.type == pygame.MOUSEBUTTONUP:
if event.button in flags:
return
elif event.type == pygame.KEYDOWN:
flags.append(event.key)
elif event.type == pygame.KEYUP:
if event.key in flags:
return
screen.fill((0, 0, 0))
texts = [f'Ваш счёт {SCORE}', 'нажмите любую кнопку, чтобы выйти в меню']
cr = (100, 255, 100)
for i in range(2):
font = pygame.font.Font(None, 50)
if i == 1:
font = pygame.font.Font(None, 30)
text = font.render(texts[i], 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_x = WIDTH // 2 - text_w // 2
text_y = (HEIGHT // 5) * 2 - text_h // 2
if i == 1:
text_y = (HEIGHT // 5) * 4 - text_h // 2
screen.blit(text, (text_x, text_y))
pygame.display.flip()
clock.tick(FPS)
# Обработка экрана выбора режима игры
def choose_mode_screen():
choose_mode()
flag = None
btn = None
global PLAYRSCOUNT
choosing = True
while True: # Оно работает, это главное
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
btn = what_is_pressed(MODEBTTNS, (x, y))
if btn == 0:
flag = 1
elif btn == 1:
flag = 2
elif btn == 2:
flag = game
else:
flag = start_screen
choose_mode(btn)
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if flag is not None:
it = what_is_pressed(MODEBTTNS, event.pos)
if it == btn:
if it == 0:
PLAYRSCOUNT = 1
elif it == 1:
PLAYRSCOUNT = 2
elif it == 2:
if PLAYRSCOUNT != 0:
choosing = False
break
elif it == 3:
choosing = False
PLAYRSCOUNT = 0
break
else:
flag = None
choose_mode()
elif event.type == pygame.MOUSEMOTION:
btn2 = what_is_pressed(MODEBTTNS, event.pos)
if 1 in event.buttons:
choose_mode(btn, btn2)
else:
choose_mode(moved=btn2)
if not choosing:
break
pygame.display.flip()
clock.tick(FPS)
flag()
# Функция создания уровня на экране
def generate_level(level, biom):
global player_one, player_two, green_spawnpoint, red_spawnpoint
green_spawnpoint, red_spawnpoint = None, None # Точки появления для игроков
enemies_spawnpoints = [] # список с точками появления проивников
empty_name = 'empty'
biome_modif = ''
if biom == 'snowy': # Поддержка биомов, указываемых в файле с уровнем
biome_modif = 'snowy_'
empty_name = 'snowy'
for y in range(1, len(level), 2): # Создание всех объектов, что не заборчики
for x in range(1, len(level[y]), 2):
if level[y][x] == '.':
Tile(empty_name, (x - 1) // 2, (y - 1) // 2)
if random.randint(1, 10) == 1:
Tree('default', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == '#':
Building(biome_modif + 'wall', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == 'G':
Tile(empty_name, (x - 1) // 2, (y - 1) // 2)
green_spawnpoint = ((x - 1) // 2, (y - 1) // 2)
player_one = Player(1, green_spawnpoint)
elif level[y][x] == 'R':
Tile(empty_name, (x - 1) // 2, (y - 1) // 2)
red_spawnpoint = ((x - 1) // 2, (y - 1) // 2)
if PLAYRSCOUNT == 2:
red_spawnpoint = ((x - 1) // 2, (y - 1) // 2)
player_two = Player(2, red_spawnpoint)
elif level[y][x] == 'E':
Tile(empty_name, (x - 1) // 2, (y - 1) // 2)
enemies_spawnpoints.append(((x - 1) // 2, (y - 1) // 2))
elif level[y][x] == 'V':
Tile(biome_modif + 'ver_rail', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == 'H':
Tile(biome_modif + 'hor_rail', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == '2':
Tile(biome_modif + 'ver_rail_be', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == '8':
Tile(biome_modif + 'ver_rail_te', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == '4':
Tile(biome_modif + 'hor_rail_le', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == '6':
Tile(biome_modif + 'hor_rail_re', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == 'W':
Tile(biome_modif + 'ver_rail', (x - 1) // 2, (y - 1) // 2)
Train('verti', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == 'A':
Tile(biome_modif + 'hor_rail', (x - 1) // 2, (y - 1) // 2)
Train('horiz', (x - 1) // 2, (y - 1) // 2)
elif level[y][x] == 'T':
Tile(empty_name, (x - 1) // 2, (y - 1) // 2)
Tree('default', (x - 1) // 2, (y - 1) // 2)
for y in range(1, len(level), 2): # Обработка вертикальных заборчиков
for x in range(0, len(level[y]), 2):
if level[y][x] == 'V':
Boarding('verti', x // 2, (y - 1) // 2)
for y in range(0, len(level), 2): # Обработка горизонтальных заборчиков(жаль, что они только 1 типа)
for x in range(1, len(level[y]), 2):
if level[y][x] == 'H':
Boarding('horiz', (x - 1) // 2, y // 2)
return green_spawnpoint, red_spawnpoint, enemies_spawnpoints # возвращаем точки появления всех танков
# Функция обработки игрового процесса с заданным уровнем
def level_play(level_name='level1.txt'):
global ENEMIES_LEFT, LOCAL_SCORE
print('Game has been started')
in_game = True
LOCAL_SCORE = 0
all_sprites.empty()
tile_group.empty()
player_group.empty()
object_group.empty()
solid_group.empty()
player_damageable_group.empty()
enemy_damageable_group.empty()
temporary_group.empty()
level, ENEMIES_LEFT, biom = load_level(level_name)
enemies_to_spawn = ENEMIES_LEFT
green, red, enemies = generate_level(level, biom)
enemies = list(map(lambda x: [*x, random.randint(0, 30)], enemies))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.KEYDOWN:
key = event.key
if key == pygame.K_ESCAPE:
res = pause_screen()
print(res)
if res == start_screen:
with open('data/cont.txt', 'w') as f:
f.write(f'{int(level_name.split(".")[0].split("evel")[1]) - 1} '
f'{PLAYRSCOUNT} {SCORE}')
if res is not None:
in_game = False
break
# print("Pressed", key)
for num, keys in enumerate(PLAYRSKEYS):
if key not in keys:
continue
if num == 0:
player = player_one
else:
if PLAYRSCOUNT == 2:
player = player_two
else:
continue
idx = keys.index(key)
if 0 <= idx <= 3:
player.moving = True
player.angle = ANGLES[idx]
if idx == 4:
player.shoot()
elif event.type == pygame.KEYUP:
key = event.key
if (key in PLAYRSKEYS[0] and
(PLAYRSKEYS[0].index(key), player_one.angle) in list(ANGLES.items())):
player_one.moving = False
if (key in PLAYRSKEYS[1] and PLAYRSCOUNT == 2 and
(PLAYRSKEYS[1].index(key), player_two.angle) in list(ANGLES.items())):
player_two.moving = False
if not in_game:
break
for i in enemies:
if i[2] >= 120 and random.randint(1, 8) == 1 and enemies_to_spawn > 0:
en_type = random.randint(1, 3)
if PLAYRSCOUNT == 1:
player = player_one
else:
player = random.choice([player_one, player_two])
Enemy(i[0], i[1], player, level=en_type)
i[2] = -1
enemies_to_spawn -= 1
i[2] += 1
screen.fill(pygame.Color('black'))
tile_group.draw(screen)
player_group.draw(screen)
enemy_group.draw(screen)
player_damageable_group.draw(screen)
enemy_damageable_group.draw(screen)
temporary_group.draw(screen)
solid_group.draw(screen)
if ENEMIES_LEFT == 0:
return LOCAL_SCORE
all_sprites.update()
clock.tick(FPS)
# Тут у нас счёт вырисовывается. Криво, но рисуется
font = pygame.font.Font(None, 50)
cr = (0, 200, 0)
text = font.render(str(LOCAL_SCORE + SCORE), 1, cr)
text_h = text.get_height()
text_w = text.get_width()
text_x = WIDTH // 2 - text_w // 2
text_y = (HEIGHT // 20) * 1 - text_h // 2
pygame.draw.rect(screen, (255, 255, 255), (text_x - 5, text_y - 5,
text_w + 10, text_h + 10))
screen.blit(text, (text_x, text_y))
pygame.display.flip()
res() # Оно нужно. Просто знайте это
# Функция запуска игры
def game(start_level=0, load=None):
global SCORE
level = start_level
if load is None:
SCORE = 0
while True:
level += 1
print(LEVELS, level)
if LEVELS == level:
break
new_level_screen(level)
result = level_play(f'level{level}.txt')
SCORE += result
print(level)
look_at_score() # Просмотр счёта после игры
start_screen() # Поиграли и в меню
# Класс одной клетки игрового поля
class Tile(pygame.sprite.Sprite):
def __init__(self, tile_type, pos_x, pos_y):
super().__init__(tile_group, all_sprites)
self.image = tile_images[tile_type]
self.rect = self.image.get_rect().move(tile_width * pos_x, tile_height * pos_y)
# Базовый класс объекта игрового поля
class Object(pygame.sprite.Sprite):
def __init__(self, *groups):
super().__init__(list(groups) + [object_group])
self.strength = 1000
self.image = None
self.fireable = False
self.is_fired = False
self.fire_damage = 0
self.images = []
self.is_update_images = True
# Функция нанесения урона
def damage(self, damage_level, shot=False):
self.strength -= damage_level
if shot and self.fireable:
self.is_fired = True
self.fire_damage = 1
# Функция обновления
def update(self):
global ENEMIES_LEFT
if self.strength <= 0:
if type(self) == Enemy:
ENEMIES_LEFT -= 1
self.kill()
if self.is_update_images:
for image in self.images:
if image[0] <= self.strength <= image[1]:
self.image = image[2]
if self.is_fired:
self.strength -= self.fire_damage
if random.randint(1, 8) == 1 and self.fire_damage <= 5:
self.fire_damage += 1
# Класс объекта с бесконечным запасом прочности (Building, т.к. изначально это были постройки)
class Building(Object):
def __init__(self, tile_type, pos_x, pos_y):
super().__init__(solid_group, all_sprites)
self.strength = float("inf")
self.image = building_images[tile_type]
self.rect = self.image.get_rect().move(tile_width * pos_x, tile_height * pos_y)
# Класс ограждения
class Boarding(Object):
def __init__(self, board_type, pos_x, pos_y):
super().__init__(player_damageable_group, enemy_damageable_group, all_sprites)
self.strength = 600
self.images = [[1, 300, board_images[board_type][1]],
[301, 600, board_images[board_type][0]]]
self.image = board_images[board_type][0]
board_width = 4
board_height = 4
self.rect = self.image.get_rect().move(tile_width * pos_x - board_width,
tile_height * pos_y - board_height)
# Класс вагона
class Train(Object):
def __init__(self, train_type, pos_x, pos_y):
super().__init__(player_damageable_group, enemy_damageable_group, all_sprites)
self.strength = 800
self.images = [[1, 400, train_images[train_type][1]],
[401, 800, train_images[train_type][0]]]
self.image = train_images[train_type][0]
board_width = 2
board_height = 2
self.rect = self.image.get_rect().move(tile_width * pos_x + board_width,
tile_height * pos_y + board_height)
# Класс дерева
# Если выстрелить в дерево - оно загорится и будет гореть, пока не сгорит полностью
class Tree(Object):
def __init__(self, tree_type, pos_x, pos_y):
super().__init__(player_damageable_group, enemy_damageable_group, all_sprites)
self.strength = 400
self.fireable = True
self.images = [[133, 200, tree_images[tree_type][1]],
[201, 400, tree_images[tree_type][0]],
[66, 132, tree_images[tree_type][2]],
[0, 65, tree_images[tree_type][3]]]
self.image = tree_images[tree_type][0]
board_width = random.randint(1, 35)
board_height = random.randint(1, 35)
self.rect = self.image.get_rect().move(tile_width * pos_x + board_width,
tile_height * pos_y + board_height)
# Класс игрока
class Player(Object):
def __init__(self, num, spawnpoint, level=1):
global ENEMIES_LEFT
super().__init__(player_group, enemy_damageable_group, all_sprites)
self.num = num
self.level = level
self.spawnpoint = spawnpoint
self.xp = 0
self.angle = 0
self.shoot_delay = 0
self.game_delay = -1
self.strength = PLAYER_HP[self.level]
self.moving = False
self.is_update_images = False
self.update_image()
self.rect = self.image.get_rect().move(tile_width * spawnpoint[0],
tile_height * spawnpoint[1])
if len(pygame.sprite.spritecollide(self, player_group, False)) > 1:
if self.num == 1:
player_two.kill()
else:
player_one.kill()
ENEMIES_LEFT -= len(pygame.sprite.spritecollide(self, enemy_group, True))
def update(self):
super().update()
if self.game_delay > 0:
self.game_delay -= 1
return
if self.game_delay == 0:
self.killall()
self.update_level()
self.update_image()
self.shoot_delay -= 1
if not self.moving:
return
delta_x = 0
delta_y = 0
if self.angle == 0:
delta_y -= VELOCITY
elif self.angle == 1:
delta_x += VELOCITY
elif self.angle == 2:
delta_y += VELOCITY
else:
delta_x -= VELOCITY
self.rect = self.rect.move(delta_x, delta_y)
res = pygame.sprite.spritecollide(self, player_damageable_group, False)
for obj in res:
if type(obj) != Enemy:
obj.damage(PLAYER_DAMAGE[self.level])
condition = (pygame.sprite.spritecollideany(self, solid_group, False) or
pygame.sprite.spritecollideany(self, player_damageable_group, False) or
len(pygame.sprite.spritecollide(self, player_group, False)) > 1)
if condition:
self.rect = self.rect.move(-delta_x, -delta_y)
self.moving = False
def update_level(self):
if self.level == len(PLAYER_LEVELS_LIMIT) - 1:
return
if self.xp >= PLAYER_LEVELS_LIMIT[self.level + 1]:
self.xp = 0
self.level += 1
self.update_image()
def update_image(self):
if self.num == 1:
images = player_one_images
else:
images = player_two_images
self.image = images[self.level][self.angle]
def shoot(self):
if self.shoot_delay <= 0:
tank_shot.play()
Shot(self)
self.shoot_delay = 30
def kill(self):
if self.game_delay == -1:
self.game_delay = 120
self.image = null_image
player_group.remove(self)
enemy_damageable_group.remove(self)
def killall(self):
global player_one, player_two
if self.num == 1:
player_one = Player(1, self.spawnpoint)
player = player_one
else:
player_two = Player(2, self.spawnpoint)
player = player_two
player_group.add(player)
enemy_damageable_group.add(player)
super().kill()
# Класс врага
class Enemy(Object):
def __init__(self, pos_x, pos_y, player, level=1):
super().__init__(enemy_group, player_damageable_group, all_sprites)
self.level = level
self.angle = 0
self.moving = False
self.is_update_images = False
self.strength = ENEMY_HP[level]
self.player = player
self.update_image()
self.rect = self.image.get_rect().move(tile_width * pos_x, tile_height * pos_y)
self.delay = 10
self.shoot_delay = 0
pygame.sprite.spritecollide(self, player_group, True)
def update(self):
super().update()
self.update_image()
self.move()
self.shoot()
self.shoot_delay -= 1
if self.delay > 0:
self.delay -= 1
return
self.moving = False
if random.randint(1, 5) == 1:
self.delay = random.randint(10, 20)
return
dx, dy = self.get_dist()
angles = [0, 1, 2, 3]
if abs(dx) > MAXDIST:
if dx > 0:
angles.remove(1)
else:
angles.remove(3)
if abs(dy) > MAXDIST:
if dy > 0:
angles.remove(2)
else:
angles.remove(0)
self.delay = random.randint(10, 30)
self.angle = random.choice(angles)
self.moving = True
def get_dist(self):
dx = self.rect.x - self.player.rect.x
dy = self.rect.y - self.player.rect.y
return dx, dy
def move(self):
if not self.moving:
return
delta_x = 0
delta_y = 0
if self.angle == 0:
delta_y -= VELOCITY
elif self.angle == 1:
delta_x += VELOCITY
elif self.angle == 2:
delta_y += VELOCITY
else:
delta_x -= VELOCITY
condition = None
if len(pygame.sprite.spritecollide(self, enemy_group, False)) > 1:
condition = False
self.rect = self.rect.move(delta_x, delta_y)
res = pygame.sprite.spritecollide(self, enemy_damageable_group, False)
for obj in res:
if type(obj) != Player:
obj.damage(ENEMY_DAMAGE[self.level])
if condition is None:
condition = len(pygame.sprite.spritecollide(self, enemy_group, False)) > 1
condition = (condition or pygame.sprite.spritecollideany(self, solid_group, False) or
pygame.sprite.spritecollideany(self, enemy_damageable_group, False))
if condition:
self.rect = self.rect.move(-delta_x, -delta_y)
def update_image(self):
images = enemy_images
self.image = images[self.level][self.angle]
def shoot(self):
if self.shoot_delay <= 0:
tank_shot.play()
Shot(self)
self.shoot_delay = random.randint(30, 120)
# Анимированный спрайт
class AnimatedSprite(pygame.sprite.Sprite):
def __init__(self, *groups):
super().__init__(list(groups))
self.images = []
self.cur_image = -1
self.cadres = 1
self.delay = 0
self.lifetime = float("inf")
self.image = None
self.rect = None
def update(self):
if self.delay > 0:
self.delay -= 1
return