-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_1.py
955 lines (828 loc) · 41.6 KB
/
chapter_1.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
import arcade
import math
import random
import settings
# default window
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "WeFly X Charlie"
BULLET_SPEED = 2
Score = 0
INSTRUCTIONS_PAGE_0 = 0
INSTRUCTIONS_PAGE_1 = 1
GAME_RUNNING = 2
GAME_OVER = 3
WIN = 4
position_y_1 = 600
position_y_2 = 0
# default boss' properties
explode = 0
explode_x = 0
explode_y = 0
fps = 0
boss_create_fps = 0
level = 0
# boss level prompt
prompt = False
prompt_time = 0
boss_sound_on = 0
game_sound_on = 0
boss_hp = 0
boss_hp_current = 0
# default boss laser
laser_bomb = False
laser_effect = 0
laser_fps = 0
# Calculate the remaining missile
laser_counter = 0
laser_counter_update = 0
try:
background_sound = arcade.sound.load_sound("music/bgm_zhuxuanlv.mp3")
missile_sound_1 = arcade.load_sound("music/rocketswitch.wav")
hp_bonus_sound = arcade.load_sound("music/supply.wav")
button_sound = arcade.load_sound("music/button.wav")
bomb_sound = arcade.load_sound("music/all_bomb.wav")
game_sound = arcade.sound.load_sound("music/bgm_zhandou2.mp3.wav")
game_sound_1 = arcade.sound.load_sound("music/bgm_zhandou2.mp3.wav")
game_sound_2 = arcade.sound.load_sound("music/bgm_zhandou2.mp3.wav")
game_sound_3 = arcade.sound.load_sound("music/bgm_zhandou2.mp3.wav")
boss_sound_1 = arcade.sound.load_sound("music/boss_sound.wav")
boss_sound_2 = arcade.sound.load_sound("music/boss_sound.wav")
boss_sound_3 = arcade.sound.load_sound("music/boss_sound.wav")
boss_sound_4 = arcade.sound.load_sound("music/boss_sound.wav")
except Exception as e:
print("Error loading sound.", e)
class Enemy(arcade.Sprite):
# pass attribute to enemy
def __init__(self, image, scale, ehp, score, speed, boss):
"""
Initialize an enemy with information passed in.
:param image: enemy image
:param scale: enemy scale
:param ehp: enemy hit points
:param score: kill enemy score
:param speed: enemy speed
:param boss: enemy type, True when he is boss
"""
arcade.Sprite.__init__(self, image, scale)
self.ehp = ehp
self.score = score
self.speed = speed
self.boss = boss
self.left_boss = True
# self armo damage, hhp
def hitted(self, hhp):
"""
Enemy hit by self bullet. Return boss kill information and killed coordinates.
:param hhp: self bullet damage to the enemy
:return: Tuple, represents boss killed(1), otherwise(0); killed xy coordinates in order.
"""
global Score
self.ehp = max(0, self.ehp - hhp)
if self.ehp == 0:
self.kill()
Score += self.score
if self.boss:
return (1, self.center_x, self.center_y)
return (0, 0, 0)
def drop(self):
"""
Update enemy location
:return: None
"""
if self.boss and self.center_y <= 450:
if self.center_x <= 100:
self.left_boss = False
if self.center_x >= 700:
self.left_boss = True
if self.left_boss:
self.center_x -= 2
else:
self.center_x += 2
if self.center_x == 100:
self.left_boss = False
if self.center_x == 700:
self.left_boss = True
else:
self.center_y -= self.speed
if self.center_y < 0:
self.kill()
class Chapter1View(arcade.View):
def __init__(self):
super().__init__()
self.frame_count = 0
self.hp = 100
self.boss = False
self.laser_player = 0
self.enemy_list = None
self.bullet_list = None
self.bullet_self_list = None
self.player_list = None
self.player = None
self.assist = None
self.bonus = None
self.instructions = []
texture = arcade.load_texture("images/fm.jpeg")
self.instructions.append(texture)
texture = arcade.load_texture("images/intro.jpeg")
self.instructions.append(texture)
self.current_state = INSTRUCTIONS_PAGE_0
def setup(self):
"""
Initialize game interface. Default schedule is 60 fps.
:return: None
"""
self.frame_count = 0
self.hp = 100
self.boss = False
self.laser_player = 0
self.enemy_list = None
self.bullet_list = None
self.bullet_self_list = None
self.player_list = None
self.player = None
self.assist = None
self.bonus = None
arcade.schedule(self.on_update, 1 / 60)
self.enemy_list = arcade.SpriteList()
self.bullet_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bullet_self_list = arcade.SpriteList()
self.assist = arcade.SpriteList()
self.bonus = arcade.SpriteList()
# Add player ship
self.player = arcade.Sprite("images/SeHero.png", 0.6)
self.player_list.append(self.player)
# draw instruction page
def draw_instructions_page(self, page_number):
"""
Draw an instruction page. Load the page as an image.
"""
page_texture = self.instructions[page_number]
arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, page_texture.width,
page_texture.height,
page_texture, 0)
if self.current_state == INSTRUCTIONS_PAGE_0:
page_texture = arcade.load_texture("images/play.png")
arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, 200, page_texture.width, page_texture.height,
page_texture,
0)
# draw game over page
def draw_game_over(self):
"""
Draw "Game over" across the screen.
"""
output = "Game Over"
arcade.draw_text(output, 220, 350, arcade.color.WHITE, 54)
output = "Click anywhere to quit"
arcade.draw_text(output, 245, 260, arcade.color.WHITE, 24)
def draw_game_win(self):
texture = arcade.load_texture("images/win_page.jpeg")
arcade.draw_texture_rectangle(400, 300, 800, 600, texture)
def draw_game(self):
# Draw background and boss for each level
if level == 0:
texture_1 = arcade.load_texture("images/bg_0.jpg")
arcade.draw_texture_rectangle(400, position_y_1, 800, 600, texture_1)
texture_2 = arcade.load_texture("images/bg_0.jpg")
arcade.draw_texture_rectangle(400, position_y_2, 800, 600, texture_1)
texture_0 = arcade.load_texture("images/boss_2.png")
if level == 1:
texture_1 = arcade.load_texture("images/bg_new.jpg")
arcade.draw_texture_rectangle(400, position_y_1, 800, 600, texture_1)
texture_2 = arcade.load_texture("images/bg_new.jpg")
arcade.draw_texture_rectangle(400, position_y_2, 800, 600, texture_1)
texture_0 = arcade.load_texture("images/boss_4.png")
if level == 2:
texture_1 = arcade.load_texture("images/bg_1.jpg")
arcade.draw_texture_rectangle(400, position_y_1, 800, 600, texture_1)
texture_2 = arcade.load_texture("images/bg_1.jpg")
arcade.draw_texture_rectangle(400, position_y_2, 800, 600, texture_1)
texture_0 = arcade.load_texture("images/boss_1.png")
if level == 3:
texture_1 = arcade.load_texture("images/bg_new_1.jpg")
arcade.draw_texture_rectangle(400, position_y_1, 800, 600, texture_1)
texture_2 = arcade.load_texture("images/bg_new_1.jpg")
arcade.draw_texture_rectangle(400, position_y_2, 800, 600, texture_1)
texture_0 = arcade.load_texture("images/boss_5.png")
# draw images
self.enemy_list.draw()
self.bullet_list.draw()
self.player_list.draw()
self.bullet_self_list.draw()
self.assist.draw()
self.bonus.draw()
# boss killed explode animation
if explode == 1:
arcade.draw_texture_rectangle(explode_x, explode_y, 240, 180, texture_0)
texture_1 = arcade.load_texture("images/bigairplane3.png")
arcade.draw_texture_rectangle(explode_x, explode_y, 90, 90, texture_1)
elif explode == 2:
arcade.draw_texture_rectangle(explode_x, explode_y, 240, 180, texture_0)
texture_1 = arcade.load_texture("images/bigairplane4.png")
arcade.draw_texture_rectangle(explode_x, explode_y, 90, 90, texture_1)
elif explode == 3:
arcade.draw_texture_rectangle(explode_x, explode_y, 240, 180, texture_0)
texture_1 = arcade.load_texture("images/bigairplane5.png")
arcade.draw_texture_rectangle(explode_x, explode_y, 90, 90, texture_1)
elif explode == 4:
texture_0 = arcade.load_texture("images/bg_road.png")
arcade.draw_texture_rectangle(400, 300, 450, 430, texture_0)
# Draw different boss lasers
for b in self.enemy_list:
if level == 0:
if laser_effect == 1:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
elif laser_effect == 2:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
elif laser_effect == 3:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
elif laser_effect == 4:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
elif laser_effect == 5:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
elif laser_effect == 6:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
if level == 1:
if laser_effect == 1:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
elif laser_effect == 2:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
elif laser_effect == 3:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
elif laser_effect == 4:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
elif laser_effect == 5:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
elif laser_effect == 6:
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
if level == 2:
if laser_effect == 1:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
arcade.draw_texture_rectangle(b.center_x + 30, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
elif laser_effect == 2:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
arcade.draw_texture_rectangle(b.center_x + 30, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
elif laser_effect == 3:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
arcade.draw_texture_rectangle(b.center_x + 30, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
elif laser_effect == 4:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
arcade.draw_texture_rectangle(b.center_x + 30, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
elif laser_effect == 5:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
arcade.draw_texture_rectangle(b.center_x + 30, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
elif laser_effect == 6:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
arcade.draw_texture_rectangle(b.center_x + 30, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
if level == 3:
if laser_effect == 1:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
arcade.draw_texture_rectangle(b.center_x + 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser6.png"))
elif laser_effect == 2:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
arcade.draw_texture_rectangle(b.center_x + 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser7.png"))
elif laser_effect == 3:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
arcade.draw_texture_rectangle(b.center_x + 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser8.png"))
elif laser_effect == 4:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
arcade.draw_texture_rectangle(b.center_x + 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser9.png"))
elif laser_effect == 5:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
arcade.draw_texture_rectangle(b.center_x + 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser10.png"))
elif laser_effect == 6:
arcade.draw_texture_rectangle(b.center_x - 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
arcade.draw_texture_rectangle(b.center_x, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
arcade.draw_texture_rectangle(b.center_x + 40, b.center_y - 300, 30, 600,
arcade.load_texture("images/bomb_laser11.png"))
if prompt:
arcade.draw_texture_rectangle(400, 350, 300, 200, arcade.load_texture("images/boss_prompt.png"))
if self.boss:
arcade.draw_lrtb_rectangle_outline(300, 500, 580, 560, arcade.color.BLACK, 2)
arcade.draw_lrtb_rectangle_filled(302, 302 + (198 * boss_hp_current) // boss_hp, 578, 562,
arcade.color.RADICAL_RED)
# show hp, current score, and remaining laser times on the screen
arcade.draw_text("Score: {0:10.2f}".format(Score), 610, 560, arcade.color.WHITE, 12)
arcade.draw_lrtb_rectangle_outline(60, 170, 580, 560, arcade.color.WHITE, 2)
arcade.draw_lrtb_rectangle_filled(62, 62 + (106 * self.hp) // 100, 578, 562, arcade.color.WHITE)
arcade.draw_text("HP: {0:10.2f}%".format(self.hp), 180, 562, arcade.color.WHITE, 12)
if self.laser_player >= 1:
for i in range(self.laser_player):
arcade.draw_texture_rectangle(760 - i * 50, 520, 50, 40,
arcade.load_texture("images/missile_icon.png"))
def on_show(self):
arcade.set_background_color(arcade.color.BLUE_SAPPHIRE)
def dead(self):
"""
Clear the screen when dead
:return: None
"""
self.enemy_list = arcade.SpriteList()
self.bullet_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bullet_self_list = arcade.SpriteList()
self.current_state = GAME_OVER
def on_draw(self):
arcade.start_render()
# arcade.draw_text("Chapter 1", settings.WIDTH/2, settings.HEIGHT/2,
# arcade.color.BLACK, font_size=30, anchor_x="center")
# page_texture = arcade.load_texture("Icon-57.png")
# arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, 200, page_texture.width, page_texture.height, page_texture,
# 0)
#
# arcade.start_render()
if self.current_state == GAME_RUNNING:
self.draw_game()
elif self.current_state == INSTRUCTIONS_PAGE_0:
self.draw_instructions_page(0)
elif self.current_state == INSTRUCTIONS_PAGE_1:
self.draw_instructions_page(1)
elif self.current_state == GAME_OVER:
self.draw_game()
self.draw_game_over()
elif self.current_state == WIN:
self.draw_game_win()
def update(self, delta_time):
"""All the logic to move, and the game logic goes here. """
global explode, explode_x, explode_y, fps, position_y_1, position_y_2, level, prompt, prompt_time, boss_hp, boss_hp_current
global up_pressed, down_pressed, left_pressed, right_pressed, laser_bomb, laser_effect, laser_fps, laser_counter, laser_counter_update
global boss_create_fps, boss_sound_on, game_sound_on, game_sound_1, game_sound_2, game_sound_3, boss_sound_1, boss_sound_2, boss_sound_3, game_sound, boss_sound_4
if self.current_state != GAME_RUNNING and self.frame_count % 3480 == 0:
try:
arcade.play_sound(background_sound)
except Exception as e:
print("Error playing sound.", e)
pass
if self.current_state == GAME_RUNNING:
try:
arcade.stop_sound(background_sound)
except Exception as e:
print("Error pausing sound.", e)
pass
if level == 4:
self.current_state = WIN
return
if self.current_state == GAME_RUNNING:
if self.boss and boss_sound_on == 0:
boss_sound_on = 1
try:
if level == 0:
arcade.stop_sound(game_sound)
arcade.play_sound(boss_sound_1)
if level == 1:
game_sound_1.pause()
arcade.play_sound(boss_sound_2)
if level == 2:
game_sound_2.pause()
arcade.play_sound(boss_sound_3)
if level == 3:
game_sound_3.pause()
arcade.play_sound(boss_sound_4)
except Exception as e:
print("Error pausing sound.", e)
pass
if not self.boss:
try:
if level == 0:
boss_sound_1.pause()
if level == 1:
boss_sound_2.pause()
if level == 2:
boss_sound_3.pause()
if level == 3:
boss_sound_4.pause()
except Exception as e:
print("Error pausing sound.", e)
pass
boss_sound_on = 0
# if (self.frame_count - fps) == 180 and fps != 0:
# game_sound_on = 0
if game_sound_on == 0:
try:
if level == 0:
arcade.play_sound(game_sound)
if level == 1:
arcade.play_sound(game_sound_1)
if level == 2:
arcade.play_sound(game_sound_2)
if level == 3:
arcade.play_sound(game_sound_3)
except Exception as e:
print("Error playing sound.", e)
pass
game_sound_on = 1
# update remaining laser based on current score
laser_counter = Score // 1000 + 1
if laser_counter + laser_counter_update == 1:
arcade.play_sound(missile_sound_1)
self.laser_player += 1
laser_counter_update -= 1
if self.hp <= 0:
game_sound_on = 10
try:
arcade.stop_sound(game_sound)
# game_sound_1.pause()
# game_sound_2.pause()
# game_sound_3.pause()
# boss_sound_1.pause()
# boss_sound_2.pause()
# boss_sound_3.pause()
# boss_sound_4.pause()
except Exception as e:
print("Error pausing sound.", e)
self.dead()
else:
# drop hp bonus every 60s
if self.frame_count % 3600 == 3599:
bonus_hp = arcade.Sprite("images/hp_bonus.png", 0.45)
bonus_hp.center_x = random.randrange(0, SCREEN_WIDTH)
bonus_hp.center_y = random.randrange(SCREEN_HEIGHT, SCREEN_HEIGHT * 1.25)
self.bonus.append(bonus_hp)
if self.frame_count % 240 == 0 and not self.boss and not 1 <= explode <= 4:
for _ in range(2 + level):
# generate randomly enemy planes of different levels
ranNum = random.randint(0, 1000)
if ranNum < 500:
enemy = Enemy("images/plane_small.png", 0.8, 2, 10, 4, False)
elif ranNum < 850:
enemy = Enemy("images/bigplane0.png", 0.7, 3, 50, 3, False)
else:
enemy = Enemy("images/boss0.png", 0.35, 5, 100, 2, False)
enemy.center_x = random.randrange(0, SCREEN_WIDTH)
enemy.center_y = random.randrange(SCREEN_HEIGHT, SCREEN_HEIGHT * 1.25)
enemy.angle = 180
self.enemy_list.append(enemy)
# create a boss and ensure no small enemies appear during the boss battle
elif self.frame_count - fps == (1799 * (level + 1)) and not self.boss and not 1 <= explode <= 4:
# 提示
boss_create_fps = self.frame_count
prompt = True
prompt_time = self.frame_count
# update boss image based on game level
if level == 0:
enemy = Enemy("images/boss_2.png", 0.8, 25, 500, 2, True)
elif level == 1:
enemy = Enemy("images/boss_4.png", 0.8, 35, 1000, 3, True)
elif level == 2:
enemy = Enemy("images/boss_1.png", 0.8, 50, 2000, 3, True)
elif level == 3:
enemy = Enemy("images/boss_5.png", 0.8, 70, 4000, 3, True)
enemy.center_x = random.randrange(0, SCREEN_WIDTH)
enemy.center_y = SCREEN_HEIGHT * 2
enemy.angle = 180
self.enemy_list.append(enemy)
self.boss = True
boss_hp = enemy.ehp
# set time for boss prompt to be 3s
if self.frame_count - prompt_time == 180 and prompt:
prompt = False
# update player's hp based on different damage levels from boss
for boss in self.enemy_list:
if 1 <= laser_effect <= 6:
# realize the disappearance of self bullet when it hits boss
for e in self.bullet_self_list:
if boss.center_x - 20 <= e.center_x <= boss.center_x + 20:
e.kill()
# calculate different damage levels of laser from boss
if level == 0:
if self.player.center_x - 36 < boss.center_x < self.player.center_x + 36:
self.hp = max(0, self.hp - 0.8)
if level == 1:
if self.player.center_x - 36 < boss.center_x < self.player.center_x + 36:
self.hp = max(0, self.hp - 0.9)
if level == 2:
if self.player.center_x - 36 < boss.center_x - 45 < self.player.center_x + 36 or self.player.center_x - 36 < boss.center_x + 15 < self.player.center_x + 36:
self.hp = max(0, self.hp - 1)
if level == 3:
if self.player.center_x - 36 < boss.center_x - 45 < self.player.center_x + 36 or self.player.center_x - 36 < boss.center_x < self.player.center_x + 36 or self.player.center_x - 36 < boss.center_x + 15 < self.player.center_x + 36:
self.hp = max(0, self.hp - 1.1)
# update the background position
position_y_1 -= 1
position_y_2 -= 1
if position_y_1 == -300:
position_y_1 = 900
if position_y_2 == -300:
position_y_2 = 900
# collision with bullet
bullet_collide_list = arcade.check_for_collision_with_list(self.player, self.bullet_list)
for collide_bullet in bullet_collide_list:
collide_bullet.kill()
self.hp = max(0, self.hp - 5)
# collision with enemy
enemy_collide_list = arcade.check_for_collision_with_list(self.player, self.enemy_list)
for collide_enemy in enemy_collide_list:
collide_enemy.kill()
if self.boss:
self.hp = 0
self.hp = max(0, self.hp - 30)
# calculate different damage of player's bullet or bomb makes on enemy or boss
for e in self.enemy_list:
if e.boss:
boss_hp_current = e.ehp
bullet_hit_list = arcade.check_for_collision_with_list(e, self.bullet_self_list)
for bullet_hit in bullet_hit_list:
bullet_hit.kill()
boss_hit = e.hitted(1)
if boss_hit[0] == 1:
self.boss = False
explode = 1
explode_x = boss_hit[1]
explode_y = boss_hit[2]
fps = self.frame_count
for bomb in self.assist:
bullet_hit_list = arcade.check_for_collision_with_list(bomb, self.bullet_list)
for b in bullet_hit_list:
b.kill()
for e in self.enemy_list:
if e.boss:
boss_hp_current = e.ehp
bullet_hit_list = arcade.check_for_collision_with_list(e, self.assist)
for bullet_hit in bullet_hit_list:
boss_hit = e.hitted(0.3)
if boss_hit[0] == 1:
self.boss = False
explode = 1
explode_x = boss_hit[1]
explode_y = boss_hit[2]
fps = self.frame_count
# boss explode animation
if explode == 1 and self.frame_count - fps == 20:
arcade.play_sound(bomb_sound)
explode += 1
elif explode == 2 and self.frame_count - fps == 40:
explode += 1
elif explode == 3 and self.frame_count - fps == 60:
explode += 1
elif explode == 4 and self.frame_count - fps == 180:
explode += 1
level += 1
# bomb_sound.pause()
game_sound_on = 0
# use loop to make all enemies facing to the player
for enemy in self.enemy_list:
# First, calculate the angle to the player. We could do this
# only when the bullet fires, but in this case we will rotate
# the enemy to face the player each frame, so we'll do this
# each frame.
# Position the start at the enemy's current location
start_x = enemy.center_x
start_y = enemy.center_y
# list_1[i][2]Get the destination location for the bullet
dest_x = self.player.center_x
dest_y = self.player.center_y
# Do math to calculate how to get the bullet to the destination.
# Calculation the angle in radians between the start points
# and end points. This is the angle the bullet will travel.
x_diff = dest_x - start_x
y_diff = dest_y - start_y
angle = math.atan2(y_diff, x_diff)
# use if statement to exclude the boss angle
if enemy.boss:
enemy.angle = 0
else:
enemy.angle = math.degrees(angle) - 270
# determine the shooting characteristics of enemy / boss planes
if enemy.boss and self.frame_count % ((120 - 20 * level) // 2) == 0:
bullet = arcade.Sprite("images/boss_bullet.png", 0.5)
bullet.center_x = start_x
bullet.center_y = start_y
bullet.angle = 0
bullet.change_x = 0
bullet.change_y = - BULLET_SPEED * (level // 3 + 1)
self.bullet_list.append(bullet)
elif self.frame_count % (120 - 20 * level) == 0:
bullet = arcade.Sprite("images/enemy_bullet.png", 0.5)
bullet.center_x = start_x
bullet.center_y = start_y
bullet.angle = math.degrees(angle)
bullet.change_x = math.cos(angle) * BULLET_SPEED * (level // 3 + 1)
bullet.change_y = math.sin(angle) * BULLET_SPEED * (level // 3 + 1)
self.bullet_list.append(bullet)
# determine the shooting frequency of the player airplane
if self.frame_count % (15 - 2 * level) == 0:
bullet = arcade.Sprite("images/Bomb2.png", 0.7)
bullet.center_x = self.player.center_x
bullet.center_y = self.player.center_y
# Angle the bullet sprite
bullet.angle = 0
# Taking into account the angle, calculate our change_x
# and change_y. Velocity is how fast the bullet travels.
bullet.change_x = 0
bullet.change_y = BULLET_SPEED * 3
self.bullet_self_list.append(bullet)
# arcade.play_sound(bullet_sound)
# use loops to remove the bullet when it flies off-screen
for bullet in self.bullet_self_list:
if bullet.bottom > 600:
bullet.kill()
for bullet in self.assist:
if bullet.bottom > 600:
bullet.kill()
for bullet in self.bullet_list:
if bullet.top < 0:
bullet.kill()
# use loops to control the dropping of hp_bonus
for hp_bonus in self.bonus:
hp_bonus.center_y -= 5
# update player's hp when it catches hp_bonus
if arcade.check_for_collision(self.player, hp_bonus):
self.hp = min(100, self.hp + 30)
arcade.play_sound(hp_bonus_sound)
hp_bonus.kill()
# remove hp_bonus when it gets out of windows
if hp_bonus.top < 0:
hp_bonus.kill()
# keyboard control the movement of the player
if up_pressed:
self.player.center_y = min(552, self.player.center_y + 5)
if down_pressed:
self.player.center_y = max(48, self.player.center_y - 5)
if left_pressed:
self.player.center_x = max(36, self.player.center_x - 5)
if right_pressed:
self.player.center_x = min(764, self.player.center_x + 5)
# trigger the missile
if laser_bomb and self.laser_player > 0 and len(self.assist) <= 1:
assist_bomb = arcade.Sprite("images/assisent1_1.png", 1)
assist_bomb.center_x = self.player.center_x - 25
assist_bomb.center_y = self.player.center_y
assist_bomb.angle = 0
assist_bomb.change_x = 0
assist_bomb.change_y = 10
self.assist.append(assist_bomb)
assist_bomb = arcade.Sprite("images/assisent1_1.png", 1)
assist_bomb.center_x = self.player.center_x + 25
assist_bomb.center_y = self.player.center_y
assist_bomb.angle = 0
assist_bomb.change_x = 0
assist_bomb.change_y = 10
self.assist.append(assist_bomb)
self.laser_player -= 1
# use if statement to set the laser shooting period to be 8s
if self.boss and (self.frame_count - boss_create_fps) % 480 == 0 and (
self.frame_count - boss_create_fps) != 0:
laser_effect = 1
laser_fps = self.frame_count
# use if statement to animate laser
if laser_effect == 1 and self.frame_count - laser_fps == 20:
laser_effect += 1
elif laser_effect == 2 and self.frame_count - laser_fps == 40:
laser_effect += 1
elif laser_effect == 3 and self.frame_count - laser_fps == 60:
laser_effect += 1
elif laser_effect == 4 and self.frame_count - laser_fps == 80:
laser_effect += 1
elif laser_effect == 5 and self.frame_count - laser_fps == 100:
laser_effect += 1
elif laser_effect == 6 and self.frame_count - laser_fps == 120:
laser_effect += 1
# realize the dropping of boss and enemy planes
for e in self.enemy_list:
e.drop()
if level == 4:
self.current_state = WIN
self.set_mouse_visible(True)
self.bullet_list.update()
self.bullet_self_list.update()
self.assist.update()
# update the frame_count
self.frame_count += 1
# def on_key_press(self, key, modifiers):
# self.director.next_view()
def on_mouse_motion(self, x, y, delta_x, delta_y):
"""
Called whenever the mouse moves.
:param x: player x-location
:param y: player y-location
:param delta_x: player delta x
:param delta_y: player delta y
:return: None
"""
if self.current_state == GAME_RUNNING:
self.player.center_x = x
self.player.center_y = y
def on_mouse_press(self, x, y, button, modifiers):
global level, Score, prompt, prompt_time, boss_hp, boss_hp_current, laser_bomb, laser_effect, laser_fps, laser_counter, laser_counter_update
global game_sound_on
"""
Called when the user presses a mouse button.
"""
# Change states as needed.
if self.current_state == INSTRUCTIONS_PAGE_0 and x >= 280 and x <= 520 and y >= 102 and y <= 198:
arcade.play_sound(button_sound)
# Next page of instructions.
self.current_state = INSTRUCTIONS_PAGE_1
elif self.current_state == INSTRUCTIONS_PAGE_1:
# Start the game
self.current_state = GAME_RUNNING
self.setup()
elif self.current_state == GAME_OVER:
self.close()
# The addition of sound effect would mess up our page transfer
# Restart the game.
# level = 0
# Score = 0
# prompt = False
# prompt_time = 0
#
# boss_hp = 0
# boss_hp_current = 0
#
# laser_bomb = False
# laser_effect = 0
# laser_fps = 0
#
# laser_counter = 0
# laser_counter_update = 0
#
# self.setup()
# self.current_state = GAME_RUNNING
# game_sound_on = 0
elif self.current_state == WIN:
self.close()
# Restart the game.
# level = 0
# Score = 0
# prompt = False
# prompt_time = 0
#
# boss_hp = 0
# boss_hp_current = 0
#
# laser_bomb = False
# laser_effect = 0
# laser_fps = 0
#
# laser_counter = 0
# laser_counter_update = 0
# self.setup()
# self.current_state = GAME_RUNNING
# def on_mouse_press(self, x, y, button, modifiers):
# if x >= 280 and x <= 520 and y >= 102 and y <= 198:
# game_main()
# Variables to record if certain keys are being pressed.
up_pressed = False
down_pressed = False
left_pressed = False
right_pressed = False
if __name__ == "__main__":
"""This section of code will allow you to run your View
independently from the main.py file and its Director.
You can ignore this whole section. Keep it at the bottom
of your code.
It is advised you do not modify it unless you really know
what you are doing.
"""
from utils import FakeDirector
window = arcade.Window(settings.WIDTH, settings.HEIGHT)
my_view = Chapter1View()
my_view.director = FakeDirector(close_on_next_view=True)
window.show_view(my_view)
arcade.run()