-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunar_lander.py
880 lines (780 loc) · 35.5 KB
/
lunar_lander.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
# Import and initialize needed modules
import pygame
import os
import random
pygame.init()
pygame.font.init()
# Set the screen and caption
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Lunar Lander")
# Set the different fonts to be used
small_font = pygame.font.SysFont("courier", 15)
medium_font = pygame.font.SysFont("courier", 20)
large_font = pygame.font.SysFont("courier", 50)
extra_large_font = pygame.font.SysFont("courier", 100)
# Set the non-object images to be used
current_path = os.path.dirname(__file__)
background = pygame.image.load(os.path.join(current_path, "images\\background.png"))
explosion_medium = pygame.image.load(
os.path.join(current_path, "images\explosion_medium.png")
)
in_air_explosion_large = pygame.image.load(
os.path.join(current_path, "images\in_air_explosion_large.png")
)
player_thrusting = pygame.image.load(
os.path.join(current_path, "images\player_thrusting.png")
)
# Define needed variables
main_loop = True
start_loop = True
outer_game_loop = True
pre_game_loop = True
game_loop = True
level_win_loop = True
end_loop = True
overall_win = False
level = 1
score = 0
high_score = 0
# Define some text displays so they're available under all conditions
score_text = medium_font.render("Score: " + str(score), False, (255, 255, 255))
overall_win_text = large_font.render("ALL MISSIONS COMPLETE", False, (0, 255, 0))
# Define the game objects class
class GameObject:
def __init__(self, x, y, x_speed, y_speed, name):
self.image = pygame.image.load(
os.path.join(current_path, "images\\" + name + ".png")
)
self.x = x
self.y = y
self.x_speed = x_speed
self.y_speed = y_speed
self.name = name
self.rect = self.image.get_rect()
# Change the x and y values of the object
def move(self):
self.x += self.x_speed
self.y += self.y_speed
# Move the object's rect and display its image
def display(self):
self.rect = self.image.get_rect()
self.rect.move_ip(int(self.x), int(self.y))
screen.blit(self.image, (int(self.x), int(self.y)))
def collided_with(self, other_object):
return self.rect.colliderect(other_object.rect)
# Define functions to cut down on the number of lines and make the loops easier to organize.
# Display anything at any location on the screen
def display(image, x, y):
screen.blit(image, (int(x), int(y)))
# Display the score based on how large it is
def display_score():
if 0 <= score < 10:
display(score_text, 686, 5)
if 10 <= score < 1000:
display(score_text, 660, 5)
if 1000 <= score < 10000:
display(score_text, 652, 5)
if 10000 <= score:
display(score_text, 639, 5)
# Display the level based on how large the number is
def display_level():
if 1 <= level < 10:
display(level_display, 700, 30)
if 10 <= level:
display(level_display, 688, 30)
# Display the text display
def display_text():
display(altitude, 10, 5)
display(display_fuel, 10, 30)
display(v_display, 10, 55)
display(h_display, 10, 80)
display_level()
display_score()
# Run the start screen, game, and end screen loops
while main_loop:
# Make the game loops run
outer_game_loop = True
pre_game_loop = True
game_loop = True
overall_win = False
# Show the start screen
while start_loop:
# Reset the score to 0
score = 0
# Display the background images on the screen
screen.fill((100, 100, 100))
display(background, 0, -524)
# Set the text displays on the start screen
lunar_lander_text = extra_large_font.render(
"LUNAR LANDER", False, (255, 255, 255)
)
high_score_text = medium_font.render(
"High Score: " + str(high_score), False, (255, 255, 255)
)
enter_text = small_font.render("Press enter to play.", False, (255, 255, 255))
overall_win_text = large_font.render(
"ALL MISSIONS COMPLETE", False, (0, 255, 0)
)
# Display the end screen text
display(lunar_lander_text, 35, 190)
display(high_score_text, 310, 170)
display(enter_text, 310, 300)
# Look for inputs from the keyboard or mouse
for event in pygame.event.get():
# Stop each loop to end the program when the user clicks the X in the top-right corner
if event.type == pygame.QUIT:
start_loop = False
outer_game_loop = False
end_loop = False
main_loop = False
# Start the game when the user presses enter
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
start_loop = False
# Update the display
pygame.display.update()
# Put the user at level 1
level = 1
# Run the game
while outer_game_loop:
# Make the game loop run for this level
game_loop = True
# Reset the environmental objects lists to only include objects in this level
env_objects = []
falling_objects = []
# Define the player and target
player = GameObject(380, -30, 0, 1, "player")
target = GameObject(random.randint(0, 736), 513, 0, 0, "target")
player_y_acceleration = 0.05
player_x_acceleration = 0
fuel_value = 100
# Base the level's setup on what level it is
if level == 1:
# Define the environmental objects
dish1 = GameObject(150, 505, 0, 0, "dish_large")
dish2 = GameObject(550, 537, 0, 0, "dish")
dish3 = GameObject(650, 537, 0, 0, "dish")
# Add the environmental objects to the main collision list
env_objects.append(dish1)
env_objects.append(dish2)
env_objects.append(dish3)
# Define the target
target = GameObject(350, 513, 0, 0, "target")
# Define the player
player = GameObject(360, -30, 0, 0.5, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 2:
# Define the environmental objects
dish1 = GameObject(650, 505, 0, 0, "dish_large")
rover1 = GameObject(350, 505, -0.25, 0, "rover_one")
# Add the environmental objects to the main collision list
env_objects.append(dish1)
env_objects.append(rover1)
# Define the target
target = GameObject(500, 513, 0, 0, "target")
# Define the player
player = GameObject(360, -30, 0, 0.75, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 3:
# Define the environmental objects
rocket1 = GameObject(150, 505, 0, 0, "rocket")
lander1 = GameObject(600, -30, 0, 1, "other_lander")
dish1 = GameObject(450, 537, 0, 0, "dish")
# Add the environmental objects to the main collision list
env_objects.append(rocket1)
env_objects.append(lander1)
env_objects.append(dish1)
# Add falling environmental objects to the ground collision list
falling_objects.append(lander1)
# Define the target
target = GameObject(250, 513, 0, 0, "target")
# Define the player
player = GameObject(360, -30, 0, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 4:
# Define the environmental objects
rocket1 = GameObject(200, 505, 0, 0, "rocket")
rocket2 = GameObject(50, 505, 0, 0, "rocket")
rover1 = GameObject(350, 505, 0.25, 0, "rover_one")
lander1 = GameObject(350, -30, 0, 1, "other_lander")
# Add the environmental objects to the main collision list
env_objects.append(rocket1)
env_objects.append(rocket2)
env_objects.append(rover1)
env_objects.append(lander1)
# Add falling environmental objects to the ground collision list
falling_objects.append(lander1)
# Define the target
target = GameObject(115, 513, 0, 0, "target")
# Define the player
player = GameObject(500, -30, 0, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 5:
# Define the environmental objects
rocket1 = GameObject(700, 505, 0, 0, "rocket")
lander1 = GameObject(200, -80, 0, 1, "other_lander")
meteor1 = GameObject(500, -330, 0, 1.2, "meteor_small")
meteor2 = GameObject(400, -730, 0, 1.5, "meteor_small")
meteor3 = GameObject(675, -1030, 0, 1.5, "meteor_small")
meteor4 = GameObject(150, -730, 0, 1, "meteor_small")
dish1 = GameObject(300, 537, 0, 0, "dish")
dish2 = GameObject(50, 505, 0, 0, "dish_large")
# Add the environmental objects to the main collision list
env_objects.append(rocket1)
env_objects.append(lander1)
env_objects.append(meteor1)
env_objects.append(meteor2)
env_objects.append(meteor3)
env_objects.append(meteor4)
env_objects.append(dish1)
env_objects.append(dish2)
# Add falling environmental objects to the ground collision list
falling_objects.append(lander1)
falling_objects.append(meteor1)
falling_objects.append(meteor2)
falling_objects.append(meteor3)
falling_objects.append(meteor4)
# Define the target
target = GameObject(600, 513, 0, 0, "target")
# Define the player
player = GameObject(300, -30, 0, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 6:
# Define the environmental objects
rocket1 = GameObject(25, 505, 0, 0, "rocket_construction")
rocket2 = GameObject(175, 505, 0, 0, "rocket_construction")
rocket3 = GameObject(250, 505, 0, 0, "rocket_construction")
rocket4 = GameObject(325, 505, 0, 0, "rocket_construction")
rocket5 = GameObject(400, 505, 0, 0, "rocket_construction")
rocket6 = GameObject(475, 505, 0, 0, "rocket_construction")
rocket7 = GameObject(550, 505, 0, 0, "rocket_construction")
rocket8 = GameObject(625, 505, 0, 0, "rocket_construction")
rocket9 = GameObject(700, 505, 0, 0, "rocket_construction")
satellite1 = GameObject(900, 100, -1, 0, "satellite_one")
satellite2 = GameObject(-500, 300, 1, 0, "satellite_three")
satellite3 = GameObject(-150, 200, 1, 0, "satellite_two")
satellite4 = GameObject(-800, 450, 1.25, 0, "satellite_one")
satellite5 = GameObject(1300, 400, -1, 0, "satellite_three")
# Add the environmental objects to the main collision list
env_objects.append(rocket1)
env_objects.append(rocket2)
env_objects.append(rocket3)
env_objects.append(rocket4)
env_objects.append(rocket5)
env_objects.append(rocket6)
env_objects.append(rocket7)
env_objects.append(rocket8)
env_objects.append(rocket9)
env_objects.append(satellite1)
env_objects.append(satellite2)
env_objects.append(satellite3)
env_objects.append(satellite4)
env_objects.append(satellite5)
# Add falling environmental objects to the ground collision list
falling_objects.append(satellite1)
falling_objects.append(satellite2)
falling_objects.append(satellite3)
falling_objects.append(satellite4)
falling_objects.append(satellite5)
# Define the target
target = GameObject(100, 513, 0, 0, "target")
# Define the player
player = GameObject(360, -30, 0, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 7:
# Define the environmental objects
dish1 = GameObject(330, 505, 0, 0, "dish_large")
dish2 = GameObject(460, 505, 0, 0, "dish_large")
rover1 = GameObject(200, 505, -0.25, 0, "rover_one")
rover2 = GameObject(600, 505, 0.1, 0, "rover_two")
# Add the environmental objects to the main collision list
env_objects.append(dish1)
env_objects.append(dish2)
env_objects.append(rover1)
env_objects.append(rover2)
# Define the target
target = GameObject(400, 513, 0, 0, "target")
# Define the player
player = GameObject(300, -30, -0.25, 1.5, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 8:
# Define the environmental objects
lander1 = GameObject(75, -100, 0, 1, "other_lander")
lander2 = GameObject(275, -300, 0, 1, "other_lander")
lander3 = GameObject(400, -150, 0, 1, "other_lander")
lander4 = GameObject(650, -400, 0, 1, "other_lander")
# Add the environmental objects to the main collision list
env_objects.append(lander1)
env_objects.append(lander2)
env_objects.append(lander3)
env_objects.append(lander4)
# Add falling environmental objects to the ground collision list
falling_objects.append(lander1)
falling_objects.append(lander2)
falling_objects.append(lander3)
falling_objects.append(lander4)
# Define the target
target = GameObject(500, 513, 0, 0, "target")
# Define the player
player = GameObject(200, -30, 0, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 100
if level == 9:
# Define the environmental objects
dish1 = GameObject(550, 537, 0, 0, "dish")
dish2 = GameObject(675, 537, 0, 0, "dish")
meteor1 = GameObject(25, -50, 0, 1, "meteor_large")
meteor2 = GameObject(100, -350, 0, 1, "meteor_large")
meteor3 = GameObject(175, -575, 0, 1, "meteor_large")
meteor4 = GameObject(250, -275, 0, 1, "meteor_large")
meteor5 = GameObject(325, -450, 0, 1, "meteor_large")
meteor6 = GameObject(400, -650, 0, 1, "meteor_large")
meteor7 = GameObject(400, -100, 0, 1, "meteor_large")
meteor8 = GameObject(450, -300, 0, 1, "meteor_large")
meteor9 = GameObject(500, -600, 0, 1, "meteor_large")
meteor10 = GameObject(750, -500, 0, 1, "meteor_large")
# Add the environmental objects to the main collision list
env_objects.append(dish1)
env_objects.append(dish2)
env_objects.append(meteor1)
env_objects.append(meteor2)
env_objects.append(meteor3)
env_objects.append(meteor4)
env_objects.append(meteor5)
env_objects.append(meteor6)
env_objects.append(meteor7)
env_objects.append(meteor8)
env_objects.append(meteor9)
env_objects.append(meteor10)
# Add falling environmental objects to the ground collision list
falling_objects.append(meteor1)
falling_objects.append(meteor2)
falling_objects.append(meteor3)
falling_objects.append(meteor4)
falling_objects.append(meteor5)
falling_objects.append(meteor6)
falling_objects.append(meteor7)
falling_objects.append(meteor8)
falling_objects.append(meteor9)
falling_objects.append(meteor10)
# Define the target
target = GameObject(600, 513, 0, 0, "target")
# Define the player
player = GameObject(100, -30, 0.25, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 75
if level == 10:
# Define the environmental objects
rocket1 = GameObject(700, 505, 0, 0, "rocket")
meteor0 = GameObject(600, -50, 0, 0.5, "meteor_large")
meteor1 = GameObject(0, -133, 0, 1, "meteor_large")
meteor2 = GameObject(33, -166, 0, 1, "meteor_large")
meteor3 = GameObject(66, -200, 0, 1, "meteor_large")
meteor4 = GameObject(100, -233, 0, 1, "meteor_large")
meteor5 = GameObject(133, -266, 0, 1, "meteor_large")
meteor6 = GameObject(166, -300, 0, 1, "meteor_large")
meteor7 = GameObject(200, -333, 0, 1, "meteor_large")
meteor8 = GameObject(233, -366, 0, 1, "meteor_large")
meteor9 = GameObject(266, -400, 0, 1, "meteor_large")
meteor10 = GameObject(300, -433, 0, 1, "meteor_large")
meteor11 = GameObject(333, -466, 0, 1, "meteor_large")
meteor12 = GameObject(366, -500, 0, 1, "meteor_large")
meteor13 = GameObject(400, -533, 0, 1, "meteor_large")
meteor14 = GameObject(433, -566, 0, 1, "meteor_large")
meteor15 = GameObject(466, -600, 0, 1, "meteor_large")
meteor16 = GameObject(500, -633, 0, 1, "meteor_large")
meteor17 = GameObject(533, -666, 0, 1, "meteor_large")
meteor18 = GameObject(566, -700, 0, 1, "meteor_large")
meteor19 = GameObject(600, -733, 0, 1, "meteor_large")
meteor20 = GameObject(633, -766, 0, 1, "meteor_large")
meteor21 = GameObject(666, -800, 0, 1, "meteor_large")
meteor22 = GameObject(700, -833, 0, 1, "meteor_large")
meteor23 = GameObject(733, -866, 0, 1, "meteor_large")
meteor24 = GameObject(766, -900, 0, 1, "meteor_large")
meteor25 = GameObject(800, -933, 0, 1, "meteor_large")
# Add the environmental objects to the main collision list
env_objects.append(rocket1)
env_objects.append(meteor0)
env_objects.append(meteor1)
env_objects.append(meteor2)
env_objects.append(meteor3)
env_objects.append(meteor4)
env_objects.append(meteor5)
env_objects.append(meteor6)
env_objects.append(meteor7)
env_objects.append(meteor8)
env_objects.append(meteor9)
env_objects.append(meteor10)
env_objects.append(meteor11)
env_objects.append(meteor12)
env_objects.append(meteor13)
env_objects.append(meteor14)
env_objects.append(meteor15)
env_objects.append(meteor16)
env_objects.append(meteor17)
env_objects.append(meteor18)
env_objects.append(meteor19)
env_objects.append(meteor20)
env_objects.append(meteor21)
env_objects.append(meteor22)
env_objects.append(meteor23)
env_objects.append(meteor24)
env_objects.append(meteor25)
# Add falling environmental objects to the ground collision list
falling_objects.append(meteor0)
falling_objects.append(meteor1)
falling_objects.append(meteor2)
falling_objects.append(meteor3)
falling_objects.append(meteor4)
falling_objects.append(meteor5)
falling_objects.append(meteor6)
falling_objects.append(meteor7)
falling_objects.append(meteor8)
falling_objects.append(meteor9)
falling_objects.append(meteor10)
falling_objects.append(meteor11)
falling_objects.append(meteor12)
falling_objects.append(meteor13)
falling_objects.append(meteor14)
falling_objects.append(meteor15)
falling_objects.append(meteor16)
falling_objects.append(meteor17)
falling_objects.append(meteor18)
falling_objects.append(meteor19)
falling_objects.append(meteor20)
falling_objects.append(meteor21)
falling_objects.append(meteor22)
falling_objects.append(meteor23)
falling_objects.append(meteor24)
falling_objects.append(meteor25)
# Define the target
target = GameObject(742, 513, 0, 0, "target")
# Define the player
player = GameObject(-30, -30, 0.5, 1, "player")
player_x_acceleration = 0
player_y_acceleration = 0.005
fuel_value = 50
# Set the text displays on the game screen
score_text = medium_font.render("Score: " + str(score), False, (255, 255, 255))
initial_level_display = large_font.render(
"Level " + str(level), False, (255, 255, 255)
)
enter_text = small_font.render("Press enter to play.", False, (255, 255, 255))
level_display = medium_font.render(
"Level " + str(level), False, (255, 255, 255)
)
v_display = medium_font.render(
"Descent: " + str(round(player.y_speed, 1)) + "m/s", False, (0, 255, 0)
)
h_display = medium_font.render(
"Speed: " + str(round(abs(player.x_speed), 1)) + "m/s", False, (0, 255, 0)
)
display_fuel = medium_font.render(
"Fuel: " + str(int(fuel_value)) + "%", False, (0, 255, 0)
)
game_lose = large_font.render("MISSION FAILED", False, (255, 0, 0))
game_win = large_font.render("MISSION SUCCESS", False, (0, 255, 0))
# Display the background images on the screen
screen.fill((100, 100, 100))
display(background, 0, -524)
# Pre-level loop
while pre_game_loop:
# Update the enter text
enter_text = small_font.render(
"Press enter to start.", False, (255, 255, 255)
)
# Display the text on the screen
display(initial_level_display, 290, 200)
display(enter_text, 300, 260)
# Update the display
pygame.display.update()
# Look for inputs from the user
for event in pygame.event.get():
# Stop each loop to end the program when the user clicks the X in the top-right corner
if event.type == pygame.QUIT:
pre_game_loop = False
game_loop = False
outer_game_loop = False
end_loop = False
main_loop = False
# Start the game when the user presses enter
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
pre_game_loop = False
# Make the pre-game loop run for the next level
pre_game_loop = True
# Set the value for the explosion clearing timer
delay = 0
# Run the level
while game_loop:
# Display the background images on the screen
screen.fill((100, 100, 100))
display(background, 0, -524)
# Look for inputs from the user
for event in pygame.event.get():
# Stop each loop to end the program when the user clicks the X in the top-right corner
if event.type == pygame.QUIT:
game_loop = False
outer_game_loop = False
end_loop = False
main_loop = False
# Change the acceleration of the player when the user presses the arrow keys, if they have fuel
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and fuel_value > 0:
player_x_acceleration = -0.005
if event.key == pygame.K_RIGHT and fuel_value > 0:
player_x_acceleration = 0.005
if event.key == pygame.K_UP and fuel_value > 0:
player_y_acceleration = -0.005
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player_x_acceleration = 0
if event.key == pygame.K_UP:
player_y_acceleration = 0.005
# Use fuel when the player is accelerating with an arrow key
if player_y_acceleration != 0.005 or player_x_acceleration != 0:
fuel_value -= 0.1
if fuel_value <= 0:
player_y_acceleration = 0.005
player_x_acceleration = 0
# Change the player's speeds and location based on its acceleration and speed
player.x_speed += player_x_acceleration
player.y_speed += player_y_acceleration
player.move()
# Move and display the environmental objects
for anything in env_objects:
anything.move()
anything.display()
# Display the target
target.display()
# Update the altitude text display
altitude = medium_font.render(
"Altitude: " + str(int((530 - player.y))) + "m", False, (255, 255, 255)
)
if int(530 - player.y < 0):
altitude = medium_font.render("Altitude: 0m", False, (255, 255, 255))
# Update the fuel text display
display_fuel = medium_font.render(
"Fuel: " + str(int(fuel_value)) + "%", False, (0, 255, 0)
)
if fuel_value < 25:
display_fuel = medium_font.render(
"Fuel: " + str(int(fuel_value)) + "%", False, (255, 255, 0)
)
if fuel_value <= 0:
display_fuel = medium_font.render("Fuel: 0%", False, (255, 0, 0))
# Update the descent display
if round(player.y_speed, 1) < 1.5:
v_display = medium_font.render(
"Descent: " + str(round(player.y_speed, 1)) + "m/s",
False,
(0, 255, 0),
)
if 1.5 <= round(player.y_speed, 1) < 2:
v_display = medium_font.render(
"Descent: " + str(round(player.y_speed, 1)) + "m/s",
False,
(255, 255, 0),
)
if round(player.y_speed, 1) >= 2:
v_display = medium_font.render(
"Descent: " + str(round(player.y_speed, 1)) + "m/s TOO FAST",
False,
(255, 0, 0),
)
# Update the speed text display
if round(abs(player.x_speed), 1) < 0.7:
h_display = medium_font.render(
"Speed: " + str(round(abs(player.x_speed), 1)) + "m/s",
False,
(0, 255, 0),
)
if 0.7 <= round(abs(player.x_speed), 1) < 1:
h_display = medium_font.render(
"Speed: " + str(round(abs(player.x_speed), 1)) + "m/s",
False,
(255, 255, 0),
)
if round(abs(player.x_speed), 1) >= 1:
h_display = medium_font.render(
"Speed: " + str(round(abs(player.x_speed), 1)) + "m/s TOO FAST",
False,
(255, 0, 0),
)
# Display the text display
display_text()
# Display the player (either the thrusting or non-thrusting image)
if player_x_acceleration == 0 and player_y_acceleration == 0.005:
player.display()
else:
player.rect = player.image.get_rect()
player.rect.move_ip(int(player.x), int(player.y))
screen.blit(player_thrusting, (int(player.x), int(player.y)))
# Detect collisions between two env objects or between one env object and the player
for anything in env_objects:
if player.collided_with(anything):
# Display the explosion and the game_lose text, update the display, and end the game loop
display(in_air_explosion_large, player.x - 17, player.y - 18)
display(game_lose, 200, 200)
game_loop = False
outer_game_loop = False
end_loop = True
for anything_else in env_objects:
if (
anything.collided_with(anything_else)
and anything != anything_else
):
display(
in_air_explosion_large, anything.x - 16, anything.y - 16
)
anything.x_speed = 0
anything.y_speed = 0
anything_else.x_speed = 0
anything_else.y_speed = 0
delay += 1
if delay >= 100:
if anything in env_objects:
env_objects.remove(anything)
if anything in falling_objects:
falling_objects.remove(anything)
if anything_else in env_objects:
env_objects.remove(anything_else)
if anything_else in falling_objects:
falling_objects.remove(anything_else)
delay = 0
# Detect collisions between falling environmental objects and the ground
for anything in falling_objects:
if anything.y >= 530:
anything.x_speed = 0
anything.y_speed = 0
if (
anything.name == "meteor_small"
or anything.name == "meteor_large"
):
display(explosion_medium, anything.x - 16, anything.y - 30)
delay += 1
if delay >= 100 and (
anything.name == "meteor_small"
or anything.name == "meteor_large"
):
falling_objects.remove(anything)
env_objects.remove(anything)
delay = 0
# End the game loop when the player hits the ground
if player.y >= 530:
# Win if the player is touching the target and not going too fast
if (
player.collided_with(target) == 1
and round(player.y_speed, 1) < 2
and round(abs(player.x_speed), 1) < 1
):
# Give them a score based on their location, speeds, and remaining fuel
score += int(
1000
- 40 * abs(player.x - (target.x + 13))
+ 10 * fuel_value
+ 1000 * (2 - player.y_speed)
+ 1000 * (1 - abs(player.x_speed))
)
# Update the score text display
score_text = medium_font.render(
"Score: " + str(score), False, (255, 255, 255)
)
# Display the background images on the screen
screen.fill((100, 100, 100))
display(background, 0, -524)
# Display the text display
display_text()
# Set the high score
if score >= high_score:
high_score = score
# Update the enter text
enter_text = small_font.render(
"Press enter to go to the next level.", False, (255, 255, 255)
)
if level == 10:
level_win_loop = False
game_loop = False
outer_game_loop = False
end_loop = True
overall_win = True
# Level win loop
while level_win_loop:
# Display the target, player, game_win text, and update the display
display(game_win, 180, 200)
display(enter_text, 250, 270)
target.display()
player.display()
pygame.display.update()
# Look for inputs from the user
for event in pygame.event.get():
# Stop each loop to end the program when the user clicks the X in the top-right corner
if event.type == pygame.QUIT:
level_win_loop = False
game_loop = False
outer_game_loop = False
end_loop = False
main_loop = False
# End the level win loop when the user presses enter
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
level_win_loop = False
game_loop = False
# Make the level win loop run for the next level
level_win_loop = True
# End the level win loop and proceed to the next level
level += 1
# Lose in all other circumstances
else:
# Display the explosion, display the game_lose text, update the display, and end the game loop
display(explosion_medium, player.x - 17, player.y - 30)
display(game_lose, 200, 200)
game_loop = False
outer_game_loop = False
end_loop = True
# Update the display
pygame.display.update()
# Run the end screen until the player returns to the start screen, or closes the game window
while end_loop:
# Set the text displays on the end screen
high_score_text = medium_font.render(
"High Score: " + str(high_score), False, (255, 255, 255)
)
enter_text = small_font.render(
"Press enter to return to the start screen.", False, (255, 255, 255)
)
# Display the end screen text
if overall_win:
display(overall_win_text, 100, 200)
display(high_score_text, 300, 170)
display(score_text, 340, 260)
display(enter_text, 220, 300)
# Look for inputs from the keyboard or mouse
for event in pygame.event.get():
# Stop each loop to end the program when the user clicks the X in the top-right corner
if event.type == pygame.QUIT:
end_loop = False
main_loop = False
# Return the home screen when the user presses enter
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
end_loop = False
start_loop = True
# Update the display
pygame.display.update()