-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
1259 lines (1017 loc) · 60.4 KB
/
game.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 pygame
from pygame.locals import *
import sys
from dataclasses import dataclass
import random
import traceback
import datetime
import math
import time
from utils.colours import Colours
from utils import renderutils, database, telemetry
pygame.init()
print("""
\||/
\||/
.<><><>.
.<><><><>.
'<><><><>'
'<><><>'""")
BACKGROUND = (28, 28, 28)
FPS = 147
clock = pygame.time.Clock()
max_resolution = database.get_max_resolution()
if database.get_resolution() == max_resolution:
WINDOW = pygame.display.set_mode(max_resolution)
SCREEN = None
else:
WINDOW = pygame.Surface(max_resolution)
if database.get_resolution() == pygame.FULLSCREEN:
display_info = pygame.display.Info()
if math.isclose((display_info.current_w / display_info.current_h), 1.77, abs_tol=10**-3): # If the screen is 16:9
SCREEN = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
else: # Otherwise, render a 16:9 game with black bars on the top and bottom to prevent warping of the game
fullscreen_res = (display_info.current_w, display_info.current_w / 1.778)
SCREEN = pygame.display.set_mode(fullscreen_res, pygame.FULLSCREEN|pygame.SCALED)
else:
SCREEN = pygame.display.set_mode(database.get_resolution())
pygame.display.set_caption("Gorilla Pong")
pygame.display.set_icon(pygame.image.load(renderutils.resource_path('image/gorilla.png')))
bg_normal = pygame.transform.scale(pygame.image.load(renderutils.resource_path('image/bg_dark.png')), (WINDOW.get_width(), WINDOW.get_height()))
bg_easteregg = pygame.transform.scale(pygame.image.load(renderutils.resource_path('image/bg_gorilla.png')), (WINDOW.get_width(), WINDOW.get_height()))
bg_image = bg_normal
""" This is unnecessary, since resolution can only be switched on the main menu and doing so forces a game restart.
def switch_resolution(new_res):
global WINDOW
global SCREEN
if new_res == max_resolution:
WINDOW = pygame.display.set_mode(new_res)
else:
WINDOW = pygame.Surface(max_resolution)
SCREEN = pygame.display.set_mode(new_res)
database.set_resolution(new_res) # TEMPORARY FOR TESTING.
"""
#####
tm = telemetry.TelemetryModule() # Create telemetry instance
#####
from utils import audio # Import and initialise audio controller
sound = audio.Audio()
#####
screens = []
import menus.main_menu as main_menu # Import and initialise the UI controllers
screens.append(main_menu.MainMenu(WINDOW, SCREEN, "Gorillapong", sound, tm))
screens.append(main_menu.MainMenu(WINDOW, SCREEN, "Settings", sound, tm))
paused = False # When true, run the pause menu loop instead of the game loop
show_stats = True # When true, render debug information at the bottom of the game window
active_screen = 0 # Controls the UI state (which screen is displayed)
render_queue = [] # Objects are added to this list during the game loop, then rendered all at once
font = pygame.font.SysFont(None, 32)
score_font = pygame.font.SysFont(None, 128)
mega_font = pygame.font.SysFont(None, 256)
tosaccept = False
#####
from objects import paddle, balls
# Player object
@dataclass
class Player:
paddle_horizontal: paddle.Paddle
paddle_vertical: paddle.Paddle
score = 0
total_score = 0
lives = 3
name: str
colour: tuple
# Initialise the players and their paddles
player1 = Player(paddle.Paddle(WINDOW, 0, (300, 860), 'banana', 0), paddle.Paddle(WINDOW, 1, (40, 300), 'banana', 1), 'Yellow', Colours.PLAYER_YELLOW)
player2 = Player(paddle.Paddle(WINDOW, 0, (1300, 40), 'banana_green', 2), paddle.Paddle(WINDOW, 1, (1560, 300), 'banana_green', 3), 'Green', Colours.PLAYER_GREEN)
active_balls = [balls.Ball(WINDOW, 15, 5, 0.5, Colours.BALL, 0)] # List of balls currently on the field
player_last_hit = None
bounces = 0 # Total number of times the ball bounced during the game
next_powerup_bounces = 6 # Bounces until another powerup is spawned
powerup_spawn_counter = 0 # Useful for any powerups that need a unique ID
mode = 0 # 0 = aivsai, 1 = playervsai, 2 = PvP, 3 = PvP Comp
comp_started = False
game_start_timestamp = None
game_pause_timestamp = None
game_duration = None
game_duration_string = None
game_won = False
game_winner = None
win_threshold = 25 # Make it configurable
countdown_started = False
countdown_counter = 3
temp_ai_player = None
player_who_died = 0 # hacky way to implement deaths to lives
powerups_picked_up = 0
ball_pixels_travelled = 0
serves_missed = 0
casual_win_threshold = 0
powerups_enabled = True
next_powerup_bounces_range = (9, 15)
ai_difficulty = 1
casual_ball_speed = 5.0
comp_ball_speed = 5.0
comp_miss_penalty = 2
comp_ball_speedup = False
powerups_picked_up = 0
shown_splash = False
#####
from objects import powerups
spawned_powerups = [] # List of powerups on the field
#####
# For calculating frame times
current_frame_ticks = 0
last_frame_ticks = 0
time_delta = 0
delay = 0
#####
ai = False # Whether the player2 AI is enabled or not
player1_ai = True # Causes player1 to be controlled by AI as well.
aim_randomiser = 1 # Determines where the AI will attempt to land the ball on its paddles. 0 = one corner 1 = middle 2 = other corner
repredict = True # Allow AI to make another prediction as to where the ball will land
#####
def set_paddle_sprite_for_player(player, activation = False):
""" Switch the sprites for a player's paddles between normal and terminator version """
player.paddle_vertical.swap_sprites(activation)
player.paddle_horizontal.swap_sprites(activation)
def set_db_vars():
""" Sets all configurable game related variables to the settings stored in the database. """
global casual_win_threshold
global win_threshold # comp
global powerups_enabled
global next_powerup_bounces_range
global ai_difficulty
global casual_ball_speed
global comp_ball_speed
global comp_miss_penalty
global comp_ball_speedup
gameplay_settings = database.get_all_gameplay_settings(False)
casual_win_threshold = gameplay_settings[0]
casual_ball_speed = [5.0, 6.5][gameplay_settings[1]]
if gameplay_settings[2] == 0:
powerups_enabled = False
else:
powerups_enabled = True
next_powerup_bounces_range = [(18, 30), (9, 21), (4, 11), (1, 2)][gameplay_settings[2] - 1]
ai_difficulty = gameplay_settings[3]
win_threshold = gameplay_settings[4]
comp_ball_speed = [5.0, 6.5][gameplay_settings[5]]
comp_miss_penalty = gameplay_settings[6]
comp_ball_speedup = gameplay_settings[7]
def collision(rleft, rtop, width, height, # Rect coords
center_x, center_y, radius): # Circle coords
""" Detect collision between a rectangle and circle. """
# Construct rectangle bounding box
rright, rbottom = rleft + width, rtop + height
# Construct circle bounding box
cleft, ctop = center_x-radius, center_y-radius
cright, cbottom = center_x+radius, center_y+radius
# Return false if there is no collision
if rright < cleft or rleft > cright or rbottom < ctop or rtop > cbottom:
return False
# Check whether any point of rectangle is inside circle's radius
for x in (rleft, rleft+width):
for y in (rtop, rtop+height):
# Compare distance between circle's center point and each point of
# the rectangle with the circle's radius
if math.hypot(x-center_x, y-center_y) <= radius:
return True # Collision detected
# Check if center of circle is inside rectangle
if rleft <= center_x <= rright and rtop <= center_y <= rbottom:
return True # Collision detected
return False # No collision detected
def reset_comp_vars():
""" Reset variables related to competitive gamemode """
global game_won
global game_winner
global comp_started
global countdown_started
global countdown_counter
game_won = False
game_winner = None
comp_started = False
countdown_started = False
countdown_counter = 3
def clear_powerups(clear_all):
""" Routine to clear all the powerups in the game """
global active_balls
global spawned_powerups
delete_queue = []
for powerup in spawned_powerups:
if powerup.collected or clear_all:
if type(powerup) == powerups.Computer and powerup.expired:
delete_queue.append(powerup) # Only append calculators if they're already expired
else:
delete_queue.append(powerup)
for obj in delete_queue:
spawned_powerups.remove(obj)
if clear_all: # Clear effects of powerups on the ball if requested
active_balls[0].speed = active_balls[0].default_speed
active_balls[0].in_puddle = False
active_balls[0].pringle_last_hit = None
def reset_ball():
""" Reset ball position, speed, and clear powerups """
global active_balls # absolute python 2023
global spawned_powerups
global repredict
repredict = True
clear_powerups(False)
if mode == 3:
active_balls = [balls.Ball(WINDOW, 15, comp_ball_speed, 0.1, Colours.BALL, 0)] # 5
else:
active_balls = [balls.Ball(WINDOW, 15, casual_ball_speed, 0.1, Colours.BALL, 0)]
rand = random.randint(0, 3)
if rand == 1:
active_balls[0].reverse_velocity_x()
elif rand == 2:
active_balls[0].reverse_velocity_y()
elif rand == 3:
active_balls[0].reverse_velocity_x()
active_balls[0].reverse_velocity_y()
def reset_paddles():
""" Reset paddle positions """
player1.paddle_horizontal.move_to(800)
player1.paddle_vertical.move_to(450)
player2.paddle_horizontal.move_to(800)
player2.paddle_vertical.move_to(450)
def reset_points():
""" Increment stats then reset players' points and lives """
database.set_stat("total_points_scored_p1", player1.total_score)
database.set_stat("total_points_scored_p2", player2.total_score)
player1.score = 0
player2.score = 0
player1.total_score = 0
player2.total_score = 0
player1.lives = 3
player2.lives = 3
def reset_game_vars(from_pause = False):
""" Imcrement stats, then reset various variables that change over the course of a game """
global bounces
global next_powerup_bounces
global powerups_picked_up
global game_start_timestamp
global game_duration
global ball_pixels_travelled
global serves_missed
database.set_stat("total_bounces", bounces)
database.set_stat("total_powerups", powerups_picked_up)
database.set_stat("total_pixels_travelled", ball_pixels_travelled)
database.set_stat("serves_missed", serves_missed)
if from_pause:
game_duration = game_pause_timestamp - game_start_timestamp
database.set_stat("total_playtime", game_duration.total_seconds())
database.set_stat(f"playtime_{mode}", game_duration.total_seconds())
bounces = 0
next_powerup_bounces = 20
powerups_picked_up = 0
game_start_timestamp = None
game_duration = 0
ball_pixels_travelled = 0
serves_missed = 0
def start_countdown():
""" Start the comp mode countdown by setting variables """
global comp_started
global countdown_started
global countdown_counter
global delay
if not comp_started:
sound.countdown_beep()
countdown_started = True
countdown_counter = 3
delay = 1008 # Should be about 1 second
def get_new_powerup(spawn_index = None):
""" Returns a random new powerup, or a specific powerup is spawn_index is provided """
global bounces
global powerup_spawn_counter
if spawn_index:
spawn_rand = spawn_index
else:
if mode == 0:
spawn_rand = random.randint(1, 9)
else:
spawn_rand = random.randint(1, 10)
powerup_spawn_counter += 1
if spawn_rand in [1, 2, 3]:
return powerups.Pineapple(WINDOW)
elif spawn_rand in [4, 5]:
return powerups.Pickle(WINDOW)
elif spawn_rand in [6, 7]:
return powerups.Water(WINDOW)
elif spawn_rand in [8, 9, 10]:
return powerups.Pringle(WINDOW, powerup_spawn_counter)
elif spawn_rand in [11]: # Inaccessible for now
return powerups.Computer(WINDOW)
def perfect_ai(player):
""" This will move the paddles of the designated player to hit the ball.
The method used here means it is effectively impossible for the designated player to miss. """
divisor = random.randint(20, 25) / 10 # Add some flavor to corner shots by hitting different parts of the paddle (21, 25)
# Determine whether AI aims to make the ball hit left corner, middle, or right corner of the paddle
if aim_randomiser == 0:
impact_x = player.paddle_horizontal.paddle_pos.x - player.paddle_horizontal.paddle_rect.x / divisor
impact_y = player.paddle_vertical.paddle_pos.y - player.paddle_vertical.paddle_rect.y / divisor
elif aim_randomiser == 1:
impact_x = player.paddle_horizontal.paddle_pos.x
impact_y = player.paddle_vertical.paddle_pos.y
elif aim_randomiser == 2:
impact_x = player.paddle_horizontal.paddle_pos.x + player.paddle_horizontal.paddle_rect.x / divisor
impact_y = player.paddle_vertical.paddle_pos.y + player.paddle_vertical.paddle_rect.y / divisor
if impact_x > ball.position.x:
player.paddle_horizontal.move_negative()
else:
player.paddle_horizontal.move_positive()
if impact_y > ball.position.y:
player.paddle_vertical.move_positive()
else:
player.paddle_vertical.move_negative()
# Should make the database have a max res value so that these aren't hardcoded
def map_mouse_position(pos):
""" This function maps the real mouse position on the screen to its relative position on the unscaled game window.
Due to the way the alternate resolution settings are implemeneted, without this mouse mapping, buttons would be
difficult or impossible to press and the visual position of the cursor on the screen would be different to where the game thinks it is. """
if SCREEN:
res = SCREEN.get_size()
else:
res = WINDOW.get_size()
x_map = ((1600) / (res[0]) * (pos[0]))
y_map = ((900) / (res[1]) * (pos[1]))
return (x_map, y_map)
def splash_screen():
""" Render simple splash screen """
logo = pygame.image.load(renderutils.resource_path("image/logo.png"))
WINDOW.fill(Colours.BG_GREY)
WINDOW.blit(logo, (230, 300))
try: # Questionable error handling technique, but hey, it works great
looping = True
tosaccept = database.get_tosaccept()
if not tosaccept:
shown_splash = True
sound.countdown_beep()
else:
tm.sysinfo()
while looping:
if active_screen != 3:
""" Within this IF statement is all the main menu logic. The loop and screen switching is controlled here, while everything else
like click processing and rendering is done elsewhere in designated functions contained in menu and button classes. """
if not tosaccept: # YOU SHALL NOT PASS! (without accepting our terms)
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
pass
if event.type == QUIT:
pygame.quit()
sys.exit()
WINDOW.fill(Colours.BG_GREY)
WINDOW.blit(score_font.render("TERMS OF SERVICE", True, Colours.ORANGEY_YELLOW), (380, 180))
WINDOW.blit(font.render("In order to play the game you must agree to the following conditions:", True, Colours.LIGHT_RED), (400, 340))
WINDOW.blit(font.render("You consent to the collection and transmission of telemetry data", True, Colours.WHITE), (400, 385))
WINDOW.blit(font.render("from the game \"Gorillapong\", potentially inlcuding:", True, Colours.WHITE), (400, 410))
WINDOW.blit(font.render("- device hostname, device specifications", True, Colours.WHITE), (420, 435))
WINDOW.blit(font.render("- resolution, audio settings, gameplay settings", True, Colours.WHITE), (420, 460))
WINDOW.blit(font.render("- game launches, button clicks, game sessions, errors", True, Colours.WHITE), (420, 485))
WINDOW.blit(font.render("Do you want to proceed?", True, Colours.LIGHT_PASTEL_GREEN), (400, 560))
WINDOW.blit(font.render("You may revoke authorisation at any time by accessing save.json and modifying \"tosaccept\" to false", True, Colours.SCORE_GREY), (360, 720))
button1 = pygame.Rect(990, 600, 220, 85)
button2 = pygame.Rect(390, 600, 220, 85)
if pygame.key.get_pressed()[K_SPACE]:
start_countdown()
# Collision processing for buttons
if(button1.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_PASTEL_GREEN, button1)
if pygame.mouse.get_pressed()[0] and delay <= 0:
sound.button_click() # Sound effect
database.accept_tos()
tosaccept = True
shown_splash = False
#tm.click(5004) For some reason creates duplicate entry
tm.sysinfo()
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button1)
if(button2.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_RED, button2)
if pygame.mouse.get_pressed()[0] and delay <= 0:
pygame.quit()
sys.exit()
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button2)
WINDOW.blit(font.render("Accept", True, Colours.BLACK), (button1.left + 70, button1.top + 33))
WINDOW.blit(font.render("Deny", True, Colours.BLACK), (button2.left + 80, button2.top + 33))
# When active_screen == 6, the credits screen is shown
# It needs a separate loop as it does not use the normal main menu framework, and requires a specific function call
elif active_screen == 6:
clicked = False
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
clicked = True
if event.type == QUIT:
pygame.quit()
sys.exit()
WINDOW.fill(Colours.BG_GREY)
result = screens[0].process_render_credits_screen(pygame.mouse.get_pos(), clicked) # This handles both click processing and rendering for credits screen
if result is not None: # When the back button is pressed, return to main menu screen
active_screen = result
elif active_screen == 8: # Statistics screen, similar story to credits screen, it's just text and a button and this is ironically the easiest way to make it work
clicked = False
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
clicked = True
if event.type == QUIT:
pygame.quit()
sys.exit()
WINDOW.fill(Colours.BG_GREY)
result = screens[0].process_render_statistics_screen(pygame.mouse.get_pos(), clicked) # This handles both click processing and rendering for credits screen
if result is not None: # When the back button is pressed, return to main menu screen
active_screen = result
else:
""" All other screens of the main menu are processed here """
if active_screen == 0:
WINDOW.blit(bg_image, (0, 0))
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_7 and bg_image != bg_easteregg: # Easter egg
#tosaccept = False
#database.accept_tos(False)
bg_image = bg_easteregg
tm.click(5012)
elif event.type == MOUSEBUTTONDOWN:
result, ai_toggle = screens[active_screen].process_position(event.pos, True)
if result != None:
active_screen = result
player1.lives = 3
player2.lives = 3
spawned_powerups = []
# Configure variables specific to the selected gamemode
ai = ai_toggle
if ai:
mode = 1 # Player vs AI mode
set_paddle_sprite_for_player(player2, True)
set_paddle_sprite_for_player(player1, False)
else:
mode = 2 # Multiplayer mode
set_paddle_sprite_for_player(player2, False)
set_paddle_sprite_for_player(player1, False)
player2.paddle_horizontal.ai_paddle = ai_toggle
player2.paddle_vertical.ai_paddle = ai_toggle
repredict = True
if active_screen == 5: # AI vs AI mode
player1_ai = True
active_screen = 3
mode = 0 # AI vs AI mode
set_paddle_sprite_for_player(player2, True)
set_paddle_sprite_for_player(player1, True)
elif active_screen == 7: # Comp mode
player1_ai = False
active_screen = 3
mode = 3
delay = 803 # Should be long enough to prevent issues, around 0.8 sec
countdown_started = False
comp_started = False
set_paddle_sprite_for_player(player2, False)
set_paddle_sprite_for_player(player1, False)
else:
player1_ai = False
pass
if active_screen == 3:
set_db_vars()
if mode == 3:
sound.stop_music()
else:
sound.play_game_music()
show_stats = database.get_stats_toggle()
game_start_timestamp = datetime.datetime.now()
elif pygame.mouse.get_pressed()[0] and active_screen == 1:
""" The volume level adjustment menu needs to recognise click-and-drag for its sliders, therefore a separate elif clause and function are used. """
screens[active_screen].process_hold(pygame.mouse.get_pos())
if event.type == QUIT or active_screen == -1: # Action -1 -> quits game
pygame.quit()
sys.exit()
else:
if active_screen != 3 and active_screen != 6 and active_screen != 8:
""" active_screen == 3 -> renders the game, so run the game loop
active_screen == 6 -> renders the credits menu, using its specific function
active_screen == 8 -> renders statistics menu, using its specific function, similar to credits
all other main menu screens use the same function calls so they can all follow this logic """
screens[active_screen].process_position(pygame.mouse.get_pos())
if active_screen != 0:
WINDOW.fill(BACKGROUND)
screens[active_screen].render()
if shown_splash: # No music until splash screen has finished
sound.play_menu_music() # The sound controller keeps track and ensures duplicate music will not be played even if this is called many times
else:
""" THIS IS THE GAME LOOP """
# Frame time calculation, used for timers
last_frame_ticks = current_frame_ticks
current_frame_ticks = pygame.time.get_ticks()
time_delta = (current_frame_ticks - last_frame_ticks)
if time_delta > 800: # Hopefully nobody's FPS is so low that the timedelta between frames is legitimately 800
time_delta = 1 # Prevents breakage of menu delays in case of a lag spike
##################################################
for event in pygame.event.get(): # Get all inputs from mouse and keyboard
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
if paused:
paused = False
else:
paused = True
game_pause_timestamp = datetime.datetime.now()
tm.session("PAUSE", mode, game_pause_timestamp - game_start_timestamp, player1.score, player2.score, bounces, serves_missed)
# Cheat keys for spawning powerups
if event.key == K_1:
spawned_powerups.append(get_new_powerup(1))
""" Not really necessary anymore
if event.key == K_BACKSPACE: # Shortcut to return to main menu
active_screen = 0 # Back to main menu
reset_points()
reset_game_vars(True)
"""
if event.type == QUIT: # This occurs when they press the X button in the window status bar
pygame.quit()
sys.exit()
if paused:
""" PAUSE MENU """
pygame.draw.rect(WINDOW, Colours.SCORE_GREY, pygame.Rect(350, 200, 900, 400)) # Blank surface on which to render texts & buttons
WINDOW.blit(score_font.render("PAUSED", True, Colours.BALL), (610, 225))
if mode == 3:
WINDOW.blit(font.render(f"First to {win_threshold} points wins", True, Colours.WHITE), (670, 320))
else:
if casual_win_threshold <= 0:
WINDOW.blit(font.render(f"Infinite game", True, Colours.WHITE), (720, 320))
else:
WINDOW.blit(font.render(f"Score goal: {casual_win_threshold}", True, Colours.WHITE), (710, 320))
button1 = pygame.Rect(430, 460, 220, 85)
button2 = pygame.Rect(930, 460, 220, 85)
button3 = pygame.Rect(682, 460, 220 , 85)
button4 = pygame.Rect(682, 360, 220, 85)
# These if statements detect collisions for each button
if(button1.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_RED, button1)
if pygame.mouse.get_pressed()[0]:
sound.button_click() # Sound effect
paused = False
active_screen = 0
reset_points()
reset_game_vars(True)
tm.click(5005)
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button1)
if(button2.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_PASTEL_GREEN, button2)
if pygame.mouse.get_pressed()[0]:
sound.button_click() # Sound effect
paused = False
tm.click(5006)
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button2)
if(button3.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.ORANGEY_YELLOW, button3)
if pygame.mouse.get_pressed()[0]:
sound.button_click() # Sound effect
reset_ball()
# Unfortunately cannot telemetry log, risk of spamming
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button3)
if mode != 3 and powerups_enabled: # Don't show powerup related button in comp mode, where there are no powerups
if(button4.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.ORANGEY_YELLOW, button4)
if pygame.mouse.get_pressed()[0]:
sound.button_click() # Sound effect
clear_powerups(True)
# Unfortunately cannot telemetry log, risk of spamming
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button4)
# Render texts for buttons (button rects were rendered earlier, in the if statements)
WINDOW.blit(font.render("Main Menu", True, Colours.BLACK), (button1.left + 50, button1.top + 33))
WINDOW.blit(font.render("Resume Game", True, Colours.BLACK), (button2.left + 35, button2.top + 33))
WINDOW.blit(font.render("Reset Ball", True, Colours.BLACK), (button3.left + 55, button3.top + 33))
if mode != 3 and powerups_enabled: # Only show button to clear powerups if they're enabled and can spawn in the current gamemode
WINDOW.blit(font.render("Clear Powerups", True, Colours.BLACK), (button4.left + 22, button4.top + 33))
pygame.draw.rect(WINDOW, Colours.BG_GREY, pygame.Rect(0, 870, 176, 30)) # Cover up the old FPS text so that the new one can be rendered without it overlapping
if show_stats: # Only render fps text if stats for nerds is enabled
WINDOW.blit(font.render("FPS: {} / {}".format(round(clock.get_fps(), 1), time_delta), True, Colours.GREY), (5, 875))
elif mode == 3 and not comp_started:
""" COMP PRE-START MENU
players are brought here when they select comp mode
this shows information about the upcoming game and allows them to start when they are ready
presing start initialises a 3 second countdown before the game begins """
WINDOW.fill(BACKGROUND)
pygame.draw.rect(WINDOW, Colours.SCORE_GREY, pygame.Rect(350, 200, 900, 400))
if countdown_started:
""" If they pressed start, render countdown numbers """
WINDOW.blit(mega_font.render(f"{countdown_counter}", True, Colours.LIGHT_PASTEL_GREEN), (740, 330))
delay -= time_delta # Subtract the time between the last frame and this frame from the total time we need to wait
if delay <= 0: # If waiting time has elapsed, count down by 1
sound.countdown_beep()
countdown_counter -= 1
delay = 1008 # Should be about 1 second
if countdown_counter <= 0: # Countdown is finished, prepare and start the game
reset_ball()
reset_paddles()
sound.play_comp_music()
game_start_timestamp = datetime.datetime.now()
comp_started = True
else:
""" Otherwise, show them information and buttons, wait for them to press start or return to the main menu """
if delay > 0:
delay -= time_delta
WINDOW.fill(BACKGROUND)
pygame.draw.rect(WINDOW, Colours.SCORE_GREY, pygame.Rect(350, 200, 900, 400))
WINDOW.blit(score_font.render("ARE YOU READY?", True, Colours.LIGHT_PASTEL_GREEN), (400, 230))
WINDOW.blit(font.render(f"First to {win_threshold} points wins", True, Colours.WHITE), (700, 350))
WINDOW.blit(font.render("No powerups", True, Colours.WHITE), (750, 380))
if comp_miss_penalty == 2:
WINDOW.blit(font.render("Missing a serve loses a life", True, Colours.WHITE), (675, 410))
elif comp_miss_penalty == 1:
WINDOW.blit(font.render("Missing a serve loses a point", True, Colours.WHITE), (675, 410))
WINDOW.blit(font.render("(or press space)", True, Colours.WHITE), (1018, 570))
button1 = pygame.Rect(390, 480, 220, 85)
button2 = pygame.Rect(990, 480, 220, 85)
if pygame.key.get_pressed()[K_SPACE]:
start_countdown()
# Collision processing for buttons
if(button1.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_RED, button1)
if pygame.mouse.get_pressed()[0] and delay <= 0:
sound.button_click() # Sound effect
paused = False
active_screen = 0
tm.click(5009)
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button1)
if(button2.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_PASTEL_GREEN, button2)
if pygame.mouse.get_pressed()[0] and delay <= 0:
start_countdown()
tm.click(5010)
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button2)
WINDOW.blit(font.render("Back to Menu", True, Colours.BLACK), (button1.left + 38, button1.top + 33))
WINDOW.blit(font.render("LET'S GO!", True, Colours.BLACK), (button2.left + 57, button2.top + 33))
elif game_won:
""" WIN SCREEN """
pygame.draw.rect(WINDOW, Colours.SCORE_GREY, pygame.Rect(350, 200, 900, 400))
WINDOW.blit(score_font.render(game_winner.name.upper(), True, game_winner.colour), (450, 230))
WINDOW.blit(font.render("has won the game!", True, Colours.WHITE), (900, 280))
if player1.score > 9: # If the score is less than 10, render the single digit further to the right so it doesn't look asymmetrical
WINDOW.blit(score_font.render(str(player1.score), True, player1.colour), (640, 350))
else:
WINDOW.blit(score_font.render(str(player1.score), True, player1.colour), (680, 350))
WINDOW.blit(score_font.render("-", True, Colours.WHITE), (780, 350))
WINDOW.blit(score_font.render(str(player2.score), True, player2.colour), (860, 350))
button1 = pygame.Rect(390, 480, 220, 85)
# Button collisions
if(button1.collidepoint(map_mouse_position(pygame.mouse.get_pos()))):
pygame.draw.rect(WINDOW, Colours.LIGHT_RED, button1)
if pygame.mouse.get_pressed()[0]:
sound.button_click() # Sound effect
paused = False
active_screen = 0
reset_points()
reset_game_vars()
reset_comp_vars()
tm.click(5011)
else:
pygame.draw.rect(WINDOW, Colours.WHITE, button1)
WINDOW.blit(font.render("Back to Menu", True, Colours.BLACK), (button1.left + 38, button1.top + 33))
WINDOW.blit(font.render(f"Total bounces: {bounces}", True, Colours.LIGHT_GREY), (950, 490))
WINDOW.blit(font.render(f"Game duration: {game_duration_string}", True, Colours.LIGHT_GREY), (950, 520))
WINDOW.blit(font.render(f"Total points: {player1.total_score} - {player2.total_score}", True, Colours.LIGHT_GREY), (950, 550))
else:
""" GAME LOOP """
if player1.lives <= 0:
player1.lives = 3
player1.score = round(player1.score / 2)
sound.lives_run_out() # Sound effect
elif player2.lives <= 0:
player2.lives = 3
player2.score = round(player2.score / 2)
sound.lives_run_out() # Sound effect
#### Input
keys = pygame.key.get_pressed()
# Holding shift/ralt: slows paddle movement
if keys[K_LSHIFT]:
p1_shift = True
else:
p1_shift = False
if keys[K_0] or keys[K_RALT]: # Use numpad 0
p2_shift = True
else:
p2_shift = False
# Paddle movement
if player1_ai or temp_ai_player == player1:
pass # Do nothing
else:
if keys[K_w]:
player1.paddle_vertical.move_positive(p1_shift)
if keys[K_s]:
player1.paddle_vertical.move_negative(p1_shift)
if keys[K_a]:
player1.paddle_horizontal.move_negative(p1_shift)
if keys[K_d]:
player1.paddle_horizontal.move_positive(p1_shift)
if ai or temp_ai_player == player2:
pass # Do nothing
else:
if keys[K_UP]:
player2.paddle_vertical.move_positive(p2_shift)
if keys[K_DOWN]:
player2.paddle_vertical.move_negative(p2_shift)
if keys[K_RIGHT]:
player2.paddle_horizontal.move_positive(p2_shift)
if keys[K_LEFT]:
player2.paddle_horizontal.move_negative(p2_shift)
render_queue += [player1.paddle_vertical, player1.paddle_horizontal, player2.paddle_vertical, player2.paddle_horizontal]
#### Game loop logic
out_of_bounds = False # Need to make it per ball, so just one can be popped when it goes out of bounds and not all reset
scoring_player = None
for i, ball in enumerate(active_balls):
if ai and not temp_ai_player == player2: #
if repredict:
impact_pos = ball.future_position(None)
# This method will result in the irrelevant paddle still jiggling slightly upon reprediction.
# A check that stops aim randomiser from being applied to irrelevant paddles would fix it.
if impact_pos.x == -1:
impact_pos.x = player2.paddle_horizontal.paddle_pos.x
if impact_pos.y == -1:
impact_pos.y = player2.paddle_vertical.paddle_pos.y
""" The divisor determines the margin of error for the AI's aim.
A larger number, in short, gives it a higher chance of missing a corner shot.
Therefore, create a larger divisor more often based on the ai difficulty configured in settings
It's worth noting that HARD mode effectively cannot miss, besides a few specific scenarios.
"""
divisor_divisor_random = random.randrange(0, 20)
if ai_difficulty == 0 and divisor_divisor_random > 5: # 3/4 chance of bad aim on easy
divisor_divisor = 15
elif ai_difficulty == 1 and divisor_divisor_random > 10: # 2/4 chance of bad aim on medium
divisor_divisor = 14
else: # Normal accuracy
divisor_divisor = 10
divisor = random.randint(21, 25) / divisor_divisor # Add some flavor to corner shots by hitting different parts of the paddle
if aim_randomiser == 0:
impact_pos.x -= player2.paddle_horizontal.paddle_rect.x / divisor
impact_pos.y -= player2.paddle_vertical.paddle_rect.y / divisor
elif aim_randomiser == 2:
impact_pos.x += player2.paddle_horizontal.paddle_rect.x / divisor
impact_pos.y += player2.paddle_vertical.paddle_rect.y / divisor
repredict = False
# Move paddles using lerp
new_x = renderutils.lerp(player2.paddle_horizontal.paddle_pos.x, impact_pos.x, 8/FPS)
new_y = renderutils.lerp(player2.paddle_vertical.paddle_pos.y, impact_pos.y, 8/FPS)
# Cap paddle movement at their max movement speed otherwise lerp will go haywire with it
if abs(player2.paddle_horizontal.paddle_pos.x - new_x) > player2.paddle_horizontal.ai_speed:
if new_x > player2.paddle_horizontal.paddle_pos.x:
new_x = player2.paddle_horizontal.paddle_pos.x + player2.paddle_horizontal.ai_speed
else:
new_x = player2.paddle_horizontal.paddle_pos.x - player2.paddle_horizontal.ai_speed
if abs(player2.paddle_vertical.paddle_pos.y - new_y) > player2.paddle_vertical.ai_speed:
if new_y > player2.paddle_vertical.paddle_pos.y:
new_y = player2.paddle_vertical.paddle_pos.y + player2.paddle_vertical.ai_speed
else:
new_y = player2.paddle_vertical.paddle_pos.y - player2.paddle_vertical.ai_speed
# Using paddle function allows paddle to manage its movement limits (prevents going off screen)
player2.paddle_horizontal.move_to(new_x)
player2.paddle_vertical.move_to(new_y)
if player1_ai: # Francis Sinclair
perfect_ai(player1)
elif temp_ai_player != None:
perfect_ai(temp_ai_player)
# ball.tick() returns its distance moved
ball_pixels_travelled += abs(ball.tick())
# For testing
#ball.position.x = pygame.mouse.get_pos()[0]
#ball.position.y = pygame.mouse.get_pos()[1]
# Ball out of bounds checks
if ball.position.x < -100 or ball.position.y > 1000: # Went out on yellow player's sides
out_of_bounds = True
if mode == 3 and player_last_hit == None:
sound.lose_life()
if comp_miss_penalty == 2:
player1.lives -= 1
elif comp_miss_penalty == 1 and player1.score > 0:
player1.score -= 1
scoring_player = player2
elif ball.position.x > 1700 or ball.position.y < -100: # Went out on green player's sides
out_of_bounds = True
if mode == 3 and player_last_hit == None:
sound.lose_life()
if comp_miss_penalty == 2:
player2.lives -= 1
elif comp_miss_penalty == 1 and player2.score > 0:
player2.score -= 1
scoring_player = player1
# We add balls to render queue later so that they are not behind powerups e.g. water puddle
#render_queue.append(ball)
paddle_collisions = [False, False, False, False]