-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
834 lines (741 loc) · 31.8 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
# importing necessary modules
import pygame
import math
import random
pygame.init()
# Creates a screen with width 800 and height 600
screen = pygame.display.set_mode((800, 600))
# Displaying the name on the window that it appears in and also
# Displaying an icon with it
pygame.display.set_caption("Plane Dropping Cargo Game")
icon = pygame.image.load("C:\\Users\\Kobi\\Documents\\Yaqub A level stuff\\CS NEA Development\\plane.png")
pygame.display.set_icon(icon)
# Creates a clock to control the frame rate
clock = pygame.time.Clock()
# making some colors
color_active = pygame.Color('orange')
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
# making some constants
GRAVITY = 9.81 # The acceleration due to gravity in m/s^2
SCALE = 10 # The scale factor to convert pixels to meters
# making some fonts
small_font = pygame.font.SysFont("Arial", 16)
large_font = pygame.font.SysFont("Arial", 32)
scoreboard_font = pygame.font.SysFont("Stencil Regular", 32)
# making some sounds
menu_sound = pygame.mixer.Sound("dangerzone.mp3")
drop_sound = pygame.mixer.Sound("drop.wav")
boom_sound = pygame.mixer.Sound("boom.wav")
button_sound = pygame.mixer.Sound("buttonsound.mp3")
# This will load and display the spirte images
plane_image = pygame.image.load("plane.png")
plane_image = pygame.transform.scale(plane_image, (150, 100))
cargo_image = pygame.image.load("cargo.png")
cargo_image = pygame.transform.scale(cargo_image, (25, 25))
truck_image = pygame.image.load("truck.png")
truck_image = pygame.transform.scale(truck_image, (120, 100))
cargofilled_image = pygame.image.load("cargofilled.png")
cargofilled_image = pygame.transform.scale(cargofilled_image, (160, 90))
# All the menu related images
play_button_image = pygame.image.load("playbutton.png")
play_button_image = pygame.transform.scale(play_button_image, (220, 90))
scoreboard_button_image = pygame.image.load("scoreboard.png")
scoreboard_button_image = pygame.transform.scale(scoreboard_button_image, (350, 90))
exit_button_image = pygame.image.load("exitbutton.png")
exit_button_image = pygame.transform.scale(exit_button_image, (190, 90))
title_image = pygame.image.load("title.png")
title_image = pygame.transform.scale(title_image, (400, 50))
resume_button_image = pygame.image.load("resumebutton.png")
resume_button_image = pygame.transform.scale(resume_button_image, (260, 63))
paused_title_image = pygame.image.load("pausetitle.png")
paused_title_image = pygame.transform.scale(paused_title_image, (400, 80))
background_image = pygame.image.load("backgroundmenu.png")
background_image = pygame.transform.scale(background_image, (700, 560))
settings_button_image = pygame.image.load("settingsbutton.png")
settings_button_image = pygame.transform.scale(settings_button_image, (260, 63))
settings_button_image1 = pygame.image.load("settingsbutton1.png")
settings_button_image1 = pygame.transform.scale(settings_button_image1, (300, 73))
settings_title_image = pygame.image.load("settingstitle.png")
settings_title_image = pygame.transform.scale(settings_title_image, (400, 90))
goback_button_image = pygame.image.load("gobackbutton.png")
goback_button_image = pygame.transform.scale(goback_button_image, (260, 63))
soundcontrol_button_image = pygame.image.load("soundcontrolbutton.png")
soundcontrol_button_image = pygame.transform.scale(soundcontrol_button_image, (300, 63))
howtoplay_button_image = pygame.image.load("howtoplaybutton.png")
howtoplay_button_image = pygame.transform.scale(howtoplay_button_image, (260, 63))
howtoplayscreen_image = pygame.image.load("howtoplayscreen.png")
howtoplayscreen_image = pygame.transform.scale(howtoplayscreen_image, (800, 600))
sound_title_image = pygame.image.load("soundtitle.png")
sound_title_image = pygame.transform.scale(sound_title_image, (430, 70))
sound_button_image = pygame.image.load("soundbutton.png")
sound_button_image = pygame.transform.scale(sound_button_image, (400, 120))
soundcontrolmute_button_image = pygame.image.load("mutesoundbutton.png")
soundcontrolmute_button_image = pygame.transform.scale(soundcontrolmute_button_image, (410, 130))
playernametitle_button_image = pygame.image.load("playernametitle.png")
playernametitle_button_image = pygame.transform.scale(playernametitle_button_image, (430, 70))
scoreboard_title_image = pygame.image.load("scoreboardtitle.png")
scoreboard_title_image = pygame.transform.scale(scoreboard_title_image, (430, 70))
# some global variables used in the functions below
menu = True
pause = False
settings = False
sound_settings = False
username_menu = False
scoreboard_menu = False
howtoplay_menu = False
x = 1
username = ""
score = 0
not_acceptable_name = "Too Long!"
mute_button_pressed = False
mute_state = False
menu_pause = False
menu_settings = False
# function for the how to play menu
def draw_howtoplay_menu():
# using the necessary global variables for this function
global howtoplay_menu
global pause
global settings
global sound_settings
# sets the screen background colour
screen.fill("blue3")
# blitting the images needed for this menu
screen.blit(howtoplayscreen_image, (0, 0))
goback_btn = pygame.draw.rect(screen, "dark gray", (300, 500, 200, 50))
screen.blit(goback_button_image, (279, 493))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if goback_btn.collidepoint(event.pos):
button_sound.play()
howtoplay_menu = False
settings = True
pygame.display.flip()
return menu
# function for the scoreboard menu
def draw_scoreboard_menu():
# using the necessary global variables for this function
global score
global x
global pause
global settings
global sound_settings
global menu_sound
global username
global scoreboard_menu
global menu
screen.fill("red")
screen.blit(scoreboard_title_image, (209, 40))
text_file = open("names.txt", "r")
def sort_by_score(line):
username, score = line.strip().split('-')
return int(score)
contents = text_file.readlines()
contents.sort(key=sort_by_score, reverse=True)
for i, line in enumerate(contents):
values = line.split()
username = values[0]
username_text = scoreboard_font.render(username, True, black)
screen.blit(username_text, (300, 150 + i * 30))
text_file.close()
goback_btn = pygame.draw.rect(screen, "dark gray", (300, 500, 200, 50))
screen.blit(goback_button_image, (279, 500 - 7))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if goback_btn.collidepoint(pygame.mouse.get_pos()):
button_sound.play()
scoreboard_menu = False
sound_settings = False
menu = True
pygame.display.flip()
return menu
# function for the username menu
def draw_username_menu():
# using the necessary global variables for this function
global menu
global username_menu
global username
global not_acceptable_name
global score
error = False
input_rect = pygame.Rect(260, 300, 300, 50)
colour_on = pygame.Color('lightskyblue3')
colour_off = pygame.Color('black')
color = colour_off
active = False
screen.fill("blue3")
screen.blit(background_image, (1, 50))
screen.blit(playernametitle_button_image, (209, 40))
start_btn = pygame.draw.rect(screen, "dark gray", (300, 200, 200, 50))
screen.blit(play_button_image, (289, 180))
goback_btn = pygame.draw.rect(screen, "dark gray", (300, 380, 200, 50))
screen.blit(goback_button_image, (279, 373))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if input_rect.collidepoint(event.pos):
print("username box has been clicked")
active = True
else:
active = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
username = username[:-1]
else:
username += event.unicode
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if start_btn.collidepoint(event.pos):
if 1 <= len(username) <= 10:
username = username
print("username:", username)
menu = False
username_menu = False
button_sound.play()
else:
error_text = large_font.render(not_acceptable_name, True, red)
screen.blit(error_text, (350, 345))
print("not acceptable name")
elif goback_btn.collidepoint(event.pos):
button_sound.play()
username_menu = False
menu = True
pygame.draw.rect(screen, color, input_rect)
text_surface = large_font.render(username, True, (255, 255, 255))
screen.blit(text_surface, (input_rect.x + 5, input_rect.y + 5))
input_rect.w = max(100, text_surface.get_width() + 10)
pygame.display.flip()
return menu
# function for the sound control menu
def draw_soundcontrol_menu():
# using the necessary global variables for this function
global x
global pause
global settings
global sound_settings
global menu_sound
global mute_state
global mute_button_pressed
screen.fill("blue3")
screen.blit(sound_title_image, (209, 40))
volumedown_btn = pygame.draw.rect(screen, "dark gray", (290, 235, 50, 50))
volumeup_btn = pygame.draw.rect(screen, "dark gray", (500, 235, 50, 50))
mute_btn = pygame.draw.rect(screen, "dark gray", (397, 235, 50, 50))
screen.blit(sound_button_image, (215, 200))
goback_btn = pygame.draw.rect(screen, "dark gray", (300, 380, 200, 50))
screen.blit(goback_button_image, (279, 373))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if volumedown_btn.collidepoint(pygame.mouse.get_pos()):
button_sound.play()
x = x - 0.2
menu_sound.set_volume(x)
elif volumeup_btn.collidepoint(pygame.mouse.get_pos()):
button_sound.play()
x = x + 0.2
menu_sound.set_volume(x)
elif mute_btn.collidepoint(pygame.mouse.get_pos()):
if mute_state:
print("game unmuted")
mute_state = False
pygame.mixer.unpause()
else:
print("game muted")
mute_state = True
pygame.mixer.pause()
mute_button_pressed = not mute_button_pressed
elif goback_btn.collidepoint(pygame.mouse.get_pos()):
button_sound.play()
sound_settings = False
if mute_state:
screen.blit(soundcontrolmute_button_image, (212, 195))
pygame.display.flip()
return menu
# function for the settings menu
def draw_settings_menu():
# using the necessary global variables for this function
global menu
global pause
global settings
global sound_settings
global howtoplay_menu
global menu_pause
global menu_settings
screen.fill("blue3")
screen.blit(settings_title_image, (209, 40))
howtoplay_btn = pygame.draw.rect(screen, "dark gray", (300, 200, 200, 50))
screen.blit(howtoplay_button_image, (279, 193))
soundcontrol_btn = pygame.draw.rect(screen, "dark gray", (300, 280, 160, 50))
screen.blit(soundcontrol_button_image, (264, 273))
goback_button_btn = pygame.draw.rect(screen, "dark gray", (300, 380, 200, 50))
screen.blit(goback_button_image, (279, 373))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if goback_button_btn.collidepoint(event.pos) and menu_settings == True:
button_sound.play()
menu = True
settings = False
elif goback_button_btn.collidepoint(event.pos) and menu_settings == False:
button_sound.play()
settings = False
elif soundcontrol_btn.collidepoint(event.pos):
button_sound.play()
sound_settings = True
elif howtoplay_btn.collidepoint(event.pos):
button_sound.play()
howtoplay_menu = True
pygame.display.flip()
return menu
# function for the pause menu
def draw_pause_menu():
# using the necessary global variables for this function
global menu
global pause
global settings
global score
global menu_pause
global menu_settings
screen.fill("blue3")
screen.blit(paused_title_image, (209, 40))
resume_btn = pygame.draw.rect(screen, "dark gray", (300, 200, 200, 50))
screen.blit(resume_button_image, (279, 193))
settings_btn = pygame.draw.rect(screen, "dark gray", (315, 280, 160, 50))
screen.blit(settings_button_image, (279, 273))
exit_btn = pygame.draw.rect(screen, "dark gray", (315, 370, 160, 50))
screen.blit(exit_button_image, (299, 350))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if resume_btn.collidepoint(event.pos):
button_sound.play()
pause = False
elif settings_btn.collidepoint(event.pos):
button_sound.play()
settings = True
draw_settings_menu()
elif exit_btn.collidepoint(event.pos):
text_file = open("names.txt", "a")
text_file.write(username + "-" + str(score) + "\n")
text_file.close()
score = 0
button_sound.play()
menu = True
pygame.display.flip()
return menu
# function for the main menu
def draw_menu():
# using the necessary global variables for this function
global menu
global username_menu
global scoreboard_menu
global pause
global settings
global menu_pause
global menu_settings
screen.fill("blue3")
screen.blit(background_image, (1, 50))
screen.blit(title_image, (209, 40))
start_btn = pygame.draw.rect(screen, "dark gray", (300, 200, 200, 50))
screen.blit(play_button_image, (289, 180))
scoreboard_btn = pygame.draw.rect(screen, "dark gray", (240, 300, 330, 50))
screen.blit(scoreboard_button_image, (230, 280))
settings_btn = pygame.draw.rect(screen, "dark gray", (290, 380, 160, 50))
screen.blit(settings_button_image1, (290 - 36, 373))
exit_btn = pygame.draw.rect(screen, "dark gray", (315, 470, 160, 50))
screen.blit(exit_button_image, (299, 450))
# Creating the outcome of each event for when the user decides to press any buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if start_btn.collidepoint(event.pos):
menu = False
username_menu = True
menu_settings = False
pause = False
draw_username_menu()
button_sound.play()
elif scoreboard_btn.collidepoint(event.pos):
menu = False
pause = False
scoreboard_menu = True
button_sound.play()
draw_scoreboard_menu()
elif settings_btn.collidepoint(event.pos):
menu = False
menu_settings = True
settings = True
button_sound.play()
elif exit_btn.collidepoint(event.pos):
pygame.quit()
pygame.display.flip()
return menu
# function for the main game loop which will call all the other functions
def main_game_loop():
# using the necessary global variables for this function
global last_entered_velocity, last_entered_height, angle, height, velocity, score, collision, game_over
global plane, cargo, truck, input_box, input_box2, input_box3
global menu
global pause
global settings
global score
global howtoplay_menu
running = True
menu_sound.play()
# Dealing with all the menu changes by calling the necessary functions
while running:
if menu:
menu = draw_menu()
elif pause:
draw_pause_menu()
while settings:
draw_settings_menu()
while sound_settings:
draw_soundcontrol_menu()
else:
draw_settings_menu()
while howtoplay_menu:
draw_howtoplay_menu()
elif scoreboard_menu:
draw_scoreboard_menu()
elif settings:
while settings:
draw_settings_menu()
while sound_settings:
draw_soundcontrol_menu()
else:
draw_settings_menu()
while howtoplay_menu:
draw_howtoplay_menu()
elif username_menu:
draw_username_menu()
else:
# Runs the main game
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handling all Input box events
input_box.handle_event(event)
input_box2.handle_event2(event)
input_box3.handle_event3(event)
# When the user presses down on the enter key for the cargo to be dropped
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
input_text = input_box.input_text.strip()
input_text2 = input_box2.input_text2.strip()
input_text3 = input_box3.input_text3.strip()
print("(REGISTERED BEFORE IF)Input for angle:", input_text2)
print("Input for velocity:", input_text)
if input_text:
last_entered_velocity = float(input_text)
velocity = float(input_text)
plane.vx = -velocity
else:
print("(NOT REGISTERED IN IF)Velocity input is empty!")
if input_text2:
last_entered_angle = float(input_text2)
plane.angle = last_entered_angle
angle = plane.angle
print("(REGISTERED)This is the user angle", plane.angle)
print("(REGISTERED)this is the actual angle", angle)
else:
print("(NOT REGISTERED IN IF)Angle input is empty!")
if input_text3:
last_entered_height = float(input_text3)
plane.rect.y = (600 - last_entered_height) - 90
else:
print("(NOT REGISTERED IN IF)Height input is empty!")
if not plane.dropped:
drop_sound.play()
cargo.rect.x = plane.rect.x + 80
cargo.rect.y = plane.rect.y + 62
cargo.vx = plane.vx + velocity * math.cos(math.radians(angle))
print("This is the angle of the cargo currently:", angle)
cargo.vy = plane.vy + velocity * math.sin(math.radians(angle))
cargo.in_air = True
plane.dropped = True
plane.moving = True
# When the user presses the r key to reset the location of the cargo truck
if event.key == pygame.K_r:
game_over = False
score = score
truck.image = truck_image
truck.rect.x = random.randint(100, 700)
truck.rect.y = 520
# When the user presses the esc key to enter the pause menu
elif event.key == pygame.K_ESCAPE:
print("Escape pressed")
pause = True
draw_pause_menu()
# Sets the cargo location to be dropped from the part of the plane it was held in
elif event.type == pygame.KEYUP:
cargo.rect.x = plane.rect.x + 80
cargo.rect.y = plane.rect.y + 62
# Updating the sprites
sprites.update()
# Check if the cargo has hit the truck
if pygame.sprite.collide_rect(cargo, truck):
score += 10
truck.rect.x = random.randint(100, 700)
truck.rect.y = 520
if truck.image == cargofilled_image:
collision = True
# Clear the screen
screen.fill(white)
screen.fill("lightblue1")
# Draw the sprites
sprites.draw(screen)
# Making the text
angle_text = small_font.render("Angle: " + str(angle) + "°", True, black)
height_text = small_font.render("Height: " + str(last_entered_height) + " m", True, black)
velocity_text = small_font.render("Initial Velocity: " + str(last_entered_velocity) + " m/s", True, black)
score_text = large_font.render("Score: " + str(score), True, black)
screen.blit(angle_text, (10, 10))
screen.blit(height_text, (10, 30))
screen.blit(velocity_text, (10, 50))
screen.blit(score_text, (600, 10))
velocity_input_text = small_font.render("Input u: ", True, black)
screen.blit(velocity_input_text, (205, 10))
angle_input_text = small_font.render("Input θ: ", True, black)
screen.blit(angle_input_text, (205, 35))
height_input_text = small_font.render("Input h: ", True, black)
screen.blit(height_input_text, (205, 60))
heightcheck_input_text = small_font.render("0<h<550 ", True, black)
screen.blit(heightcheck_input_text, (315, 60))
anglecheck_input_text = small_font.render("90<θ<180 ", True, black)
screen.blit(anglecheck_input_text, (315, 35))
# Drawing the game over text
show_text = pygame.USEREVENT + 1
# In your game loop
if collision:
point_awarded = True
if point_awarded:
game_over_text = large_font.render("10pts!", True, green)
# Set a timer for the custom event (2000 milliseconds = 2 seconds)
pygame.time.set_timer(show_text, 2000)
point_awarded = False
# In your event handling loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == show_text:
# When the SHOW_TEXT event is triggered, draw the text on the screen
screen.blit(game_over_text, (400, 110))
pygame.display.flip()
# Stop the timer
pygame.time.set_timer(show_text, 0)
# Update the display
input_box.draw(screen)
input_box2.draw2(screen)
input_box3.draw3(screen)
pygame.display.flip()
# Set the frame rate to 60 FPS
clock.tick(60)
# Quitting
pygame.quit()
# Class for the plane
class Plane(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = plane_image
self.rect = self.image.get_rect()
self.rect.x = 660
self.rect.y = 100
self.vx = 0 # Initialize velocity to 0
self.vy = 0
self.dropped = False
self.moving = False
self.angle = 0
def update(self):
if self.moving:
self.rect.x += self.vx / SCALE
self.rect.y += self.vy / SCALE
if self.rect.x < -200:
self.moving = False
self.rect.x = 660
self.rect.y = 100
self.vx = 0 # Reset velocity to 0
self.vy = 0
self.dropped = False
# Class for the Cargo
class Cargo(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = cargo_image
self.rect = self.image.get_rect()
self.rect.x = plane.rect.x + 80
self.rect.y = plane.rect.y + 62
self.in_air = False
def update(self):
if self.in_air:
self.vy += GRAVITY / SCALE
self.rect.x += self.vx / SCALE
self.rect.y += self.vy / SCALE
if self.rect.y > 580 and plane.rect.x == 660:
self.rect.x = plane.rect.x + 80
self.rect.y = plane.rect.y + 62
self.vx = 0
self.vy = 0
self.in_air = False
self.angle = plane.angle
# Class for the Truck
class Truck(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = truck_image
self.rect = self.image.get_rect()
self.rect.x = random.randint(100, 400)
self.rect.y = 520
def update(self):
if pygame.sprite.collide_rect(self, cargo):
boom_sound.play()
self.image = cargofilled_image
self.rect.x = random.randint(100, 400)
self.rect.y = 520
# Class for the plane velocity input box
class InputBox:
def __init__(self, x, y, width, height, font_size=20):
self.rect = pygame.Rect(x, y, width, height)
self.color = (0, 0, 0)
self.text_color = (0, 0, 0)
self.font = pygame.font.Font(None, font_size)
self.input_text = ""
self.active = False
#
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.x <= event.pos[0] <= self.rect.x + self.rect.width and \
self.rect.y <= event.pos[1] <= self.rect.y + self.rect.height:
self.active = True
print("box clicked")
self.color = color_active
else:
self.active = False
print("box not clicked")
self.color = (0, 0, 0)
if event.type == pygame.KEYDOWN and self.active == True:
if event.key == pygame.K_RETURN:
stored_input = float(self.input_text)
plane.vx = -stored_input # Sets the plane's velocity based off user input
print("Plane Velocity:", plane.vx)
self.input_text = "" # Resets to an empty string
elif event.key == pygame.K_BACKSPACE:
self.input_text = self.input_text[:-1]
else:
self.input_text += event.unicode
def get_input_value(self):
return self.input_text
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 2)
text_surface = self.font.render(str(self.input_text) + "m/s", True, self.text_color)
screen.blit(text_surface, (self.rect.x + 5, self.rect.y + 1))
# Class for the angle input box
class InputBox2:
def __init__(self, x, y, width, height, font_size=20):
self.rect = pygame.Rect(x, y, width, height)
self.color = (0, 0, 0)
self.text_color = (0, 0, 0)
self.font = pygame.font.Font(None, font_size)
self.input_text2 = ""
self.active = False
#
def handle_event2(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.x <= event.pos[0] <= self.rect.x + self.rect.width and \
self.rect.y <= event.pos[1] <= self.rect.y + self.rect.height:
self.active = True
print("box clicked")
self.color = color_active
else:
self.active = False
print("box not clicked")
self.color = (0, 0, 0)
if event.type == pygame.KEYDOWN and self.active == True:
if event.key == pygame.K_RETURN:
stored_input2 = float(self.input_text2)
plane.angle = stored_input2 # Set the plane's angle
print("(REGISTEred IN CLASS)Plane Angle:", plane.angle)
elif event.key == pygame.K_BACKSPACE:
self.input_text2 = self.input_text2[:-1]
else:
self.input_text2 += event.unicode
def get_input_value2(self):
return self.input_text2
def draw2(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 2)
text_surface = self.font.render(str(self.input_text2) + "°", True, self.text_color)
screen.blit(text_surface, (self.rect.x + 5, self.rect.y + 1))
# Class for the height input box
class InputBox3:
def __init__(self, x, y, width, height, font_size=20):
self.rect = pygame.Rect(x, y, width, height)
self.color = (0, 0, 0)
self.text_color = (0, 0, 0)
self.font = pygame.font.Font(None, font_size)
self.input_text3 = ""
self.active = False
#
def handle_event3(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.x <= event.pos[0] <= self.rect.x + self.rect.width and \
self.rect.y <= event.pos[1] <= self.rect.y + self.rect.height:
self.active = True
print("box clicked")
self.color = color_active
else:
self.active = False
print("box not clicked")
self.color = (0, 0, 0)
if event.type == pygame.KEYDOWN and self.active == True:
if event.key == pygame.K_RETURN:
stored_input = float(self.input_text3)
plane.rect.y = (600 - stored_input) - 90 # Set the plane's height
print("Plane Height:", plane.rect.y)
self.input_text3 = "" # Reset to an empty string
elif event.key == pygame.K_BACKSPACE:
self.input_text3 = self.input_text3[:-1]
else:
self.input_text3 += event.unicode
def get_input_value3(self):
return self.input_text3
def draw3(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 2)
text_surface = self.font.render(str(self.input_text3) + "m", True, self.text_color)
screen.blit(text_surface, (self.rect.x + 5, self.rect.y + 1))
# Define some variables
last_entered_velocity = 0
last_entered_height = 0
angle = 100 # The angle of the plane in degrees
height = 100 # The height of the plane in meters
velocity = 0 # The initial velocity of the cargo in m/s
collision = False
game_over = False # The flag to indicate if the game is over
# Create a group of sprites
sprites = pygame.sprite.Group()
# Create an instance of each sprite
plane = Plane()
cargo = Cargo()
truck = Truck()
input_box = InputBox(250, 10, 60, 20)
input_box2 = InputBox2(250, 35, 60, 20)
input_box3 = InputBox3(250, 60, 60, 20)
# Add the sprites to the group
sprites.add(plane, cargo, truck)
# Call the main game loop function
main_game_loop()