-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1420 lines (1194 loc) · 50.5 KB
/
Copy pathmain.py
File metadata and controls
1420 lines (1194 loc) · 50.5 KB
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 math
import random
import os
import pygame
W, H = 1000, 600
BG = (18, 20, 24)
WHITE = (235, 235, 235)
RED = (235, 70, 70)
BLUE = (80, 150, 255)
GRAY = (130, 130, 130)
YELLOW = (245, 220, 80)
GOAL_W = 160
GOAL_LINE_THICK = 8
PUCK_R = 14
PADDLE_R = 28
PUCK_MAX_SPEED = 1050.0
FRICTION = 0.996
RESTITUTION_WALL = 0.98
RESTITUTION_PADDLE = 1.02
WIN_SCORE = 7
GOAL_BANNER_SEC = 0.95
WIN_COUNTDOWN_SEC = 3.0
START_COUNTDOWN_SEC = 3.0
DIFFS = {
"easy": {"bot_speed": 640.0, "reaction": (0.18, 0.36), "aim": (-210, 210), "jitter": (0.46, 0.34), "modes": (0.45, 1.05), "towards": -95},
"normal": {"bot_speed": 820.0, "reaction": (0.12, 0.24), "aim": (-150, 150), "jitter": (0.30, 0.22), "modes": (0.30, 0.85), "towards": -70},
"expert": {"bot_speed": 980.0, "reaction": (0.07, 0.16), "aim": (-90, 90), "jitter": (0.18, 0.14), "modes": (0.22, 0.70), "towards": -45},
}
ULT_SUPER_MULT_P = 1.32
ULT_SUPER_ADD_P = 170.0
ULT_SUPER_MULT_B = 1.27
ULT_SUPER_ADD_B = 140.0
ULT_HIT_VREL_MIN = 520.0
ULT_PLAYER_BASE = 0.018
ULT_BOT_BASE = 0.0095
ULT_BONUS_STRONG = 0.030
ULT_BONUS_DEFLECT = 0.022
ULT_BONUS_ACCURATE = 0.034
ULT_BONUS_COOLDOWN = 0.28
ULT_CONSUME_COOLDOWN = 0.12
ULT_SPARK_LIFE = (0.20, 0.48)
ULT_SPARK_N = 26
MUSIC_VOL_DEFAULT = 0.18
class Vec2:
__slots__ = ("x", "y")
def __init__(self, x=0.0, y=0.0):
self.x = float(x)
self.y = float(y)
def __add__(self, o): return Vec2(self.x + o.x, self.y + o.y)
def __sub__(self, o): return Vec2(self.x - o.x, self.y - o.y)
def __mul__(self, k): return Vec2(self.x * k, self.y * k)
def length(self): return math.hypot(self.x, self.y)
def normalized(self):
l = self.length()
if l <= 1e-9:
return Vec2(0, 0)
return Vec2(self.x / l, self.y / l)
def dot(self, o): return self.x * o.x + self.y * o.y
class Body:
def __init__(self, pos: Vec2, radius: float, color, mass=1.0):
self.pos = pos
self.vel = Vec2(0, 0)
self.r = float(radius)
self.color = color
self.mass = float(mass)
def clamp(v, a, b):
return max(a, min(b, v))
def limit_speed(v: Vec2, max_speed: float) -> Vec2:
s = v.length()
if s > max_speed:
return v * (max_speed / s)
return v
def reset_round(puck: Body, player: Body, bot: Body, scored_by=None):
puck.pos = Vec2(W / 2, H / 2)
puck.vel = Vec2(0, 0)
player.pos = Vec2(W / 2, H * 0.78)
player.vel = Vec2(0, 0)
bot.pos = Vec2(W / 2, H * 0.22)
bot.vel = Vec2(0, 0)
if scored_by == "PLAYER":
puck.vel = Vec2(random.uniform(-140, 140), -420)
elif scored_by == "BOT":
puck.vel = Vec2(random.uniform(-140, 140), 420)
def keep_paddle_in_half(p: Body, top_half: bool):
left = 40 + p.r
right = W - 40 - p.r
top = 40 + p.r
bottom = H - 40 - p.r
p.pos.x = clamp(p.pos.x, left, right)
p.pos.y = clamp(p.pos.y, top, bottom)
if top_half:
p.pos.y = clamp(p.pos.y, top, H / 2 - p.r - 6)
else:
p.pos.y = clamp(p.pos.y, H / 2 + p.r + 6, bottom)
def wall_collide_puck(puck: Body):
left = 40 + puck.r
right = W - 40 - puck.r
top = 40 + puck.r
bottom = H - 40 - puck.r
gx1 = W // 2 - GOAL_W // 2
gx2 = W // 2 + GOAL_W // 2
if puck.pos.x < left:
puck.pos.x = left
puck.vel.x *= -RESTITUTION_WALL
elif puck.pos.x > right:
puck.pos.x = right
puck.vel.x *= -RESTITUTION_WALL
in_goal_x = (gx1 <= puck.pos.x <= gx2)
if puck.pos.y < top and not in_goal_x:
puck.pos.y = top
puck.vel.y *= -RESTITUTION_WALL
elif puck.pos.y > bottom and not in_goal_x:
puck.pos.y = bottom
puck.vel.y *= -RESTITUTION_WALL
def check_goal(puck: Body):
gx1 = W // 2 - GOAL_W // 2
gx2 = W // 2 + GOAL_W // 2
in_goal_x = (gx1 <= puck.pos.x <= gx2)
if in_goal_x and puck.pos.y < 40 - puck.r * 0.2:
return "PLAYER"
if in_goal_x and puck.pos.y > H - 40 + puck.r * 0.2:
return "BOT"
return None
def predict_intercept_x(puck: Body, target_y: float):
vx, vy = puck.vel.x, puck.vel.y
if abs(vy) < 1e-6:
return puck.pos.x
t = (target_y - puck.pos.y) / vy
if t <= 0:
return puck.pos.x
x = puck.pos.x + vx * t
left = 40 + PUCK_R
right = W - 40 - PUCK_R
width = right - left
if width <= 1e-6:
return clamp(x, left, right)
x_rel = x - left
period = 2 * width
m = x_rel % period
if m > width:
m = period - m
return left + m
def resolve_circle_collision_with_info(a: Body, b: Body, elasticity=1.0):
delta = b.pos - a.pos
dist = delta.length()
min_dist = a.r + b.r
if dist <= 1e-9 or dist >= min_dist:
return False, Vec2(0, 0), 0.0, 0.0
n = delta * (1.0 / dist)
rv = b.vel - a.vel
vel_along_n = rv.dot(n)
rel_speed = rv.length()
penetration = (min_dist - dist)
total_mass = a.mass + b.mass
a.pos = a.pos - n * (penetration * (b.mass / total_mass))
b.pos = b.pos + n * (penetration * (a.mass / total_mass))
if vel_along_n > 0:
return True, n, rel_speed, vel_along_n
j = -(1.0 + elasticity) * vel_along_n
j /= (1.0 / a.mass + 1.0 / b.mass)
impulse = n * j
a.vel = a.vel - impulse * (1.0 / a.mass)
b.vel = b.vel + impulse * (1.0 / b.mass)
return True, n, rel_speed, vel_along_n
class BotAI:
def __init__(self, difficulty="normal"):
self.set_difficulty(difficulty)
self.mode = "track"
self.mode_timer = 0.0
self.aim_offset = 0.0
self.reaction = 0.14
self.cooldown = 0.0
def set_difficulty(self, difficulty):
self.difficulty = difficulty
cfg = DIFFS[difficulty]
self.bot_speed = cfg["bot_speed"]
self.reaction_rng = cfg["reaction"]
self.aim_rng = cfg["aim"]
self.jitter_xy = cfg["jitter"]
self.modes_rng = cfg["modes"]
self.towards_thresh = cfg["towards"]
def reset(self):
self.mode = "track"
self.mode_timer = 0.0
self.aim_offset = random.uniform(*self.aim_rng)
self.reaction = random.uniform(*self.reaction_rng)
self.cooldown = 0.0
def update(self, dt, bot: Body, puck: Body):
self.mode_timer -= dt
self.cooldown = max(0.0, self.cooldown - dt)
if self.mode_timer <= 0:
self.mode_timer = random.uniform(self.modes_rng[0], self.modes_rng[1])
self.mode = random.choice(["track", "poke", "defend"])
self.aim_offset = random.uniform(*self.aim_rng)
self.reaction = random.uniform(*self.reaction_rng)
top = 40 + bot.r
half_bottom = H / 2 - bot.r - 6
defend_y = top + 70
attack_y = top + 200
mid_y = top + 140
puck_towards_bot = (puck.vel.y < self.towards_thresh)
puck_in_bot_half = (puck.pos.y < H / 2)
if self.mode == "defend":
target_y = defend_y
if puck_towards_bot and puck_in_bot_half:
ix = predict_intercept_x(puck, defend_y + 10)
target_x = ix + self.aim_offset * 0.18
else:
target_x = W / 2 + self.aim_offset * 0.12
elif self.mode == "poke":
if puck_in_bot_half:
ty = clamp(puck.pos.y - 55, top + 40, mid_y)
ix = predict_intercept_x(puck, ty) if puck_towards_bot else puck.pos.x
target_x = ix + self.aim_offset * 0.22
target_y = ty
else:
target_x = W / 2 + self.aim_offset * 0.15
target_y = mid_y
else:
if puck_towards_bot:
ty = clamp(attack_y, top + 60, half_bottom - 60)
ix = predict_intercept_x(puck, ty + 20)
target_x = ix + self.aim_offset * 0.25
target_y = ty
else:
if puck_in_bot_half:
target_x = puck.pos.x + self.aim_offset * 0.16
target_y = clamp(puck.pos.y - 90, top + 50, mid_y)
else:
target_x = W / 2 + self.aim_offset * 0.12
target_y = mid_y
target_x = clamp(target_x, 40 + bot.r, W - 40 - bot.r)
target_y = clamp(target_y, top, half_bottom)
if self.cooldown <= 0:
self.cooldown = self.reaction
direction = Vec2(target_x - bot.pos.x, target_y - bot.pos.y)
if direction.length() > 1e-9:
direction = direction.normalized()
jx, jy = self.jitter_xy
jitter = Vec2(random.uniform(-jx, jx), random.uniform(-jy, jy))
direction = (direction + jitter).normalized()
bot.vel = direction * self.bot_speed
class Confetti:
def __init__(self):
self.parts = []
def burst(self, center, n=320):
cx, cy = center
for _ in range(n):
a = random.random() * math.tau
s = random.uniform(240, 980)
vx = math.cos(a) * s + random.uniform(-120, 120)
vy = math.sin(a) * s + random.uniform(-120, 120)
self.parts.append({
"p": [cx + random.uniform(-10, 10), cy + random.uniform(-10, 10)],
"v": [vx, vy],
"g": random.uniform(680, 1200),
"life": random.uniform(1.6, 2.9),
"size": random.randint(2, 5),
"col": random.choice([(245,220,80),(80,150,255),(235,70,70),(120,240,170),(245,140,220),(240,240,240)]),
"spin": random.uniform(-10, 10),
"ang": random.uniform(0, math.tau),
})
def update(self, dt):
alive = []
for p in self.parts:
p["life"] -= dt
if p["life"] <= 0:
continue
p["v"][1] += p["g"] * dt
p["p"][0] += p["v"][0] * dt
p["p"][1] += p["v"][1] * dt
p["ang"] += p["spin"] * dt
alive.append(p)
self.parts = alive
def draw(self, screen):
for p in self.parts:
x, y = p["p"]
s = p["size"]
a = p["ang"]
dx = math.cos(a) * s
dy = math.sin(a) * s
pygame.draw.line(screen, p["col"], (x - dx, y - dy), (x + dx, y + dy), s)
class Sparks:
def __init__(self):
self.parts = []
def burst(self, center, color, n=ULT_SPARK_N):
cx, cy = center
for _ in range(n):
a = random.random() * math.tau
s = random.uniform(220, 980)
self.parts.append({
"p": [cx, cy],
"v": [math.cos(a) * s, math.sin(a) * s],
"life": random.uniform(*ULT_SPARK_LIFE),
"size": random.randint(2, 4),
"col": color,
})
def update(self, dt):
alive = []
for p in self.parts:
p["life"] -= dt
if p["life"] <= 0:
continue
p["p"][0] += p["v"][0] * dt
p["p"][1] += p["v"][1] * dt
p["v"][0] *= 0.90 ** (dt * 60.0)
p["v"][1] *= 0.90 ** (dt * 60.0)
alive.append(p)
self.parts = alive
def draw(self, screen):
for p in self.parts:
x, y = p["p"]
s = p["size"]
pygame.draw.circle(screen, p["col"], (int(x), int(y)), s)
def try_set_window_icon(base_dir):
ico_path = os.path.join(base_dir, "icon.ico")
png_path = os.path.join(base_dir, "img", "icon.png")
try:
if os.path.isfile(ico_path):
pygame.display.set_icon(pygame.image.load(ico_path))
return
except Exception:
pass
try:
if os.path.isfile(png_path):
pygame.display.set_icon(pygame.image.load(png_path))
return
except Exception:
pass
def music_pause():
try:
pygame.mixer.music.pause()
except Exception:
pass
def music_resume():
try:
pygame.mixer.music.unpause()
except Exception:
pass
def music_stop():
try:
pygame.mixer.music.stop()
except Exception:
pass
def pulse_scale(t):
return 1.0 + 0.18 * math.sin(min(1.0, max(0.0, t)) * math.pi)
def draw_table(screen):
screen.fill(BG)
pygame.draw.rect(screen, WHITE, (40, 40, W - 80, H - 80), 3, border_radius=16)
pygame.draw.line(screen, GRAY, (W // 2, 45), (W // 2, H - 45), 3)
pygame.draw.circle(screen, GRAY, (W // 2, H // 2), 90, 3)
gx1 = W // 2 - GOAL_W // 2
gx2 = W // 2 + GOAL_W // 2
pygame.draw.line(screen, YELLOW, (gx1, 40), (gx2, 40), GOAL_LINE_THICK)
pygame.draw.line(screen, YELLOW, (gx1, H - 40), (gx2, H - 40), GOAL_LINE_THICK)
def draw_pause_icon(screen, rect, paused):
bg = pygame.Surface((rect.w, rect.h), pygame.SRCALPHA)
bg.fill((0, 0, 0, 80 if not paused else 140))
screen.blit(bg, rect.topleft)
pygame.draw.rect(screen, WHITE, rect, 2, border_radius=6)
x, y, w, h = rect
pad = 6
bar_w = 6
if paused:
pygame.draw.polygon(screen, WHITE, [(x+pad, y+pad), (x+pad, y+h-pad), (x+w-pad, y+h//2)])
else:
pygame.draw.rect(screen, WHITE, (x + pad + 2, y + pad, bar_w, h - 2 * pad), border_radius=3)
pygame.draw.rect(screen, WHITE, (x + w - pad - bar_w - 2, y + pad, bar_w, h - 2 * pad), border_radius=3)
def button(screen, font, rect, text, active=False):
bg = pygame.Surface((rect.w, rect.h), pygame.SRCALPHA)
bg.fill((255, 255, 255, 26 if not active else 70))
screen.blit(bg, rect.topleft)
pygame.draw.rect(screen, WHITE if active else GRAY, rect, 2, border_radius=12)
surf = font.render(text, True, WHITE if active else (210, 210, 210))
screen.blit(surf, surf.get_rect(center=rect.center))
def draw_overlay(screen, big, msg, color):
panel = pygame.Surface((W, H), pygame.SRCALPHA)
panel.fill((0, 0, 0, 150))
screen.blit(panel, (0, 0))
if msg:
t = big.render(msg, True, color)
screen.blit(t, t.get_rect(center=(W // 2, H // 2 - 64)))
def draw_score_line(screen, font, bot_color, player_color, score_bot, score_player):
bot_s = font.render(f"{score_bot}", True, bot_color)
ply_s = font.render(f"{score_player}", True, player_color)
colon = font.render(":", True, WHITE)
cx = W // 2
y = H // 2 + 18
screen.blit(bot_s, bot_s.get_rect(center=(cx - 36, y)))
screen.blit(colon, colon.get_rect(center=(cx, y)))
screen.blit(ply_s, ply_s.get_rect(center=(cx + 36, y)))
def draw_countdown_bottom(screen, big, t_left):
n = int(math.ceil(t_left))
if n < 1:
return
frac = t_left - math.floor(t_left)
scale = pulse_scale(1.0 - frac)
txt = str(n)
surf = big.render(txt, True, YELLOW)
sw, sh = surf.get_size()
surf2 = pygame.transform.smoothscale(surf, (max(1, int(sw * scale)), max(1, int(sh * scale))))
panel = pygame.Surface((W, 170), pygame.SRCALPHA)
panel.fill((0, 0, 0, 90))
screen.blit(panel, (0, H - 170))
screen.blit(surf2, surf2.get_rect(center=(W // 2, H - 90)))
def draw_ult_bar(screen, small, ult, ready, flash_t, super_text_t):
w = 250
h = 18
x = W - 18 - w
y = H - 18 - h
border = pygame.Rect(x, y, w, h)
glow = 0
if ready:
glow = int(65 + 55 * (0.5 + 0.5 * math.sin(pygame.time.get_ticks() * 0.012)))
if flash_t > 0:
glow = 180
if glow > 0:
g = pygame.Surface((w + 14, h + 14), pygame.SRCALPHA)
g.fill((255, 255, 255, glow))
screen.blit(g, (x - 7, y - 7))
pygame.draw.rect(screen, (0, 0, 0), border, border_radius=9)
pygame.draw.rect(screen, WHITE, border, 2, border_radius=9)
fill_w = int((w - 4) * clamp(ult, 0.0, 1.0))
if fill_w > 0:
pygame.draw.rect(screen, BLUE, pygame.Rect(x + 2, y + 2, fill_w, h - 4), border_radius=8)
if super_text_t > 0:
a = int(255 * clamp(super_text_t / 0.40, 0.0, 1.0))
t = small.render("SUPERSHOT", True, YELLOW)
tw, th = t.get_size()
s = pygame.Surface((tw, th), pygame.SRCALPHA)
s.blit(t, (0, 0))
s.set_alpha(a)
screen.blit(s, (x + w - tw, y - th - 8))
def draw_ready_ring(screen, body: Body, flash_t):
t = pygame.time.get_ticks() * 0.014
pulse = 0.5 + 0.5 * math.sin(t)
extra = int(6 + 7 * pulse)
if flash_t > 0:
extra = 16
pygame.draw.circle(screen, WHITE, (int(body.pos.x), int(body.pos.y)), int(body.r + extra), 2)
def draw_hud(screen, font, small, fps, puck, player, bot, score_player, score_bot, difficulty, ult_p_ready, ult_b_ready, show_debug):
bot_score = font.render(f"{score_bot}", True, bot.color)
ply_score = font.render(f"{score_player}", True, player.color)
colon = font.render(":", True, WHITE)
screen.blit(bot_score, bot_score.get_rect(center=(W // 2 - 34, 28)))
screen.blit(colon, colon.get_rect(center=(W // 2, 28)))
screen.blit(ply_score, ply_score.get_rect(center=(W // 2 + 34, 28)))
dev = small.render("maciejftw Puck Arena v0.1-dev", True, GRAY)
screen.blit(dev, (14, H - 24))
if not show_debug:
return
lines = [
(f"FPS: {fps:5.1f} bot:{difficulty}", GRAY),
(f"supershoot(p): {'true' if ult_p_ready else 'false'} supershoot(b): {'true' if ult_b_ready else 'false'}", GRAY),
(f"PUCK x={puck.pos.x:7.1f} y={puck.pos.y:7.1f}", WHITE),
(f" vx={puck.vel.x:7.1f} vy={puck.vel.y:7.1f}", WHITE),
(f"PLYR x={player.pos.x:7.1f} y={player.pos.y:7.1f}", player.color),
(f"BOT x={bot.pos.x:7.1f} y={bot.pos.y:7.1f}", bot.color),
]
y = 52
for text, col in lines:
surf = small.render(text, True, col)
screen.blit(surf, (14, y))
y += 20
def draw_volume_slider(screen, small, rect, value):
pygame.draw.rect(screen, (0, 0, 0), rect, border_radius=9)
pygame.draw.rect(screen, WHITE, rect, 2, border_radius=9)
fill = pygame.Rect(rect.x + 2, rect.y + 2, int((rect.w - 4) * clamp(value, 0.0, 1.0)), rect.h - 4)
if fill.w > 0:
pygame.draw.rect(screen, WHITE, fill, border_radius=8)
knob_x = rect.x + int(rect.w * clamp(value, 0.0, 1.0))
knob = pygame.Rect(0, 0, 12, rect.h + 8)
knob.center = (knob_x, rect.centery)
pygame.draw.rect(screen, YELLOW, knob, border_radius=6)
label = small.render("Music volume:", True, GRAY)
screen.blit(label, (rect.x, rect.y - 20))
def draw_fps_input(screen, small, rect, text, focused):
bg = pygame.Surface((rect.w, rect.h), pygame.SRCALPHA)
bg.fill((255, 255, 255, 20 if not focused else 55))
screen.blit(bg, rect.topleft)
pygame.draw.rect(screen, WHITE if focused else GRAY, rect, 2, border_radius=10)
label = small.render("FPS cap (type number, 0=unlimited):", True, GRAY)
screen.blit(label, (rect.x, rect.y - 20))
t = small.render(text if text else "", True, WHITE)
screen.blit(t, (rect.x + 10, rect.y + (rect.h - t.get_height()) // 2))
if focused and (pygame.time.get_ticks() // 500) % 2 == 0:
cx = rect.x + 10 + t.get_width() + 2
pygame.draw.line(screen, WHITE, (cx, rect.y + 6), (cx, rect.y + rect.h - 6), 2)
def draw_splash(screen, title_surf, reveal, boom_alpha):
screen.fill((0, 0, 0))
tw, th = title_surf.get_size()
x = (W - tw) // 2
y = (H - th) // 2 - 10
clip_w = int(tw * clamp(reveal, 0.0, 1.0))
if clip_w > 0:
clip = pygame.Rect(0, 0, clip_w, th)
screen.blit(title_surf, (x, y), area=clip)
if boom_alpha > 0:
b = pygame.Surface((tw + 200, th + 200), pygame.SRCALPHA)
a = int(255 * clamp(boom_alpha, 0.0, 1.0))
pygame.draw.circle(b, (255, 255, 255, a), (b.get_width() // 2, b.get_height() // 2), 42, 0)
pygame.draw.circle(b, (245, 220, 80, int(a * 0.9)), (b.get_width() // 2, b.get_height() // 2), 92, 2)
screen.blit(b, (W // 2 - b.get_width() // 2, H // 2 - b.get_height() // 2 - 10))
def main():
pygame.init()
try:
pygame.mixer.init()
except Exception:
pass
base_dir = os.path.dirname(os.path.abspath(__file__))
audio_dir = os.path.join(base_dir, "audio")
score_path = os.path.join(audio_dir, "score.mp3")
victory_path = os.path.join(audio_dir, "victory.mp3")
lose_path = os.path.join(audio_dir, "lose.mp3")
soundtrack_path = os.path.join(audio_dir, "soundtrack.mp3")
supershoot_path = os.path.join(audio_dir, "supershoot.mp3")
click_path = os.path.join(audio_dir, "click.mp3")
intro_path = os.path.join(audio_dir, "intro.mp3")
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("Puck Arena")
try_set_window_icon(base_dir)
clock = pygame.time.Clock()
font = pygame.font.SysFont("consolas", 30)
small = pygame.font.SysFont("consolas", 18)
big = pygame.font.SysFont("consolas", 72)
big2 = pygame.font.SysFont("consolas", 96)
title_font = pygame.font.SysFont("consolas", 92)
score_font = pygame.font.SysFont("consolas", 56)
def safe_sound(path):
try:
if os.path.isfile(path):
return pygame.mixer.Sound(path)
except Exception:
pass
return None
score_sfx = safe_sound(score_path)
victory_sfx = safe_sound(victory_path)
lose_sfx = safe_sound(lose_path)
super_sfx = safe_sound(supershoot_path)
click_sfx = safe_sound(click_path)
intro_sfx = safe_sound(intro_path)
if score_sfx: score_sfx.set_volume(0.9)
if victory_sfx: victory_sfx.set_volume(0.9)
if lose_sfx: lose_sfx.set_volume(0.9)
if super_sfx: super_sfx.set_volume(0.95)
if click_sfx: click_sfx.set_volume(0.7)
if intro_sfx: intro_sfx.set_volume(0.85)
ch_goal = pygame.mixer.Channel(1) if pygame.mixer.get_init() else None
ch_win = pygame.mixer.Channel(2) if pygame.mixer.get_init() else None
ch_ult = pygame.mixer.Channel(3) if pygame.mixer.get_init() else None
ch_ui = pygame.mixer.Channel(4) if pygame.mixer.get_init() else None
ch_intro = pygame.mixer.Channel(5) if pygame.mixer.get_init() else None
def ui_click():
if ch_ui and click_sfx:
ch_ui.stop()
ch_ui.play(click_sfx)
def intro_play():
if ch_intro and intro_sfx:
ch_intro.stop()
ch_intro.play(intro_sfx)
def intro_stop():
if ch_intro:
ch_intro.stop()
music_volume = MUSIC_VOL_DEFAULT
soundtrack_ok = pygame.mixer.get_init() and os.path.isfile(soundtrack_path)
if soundtrack_ok:
try:
pygame.mixer.music.load(soundtrack_path)
pygame.mixer.music.set_volume(music_volume)
except Exception:
soundtrack_ok = False
puck = Body(Vec2(W / 2, H / 2), PUCK_R, WHITE, mass=0.6)
player = Body(Vec2(W / 2, H * 0.78), PADDLE_R, BLUE, mass=2.5)
bot = Body(Vec2(W / 2, H * 0.22), PADDLE_R, RED, mass=2.5)
difficulty = "normal"
ai = BotAI(difficulty)
ai.reset()
score_player, score_bot = 0, 0
reset_round(puck, player, bot)
confetti = Confetti()
sparks = Sparks()
pause_icon = pygame.Rect(14, 14, 34, 34)
show_debug = True
paused = False
fps_cap = 60
fps_text = "60"
fps_focus = False
dragging_volume = False
ult_p = 0.0
ult_b = 0.0
ult_p_ready = False
ult_b_ready = False
ult_p_flash = 0.0
ult_b_flash = 0.0
ult_p_supertext = 0.0
ult_p_base = ULT_PLAYER_BASE * random.uniform(0.90, 1.10)
ult_b_base = ULT_BOT_BASE * random.uniform(0.85, 1.15)
ult_bonus_cd_p = 0.0
ult_bonus_cd_b = 0.0
ult_consume_cd_p = 0.0
ult_consume_cd_b = 0.0
player_target = Vec2(player.pos.x, player.pos.y)
menu_panel = pygame.Rect(W // 2 - 180, H // 2 - 110, 360, 260)
btn_play = pygame.Rect(menu_panel.x + 34, menu_panel.y + 44, 292, 44)
btn_settings = pygame.Rect(menu_panel.x + 34, menu_panel.y + 102, 292, 44)
btn_instr = pygame.Rect(menu_panel.x + 34, menu_panel.y + 160, 292, 44)
btn_exit = pygame.Rect(menu_panel.x + 34, menu_panel.y + 214, 292, 36)
settings_panel = pygame.Rect(W // 2 - 230, H // 2 - 170, 460, 360)
diff_buttons = {
"easy": pygame.Rect(settings_panel.x + 18, settings_panel.y + 76, 130, 34),
"normal": pygame.Rect(settings_panel.x + 164, settings_panel.y + 76, 130, 34),
"expert": pygame.Rect(settings_panel.x + 310, settings_panel.y + 76, 130, 34),
}
fps_input_rect = pygame.Rect(settings_panel.x + 18, settings_panel.y + 156, 230, 34)
volume_slider = pygame.Rect(settings_panel.x + 18, settings_panel.y + 252, 424, 16)
btn_back = pygame.Rect(settings_panel.x + 18, settings_panel.y + 300, 130, 38)
btn_pause_menu = pygame.Rect(settings_panel.x + 164, settings_panel.y + 300, 130, 38)
btn_pause_exit = pygame.Rect(settings_panel.x + 310, settings_panel.y + 300, 130, 38)
instr_panel = pygame.Rect(W // 2 - 300, H // 2 - 210, 600, 420)
btn_instr_back = pygame.Rect(instr_panel.x + 18, instr_panel.y + 360, 130, 38)
def apply_fps_text():
nonlocal fps_cap
try:
v = int(fps_text) if fps_text.strip() else 60
except Exception:
v = 60
if v <= 0:
fps_cap = 0
else:
fps_cap = max(1, min(1000, v))
def play_music():
if soundtrack_ok:
try:
pygame.mixer.music.set_volume(music_volume)
pygame.mixer.music.play(-1)
except Exception:
pass
def stop_music():
if soundtrack_ok:
try:
pygame.mixer.music.stop()
except Exception:
pass
def is_accurate_shot_towards_bot(puck_vel: Vec2, puck_pos: Vec2):
if puck_vel.y >= -160:
return False
gx1 = W // 2 - GOAL_W // 2
gx2 = W // 2 + GOAL_W // 2
return (gx1 - 34 <= puck_pos.x <= gx2 + 34)
def is_accurate_shot_towards_player(puck_vel: Vec2, puck_pos: Vec2):
if puck_vel.y <= 160:
return False
gx1 = W // 2 - GOAL_W // 2
gx2 = W // 2 + GOAL_W // 2
return (gx1 - 34 <= puck_pos.x <= gx2 + 34)
state = "SPLASH"
splash_reveal = 0.0
splash_hold = 0.0
splash_boom = 0.0
intro_started = False
title_surf = title_font.render("Puck Arena", True, WHITE)
game_state = "START"
start_timer = START_COUNTDOWN_SEC
banner = ""
banner_color = WHITE
goal_timer = 0.0
win_timer = 0.0
show_scoreline = False
def start_game_countdown():
nonlocal game_state, start_timer, paused
game_state = "START"
start_timer = START_COUNTDOWN_SEC
paused = False
player.vel = Vec2(0, 0)
bot.vel = Vec2(0, 0)
puck.vel = Vec2(0, 0)
stop_music()
def set_game_play():
nonlocal game_state
game_state = "PLAY"
play_music()
def start_goal_banner(msg, color):
nonlocal game_state, banner, banner_color, goal_timer, show_scoreline
game_state = "GOAL"
banner = msg
banner_color = color
goal_timer = GOAL_BANNER_SEC
show_scoreline = True
player.vel = Vec2(0, 0)
bot.vel = Vec2(0, 0)
puck.vel = Vec2(0, 0)
def start_win_countdown(msg, color):
nonlocal game_state, banner, banner_color, win_timer, show_scoreline
game_state = "WIN"
banner = msg
banner_color = color
win_timer = WIN_COUNTDOWN_SEC
show_scoreline = False
player.vel = Vec2(0, 0)
bot.vel = Vec2(0, 0)
puck.vel = Vec2(0, 0)
apply_fps_text()
running = True
while running:
dt = clock.tick(fps_cap) / 1000.0
if dt > 0.05:
dt = 0.05
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_F3:
show_debug = not show_debug
if (state in ("SETTINGS",) or (state == "GAME" and paused)) and fps_focus:
if e.key == pygame.K_ESCAPE:
fps_focus = False
elif e.key == pygame.K_RETURN or e.key == pygame.K_KP_ENTER:
apply_fps_text()
fps_focus = False
elif e.key == pygame.K_BACKSPACE:
fps_text = fps_text[:-1]
else:
ch = e.unicode
if ch.isdigit():
if len(fps_text) < 4:
fps_text += ch
continue
if e.key == pygame.K_ESCAPE:
if state == "SETTINGS":
apply_fps_text()
fps_focus = False
dragging_volume = False
state = "MENU"
elif state == "INSTRUCTIONS":
state = "MENU"
elif state == "GAME" and paused:
paused = False
dragging_volume = False
apply_fps_text()
if game_state == "PLAY":
music_resume()
elif e.type == pygame.MOUSEMOTION:
mx, my = e.pos
if state == "GAME":
player_target.x = mx
player_target.y = my
if (state == "SETTINGS") or (state == "GAME" and paused):
if dragging_volume:
music_volume = clamp((mx - volume_slider.x) / max(1, volume_slider.w), 0.0, 1.0)
if soundtrack_ok:
try:
pygame.mixer.music.set_volume(music_volume)
except Exception:
pass
elif e.type == pygame.MOUSEBUTTONDOWN and e.button == 1:
mx, my = e.pos
if state == "MENU":
if btn_play.collidepoint(mx, my):
ui_click()
score_player, score_bot = 0, 0
confetti.parts = []
sparks.parts = []
reset_round(puck, player, bot)
ai.reset()
player_target = Vec2(player.pos.x, player.pos.y)
ult_p = 0.0
ult_b = 0.0
ult_p_ready = False
ult_b_ready = False
ult_p_flash = 0.0
ult_b_flash = 0.0
ult_p_supertext = 0.0
ult_p_base = ULT_PLAYER_BASE * random.uniform(0.90, 1.10)
ult_b_base = ULT_BOT_BASE * random.uniform(0.85, 1.15)
ult_bonus_cd_p = 0.0
ult_bonus_cd_b = 0.0
ult_consume_cd_p = 0.0
ult_consume_cd_b = 0.0
start_game_countdown()
state = "GAME"
elif btn_settings.collidepoint(mx, my):
ui_click()
state = "SETTINGS"
fps_focus = False
dragging_volume = False
elif btn_instr.collidepoint(mx, my):
ui_click()
state = "INSTRUCTIONS"
elif btn_exit.collidepoint(mx, my):
ui_click()
running = False
elif state == "SETTINGS":
dragging_volume = False
if fps_input_rect.collidepoint(mx, my):
ui_click()
fps_focus = True
else:
if fps_focus:
apply_fps_text()
fps_focus = False
if volume_slider.collidepoint(mx, my):
ui_click()
dragging_volume = True
music_volume = clamp((mx - volume_slider.x) / max(1, volume_slider.w), 0.0, 1.0)
if soundtrack_ok:
try:
pygame.mixer.music.set_volume(music_volume)
except Exception:
pass
for k, r in diff_buttons.items():
if r.collidepoint(mx, my):
ui_click()
difficulty = k
ai.set_difficulty(k)
ai.reset()
base = ULT_BOT_BASE
if k == "easy":
base *= 0.92
elif k == "expert":
base *= 1.06
ult_b_base = base * random.uniform(0.85, 1.15)
if btn_back.collidepoint(mx, my):
ui_click()
apply_fps_text()
fps_focus = False
dragging_volume = False
state = "MENU"
elif state == "INSTRUCTIONS":
if btn_instr_back.collidepoint(mx, my):
ui_click()
state = "MENU"
elif state == "GAME":
if pause_icon.collidepoint(mx, my) and game_state not in ("WIN", "START"):
ui_click()
paused = not paused
dragging_volume = False
fps_focus = False
if paused: