-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPacDot.py
More file actions
1856 lines (1595 loc) · 59.9 KB
/
Copy pathPacDot.py
File metadata and controls
1856 lines (1595 loc) · 59.9 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
#!/usr/bin/env python
#------------------------------------------------------------------------------
# PACDOT — half-screen (32x32) on a 64x32 LED matrix
#
# Left half : playfield (Pac, ghosts, dots, pills)
# Right half : HUD (score / level / high score)
#
# Ported from ArcadeRetroClockHD PlayPacDot AI loop, adapted for LEDarcade
# and the 64x32 panel layout.
#------------------------------------------------------------------------------
import LEDarcade as LED
import copy
import math
import random
import time
from datetime import datetime
from random import randint
LED.Initialize()
#------------------------------------------------------------------------------
# Layout
#------------------------------------------------------------------------------
PF_H0 = 0
PF_V0 = 0
PF_W = 32
PF_H = 32
HUD_H0 = 32 # right half starts here
#------------------------------------------------------------------------------
# Tunables
#------------------------------------------------------------------------------
NUM_DOTS_DEFAULT = 220
POWER_PILLS = 5
PAC_SPEED = 5
START_GHOST_SPEED1 = 5
START_GHOST_SPEED2 = 6
START_GHOST_SPEED3 = 7
START_GHOST_SPEED4 = 10
BLUE_GHOST_SPEED = 15
BLUE_GHOST_MOVES = 500
MAX_MOVES = 10000
PAC_STUCK_MAX = 300
MAIN_SLEEP = 0.008
HUD_REFRESH_EVERY = 8
DOT_POINTS = 1
PILL_POINTS = 10
BLUE_GHOST_POINTS = 5
# Title intro (Skyfall-style letters, horizontal slide from sides)
TITLE_WORD = "PACDOT"
TITLE_LETTER_ZOOM = 2
TITLE_LETTER_GAP = 1
TITLE_LETTER_RGB = (220, 200, 40) # pac yellow
TITLE_LETTER_SHADOW_RGB = (40, 35, 5)
TITLE_LETTER_STAGGER = 0.18
TITLE_SLIDE_SPEED = 1.35 # pixels per frame-unit
TITLE_HOLD_SECONDS = 1.6
TITLE_INTRO_MAX_SECONDS = 12.0
#------------------------------------------------------------------------------
# Runtime state
#------------------------------------------------------------------------------
PowerPillActive = 0
PowerPillMoves = 0
PacDotScore = 0
PacDotHighScore = 0
PacDotGamesPlayed = 0
LevelCount = 1
Ghost1Alive = Ghost2Alive = Ghost3Alive = Ghost4Alive = 1
Ghost1H = Ghost1V = 0
Ghost2H = Ghost2V = 0
Ghost3H = Ghost3V = 0
Ghost4H = Ghost4V = 0
# Ghost train: free ghosts that touch Pac latch on and follow like a snake.
# GhostTrain is ordered list of ghost ids (1–4); first = immediately behind Pac.
Ghost1Attached = Ghost2Attached = Ghost3Attached = Ghost4Attached = 0
GhostTrain = []
PacDead = False
PacLives = 3
PAC_START_LIVES = 3
Pacmoves = 0
#------------------------------------------------------------------------------
# Geometry helpers
#------------------------------------------------------------------------------
def pf_in_bounds(h, v):
return (PF_H0 <= h < PF_H0 + PF_W) and (PF_V0 <= v < PF_V0 + PF_H)
def CalculateMovement(h, v, direction):
# 1N 2E 3S 4W
if direction == 1:
v -= 1
elif direction == 2:
h += 1
elif direction == 3:
v += 1
elif direction == 4:
h -= 1
return h, v, direction
def TurnTowardsDot4Way(source_h, source_v, source_direction, target_h, target_v):
x = source_h - target_h
y = source_v - target_v
if abs(y) >= abs(x):
return 3 if y <= 0 else 1
return 2 if x <= 0 else 4
def TurnAwayFromDot4Way(source_h, source_v, source_direction, target_h, target_v):
x = source_h - target_h
y = source_v - target_v
if abs(y) >= abs(x):
return 1 if y <= 0 else 3
return 4 if x <= 0 else 2
#------------------------------------------------------------------------------
# Scan / draw
#------------------------------------------------------------------------------
def ScanDot(h, v):
"""Classify a cell by color + DotMatrix. Outside playfield = boundary."""
if not pf_in_bounds(h, v):
return "boundary"
r, g, b = LED.getpixel(h, v)
try:
dm = LED.DotMatrix[h][v]
except Exception:
dm = 0
if dm == 2:
item = "pill"
elif r == LED.DotR and g == LED.DotG and b == LED.DotB:
item = "dot"
elif r == LED.PillR and g == LED.PillG and b == LED.PillB:
item = "pill"
elif (r == LED.Ghost1R and g == LED.Ghost1G and b == LED.Ghost1B) or \
(r == LED.Ghost2R and g == LED.Ghost2G and b == LED.Ghost2B) or \
(r == LED.Ghost3R and g == LED.Ghost3G and b == LED.Ghost3B) or \
(r == LED.Ghost4R and g == LED.Ghost4G and b == LED.Ghost4B):
item = "ghost"
elif r == LED.PacR and g == LED.PacG and b == LED.PacB:
item = "pacdot"
elif r == LED.BlueGhostR and g == LED.BlueGhostG and b == LED.BlueGhostB:
item = "blueghost"
elif r == LED.WallR and g == LED.WallG and b == LED.WallB:
item = "wall"
else:
item = "empty"
if dm == 1:
item = "dot"
elif dm == 2:
item = "pill"
return item
def ScanBox(h, v, direction):
scan_hit = "NULL"
# Front
if scan_hit == "NULL":
sh, sv, sd = CalculateMovement(h, v, direction)
item = ScanDot(sh, sv)
if item == "dot":
scan_hit = "frontdot"
elif item == "ghost":
scan_hit = "frontghost"
elif item == "blueghost":
scan_hit = "frontblueghost"
elif item == "pill":
scan_hit = "frontpill"
elif item == "wall":
scan_hit = "frontwall"
# Left
if scan_hit == "NULL":
sd = LED.TurnLeft(direction)
sh, sv, sd = CalculateMovement(h, v, sd)
item = ScanDot(sh, sv)
if item == "dot":
scan_hit = "leftdot"
elif item == "ghost":
scan_hit = "leftghost"
elif item == "blueghost":
scan_hit = "leftblueghost"
elif item == "pill":
scan_hit = "leftpill"
elif item == "wall":
scan_hit = "leftwall"
# Right
if scan_hit == "NULL":
sd = LED.TurnRight(direction)
sh, sv, sd = CalculateMovement(h, v, sd)
item = ScanDot(sh, sv)
if item == "dot":
scan_hit = "rightdot"
elif item == "ghost":
scan_hit = "rightghost"
elif item == "blueghost":
scan_hit = "rightblueghost"
elif item == "pill":
scan_hit = "rightpill"
elif item == "wall":
scan_hit = "rightwall"
if scan_hit == "NULL":
scan_hit = "empty"
return scan_hit
def FollowScanner(h, v, direction):
scan_hit = ScanBox(h, v, direction)
if scan_hit == "leftblueghost":
return LED.TurnLeft(direction)
if scan_hit == "rightblueghost":
return LED.TurnRight(direction)
if scan_hit == "frontblueghost":
return direction
if scan_hit == "leftpill":
return LED.TurnLeft(direction)
if scan_hit == "frontpill":
return direction
if scan_hit == "rightpill":
return LED.TurnRight(direction)
if scan_hit == "leftdot":
return LED.TurnLeft(direction)
if scan_hit == "rightdot":
return LED.TurnRight(direction)
if scan_hit == "frontdot":
return direction
if scan_hit == "frontghost":
return LED.ReverseDirection(direction)
if scan_hit == "frontwall":
sd = LED.TurnRight(direction)
sh, sv, sd = CalculateMovement(h, v, sd)
item = ScanDot(sh, sv)
if item in ("empty", "pill", "blueghost", "dot"):
return sd
sd = LED.TurnLeft(direction)
sh, sv, sd = CalculateMovement(h, v, sd)
item = ScanDot(sh, sv)
if item in ("empty", "pill", "blueghost", "dot"):
return sd
return LED.ReverseDirection(direction)
return direction
def DrawGhost(h, v, r, g, b):
global PowerPillActive
if PowerPillActive == 1:
LED.setpixel(h, v, LED.BlueGhostR, LED.BlueGhostG, LED.BlueGhostB)
else:
LED.setpixel(h, v, r, g, b)
return h, v
def DrawPacDot(h, v, r, g, b):
LED.setpixel(h, v, r, g, b)
return h, v
def is_border_cell(h, v):
"""True if (h,v) is on the playfield blue wall ring."""
if not pf_in_bounds(h, v):
return False
return (
h == PF_H0 or h == PF_H0 + PF_W - 1 or
v == PF_V0 or v == PF_V0 + PF_H - 1
)
def DrawPlayfieldBorder():
"""Full solid blue wall around the playfield, then lives on the bottom edge."""
wr, wg, wb = LED.WallR, LED.WallG, LED.WallB
# Top + bottom rows (full width, including corners)
for h in range(PF_H0, PF_H0 + PF_W):
LED.setpixel(h, PF_V0, wr, wg, wb)
LED.setpixel(h, PF_V0 + PF_H - 1, wr, wg, wb)
# Left + right columns (corners written twice — fine)
for v in range(PF_V0, PF_V0 + PF_H):
LED.setpixel(PF_H0, v, wr, wg, wb)
LED.setpixel(PF_H0 + PF_W - 1, v, wr, wg, wb)
# Lives sit on the bottom wall (yellow pac dots)
DrawLivesIndicator()
def ClearPlayfield():
for h in range(PF_H0, PF_H0 + PF_W):
for v in range(PF_V0, PF_V0 + PF_H):
LED.setpixel(h, v, 0, 0, 0)
def ClearHUD():
for h in range(HUD_H0, LED.HatWidth):
for v in range(0, LED.HatHeight):
LED.setpixel(h, v, 0, 0, 0)
def ResetDotMatrix():
LED.DotMatrix = [[0 for _ in range(LED.HatHeight)] for _ in range(LED.HatWidth)]
def DrawDots(num_dots):
if num_dots < 5:
num_dots = 5
max_cells = (PF_W - 2) * (PF_H - 2)
if num_dots > max_cells - 10:
num_dots = max_cells - 10
placed = 0
tries = 0
while placed < num_dots and tries < 20000:
tries += 1
h = randint(PF_H0 + 1, PF_H0 + PF_W - 2)
v = randint(PF_V0 + 1, PF_V0 + PF_H - 2)
if LED.DotMatrix[h][v] == 1:
continue
r, g, b = LED.getpixel(h, v)
if r == 0 and g == 0 and b == 0:
LED.DotMatrix[h][v] = 1
LED.setpixel(h, v, LED.DotR, LED.DotG, LED.DotB)
placed += 1
return placed
def DrawPowerPills(count):
placed = 0
tries = 0
while placed < count and tries < 5000:
tries += 1
h = randint(PF_H0 + 1, PF_H0 + PF_W - 2)
v = randint(PF_V0 + 1, PF_V0 + PF_H - 2)
if LED.DotMatrix[h][v] == 1:
LED.DotMatrix[h][v] = 2
LED.setpixel(h, v, LED.PillR, LED.PillG, LED.PillB)
placed += 1
def DrawDotMatrix():
n = 0
for h in range(PF_H0, PF_H0 + PF_W):
for v in range(PF_V0, PF_V0 + PF_H):
if is_border_cell(h, v):
continue
if LED.DotMatrix[h][v] == 1:
n += 1
LED.setpixel(h, v, LED.DotR, LED.DotG, LED.DotB)
elif LED.DotMatrix[h][v] == 2:
LED.setpixel(h, v, LED.PillR, LED.PillG, LED.PillB)
return n
def CountDotsRemaining():
n = 0
for h in range(PF_H0, PF_H0 + PF_W):
for v in range(PF_V0, PF_V0 + PF_H):
if LED.DotMatrix[h][v] == 1:
n += 1
return n
def FindClosestDot(pac_h, pac_v):
global PowerPillActive
closest_x = PF_H0 + PF_W // 2
closest_y = PF_V0 + PF_H // 2
min_dist = 9999
for x in range(PF_H0, PF_H0 + PF_W):
for y in range(PF_V0, PF_V0 + PF_H):
dm = LED.DotMatrix[x][y]
if dm == 1:
dist = LED.GetDistanceBetweenDots(pac_h, pac_v, x, y)
if dist <= min_dist:
min_dist = dist
closest_x, closest_y = x, y
elif dm == 2 and PowerPillActive == 0:
dist = LED.GetDistanceBetweenDots(pac_h, pac_v, x, y)
if dist < min_dist:
min_dist = dist
closest_x, closest_y = x, y
return closest_x, closest_y
#------------------------------------------------------------------------------
# Ghost train + movement
#------------------------------------------------------------------------------
def _ghost_rgb(gid):
if gid == 1:
return LED.Ghost1R, LED.Ghost1G, LED.Ghost1B
if gid == 2:
return LED.Ghost2R, LED.Ghost2G, LED.Ghost2B
if gid == 3:
return LED.Ghost3R, LED.Ghost3G, LED.Ghost3B
return LED.Ghost4R, LED.Ghost4G, LED.Ghost4B
def _get_ghost_pos(gid):
if gid == 1:
return Ghost1H, Ghost1V
if gid == 2:
return Ghost2H, Ghost2V
if gid == 3:
return Ghost3H, Ghost3V
return Ghost4H, Ghost4V
def _set_ghost_pos(gid, h, v):
global Ghost1H, Ghost1V, Ghost2H, Ghost2V, Ghost3H, Ghost3V, Ghost4H, Ghost4V
if gid == 1:
Ghost1H, Ghost1V = h, v
elif gid == 2:
Ghost2H, Ghost2V = h, v
elif gid == 3:
Ghost3H, Ghost3V = h, v
else:
Ghost4H, Ghost4V = h, v
def _is_attached(gid):
if gid == 1:
return Ghost1Attached
if gid == 2:
return Ghost2Attached
if gid == 3:
return Ghost3Attached
return Ghost4Attached
def _set_attached(gid, value):
global Ghost1Attached, Ghost2Attached, Ghost3Attached, Ghost4Attached
if gid == 1:
Ghost1Attached = value
elif gid == 2:
Ghost2Attached = value
elif gid == 3:
Ghost3Attached = value
else:
Ghost4Attached = value
def _is_alive(gid):
if gid == 1:
return Ghost1Alive
if gid == 2:
return Ghost2Alive
if gid == 3:
return Ghost3Alive
return Ghost4Alive
def _set_alive(gid, value):
global Ghost1Alive, Ghost2Alive, Ghost3Alive, Ghost4Alive
if gid == 1:
Ghost1Alive = value
elif gid == 2:
Ghost2Alive = value
elif gid == 3:
Ghost3Alive = value
else:
Ghost4Alive = value
def IdentifyGhost(h, v):
"""Return ghost id (1–4) at cell, or 0."""
if Ghost1Alive and Ghost1H == h and Ghost1V == v:
return 1
if Ghost2Alive and Ghost2H == h and Ghost2V == v:
return 2
if Ghost3Alive and Ghost3H == h and Ghost3V == v:
return 3
if Ghost4Alive and Ghost4H == h and Ghost4V == v:
return 4
return 0
def RestoreCell(h, v):
"""Redraw a playfield cell after a train car / explosion leaves it."""
if not pf_in_bounds(h, v):
return
if is_border_cell(h, v):
LED.setpixel(h, v, LED.WallR, LED.WallG, LED.WallB)
return
if LED.DotMatrix[h][v] == 1:
LED.setpixel(h, v, LED.DotR, LED.DotG, LED.DotB)
elif LED.DotMatrix[h][v] == 2:
LED.setpixel(h, v, LED.PillR, LED.PillG, LED.PillB)
else:
LED.setpixel(h, v, 0, 0, 0)
def AttachGhost(gid):
"""Latch a free ghost onto the end of Pac's train."""
global GhostTrain
if gid < 1 or gid > 4:
return False
if not _is_alive(gid) or _is_attached(gid):
return False
if gid in GhostTrain:
return False
_set_attached(gid, 1)
GhostTrain.append(gid)
print("[PacDot] Ghost {} attached train={}".format(gid, GhostTrain))
return True
def DetachGhost(gid):
global GhostTrain
_set_attached(gid, 0)
if gid in GhostTrain:
GhostTrain.remove(gid)
def ReleaseAllAttachedGhosts():
"""Power pill frees the train — ghosts become blue free roamers."""
global GhostTrain
for gid in list(GhostTrain):
_set_attached(gid, 0)
gh, gv = _get_ghost_pos(gid)
DrawGhost(gh, gv, *_ghost_rgb(gid))
GhostTrain = []
def ResetGhostTrain():
global GhostTrain
global Ghost1Attached, Ghost2Attached, Ghost3Attached, Ghost4Attached
GhostTrain = []
Ghost1Attached = Ghost2Attached = Ghost3Attached = Ghost4Attached = 0
def KillGhost(h, v):
"""Kill ghost at cell (eaten while blue). Detach if it was in the train."""
gid = IdentifyGhost(h, v)
if gid == 0:
# Fall back to position match without alive check edge cases
global Ghost1Alive, Ghost2Alive, Ghost3Alive, Ghost4Alive
global Ghost1H, Ghost1V, Ghost2H, Ghost2V, Ghost3H, Ghost3V, Ghost4H, Ghost4V
if h == Ghost1H and v == Ghost1V:
gid = 1
elif h == Ghost2H and v == Ghost2V:
gid = 2
elif h == Ghost3H and v == Ghost3V:
gid = 3
elif h == Ghost4H and v == Ghost4V:
gid = 4
if gid:
DetachGhost(gid)
_set_alive(gid, 0)
def UpdateGhostTrain(old_pac_h, old_pac_v):
"""
Snake/train follow: first attached ghost takes Pac's previous cell,
each next car takes the previous car's old cell.
"""
global GhostTrain
if not GhostTrain:
return
# Snapshot current train car positions (before move)
old_positions = [(old_pac_h, old_pac_v)]
for gid in GhostTrain:
old_positions.append(_get_ghost_pos(gid))
# Clear previous car pixels (restore dots)
for gid in GhostTrain:
gh, gv = _get_ghost_pos(gid)
RestoreCell(gh, gv)
# Assign new positions along the chain
for i, gid in enumerate(GhostTrain):
nh, nv = old_positions[i]
_set_ghost_pos(gid, nh, nv)
r, g, b = _ghost_rgb(gid)
DrawGhost(nh, nv, r, g, b)
def DrawGhostTrain():
"""Redraw all attached ghosts (after power-pill recolor etc.)."""
for gid in GhostTrain:
if not _is_alive(gid):
continue
gh, gv = _get_ghost_pos(gid)
DrawGhost(gh, gv, *_ghost_rgb(gid))
def AllGhostsAttached():
"""True when all four living ghosts are latched (or all four slots filled)."""
if len(GhostTrain) >= 4:
return True
# Count living free ghosts — if none free and at least one attached and all living are attached
living = [g for g in (1, 2, 3, 4) if _is_alive(g)]
if not living:
return False
return all(_is_attached(g) for g in living) and len(living) >= 4
def ShowBlueGhostExplosion(h, v):
"""
Small explosion when Pac eats a blue ghost.
Uses LED.SmallExplosion (3x3 ColorAnimatedSprite) centered on the cell.
"""
eh = h - 1 # center 3x3 on ghost pixel
ev = v - 1
try:
boom = copy.deepcopy(LED.SmallExplosion)
boom.currentframe = 1
boom.h = eh
boom.v = ev
boom.Animate(eh, ev, "forward", 0.035)
except Exception as e:
print(f"[PacDot] SmallExplosion fallback: {e}")
for br in (220, 140, 60, 0):
LED.setpixel(h, v, br, br, min(255, br + 40))
time.sleep(0.03)
for dy in range(-1, 2):
for dx in range(-1, 2):
x, y = h + dx, v + dy
if not pf_in_bounds(x, y):
continue
RestoreCell(x, y)
# Explosions near the edge punch wall pixels — reseal the ring
DrawPlayfieldBorder()
def ShowPacMegaExplosion(h, v):
"""
Bigger death explosion when the full ghost train attaches.
Uses PlayerShipExplosion (5x5) then SmallExplosion for extra spark.
"""
print("[PacDot] MEGA EXPLOSION at {},{}".format(h, v))
try:
big = copy.deepcopy(LED.PlayerShipExplosion)
big.currentframe = 1
big.h = h - 2
big.v = v - 2
big.Animate(h - 2, v - 2, "forward", 0.04)
except Exception as e:
print(f"[PacDot] PlayerShipExplosion failed: {e}")
try:
big2 = copy.deepcopy(LED.BigShipExplosion)
big2.currentframe = 1
big2.Animate(h - 4, v - 2, "forward", 0.04)
except Exception as e2:
print(f"[PacDot] BigShipExplosion failed: {e2}")
# Secondary spark
try:
spark = copy.deepcopy(LED.SmallExplosion)
spark.currentframe = 1
spark.Animate(h - 1, v - 1, "forward", 0.03)
except Exception:
pass
# Wipe local area clean
for dy in range(-3, 4):
for dx in range(-3, 4):
x, y = h + dx, v + dy
if pf_in_bounds(x, y):
RestoreCell(x, y)
DrawPlayfieldBorder()
def MoveGhost(h, v, direction, r, g, b, ghost_id):
"""Move a free (non-attached) ghost. Touching Pac attaches to the train."""
global PowerPillActive
if _is_attached(ghost_id):
return h, v, direction
newh, newv, direction = CalculateMovement(h, v, direction)
item = ScanDot(newh, newv)
if item in ("wall", "pill", "ghost", "boundary"):
direction = randint(1, 4)
return h, v, direction
if item in ("empty", "dot"):
if PowerPillActive == 1:
LED.setpixel(newh, newv, LED.BlueGhostR, LED.BlueGhostG, LED.BlueGhostB)
else:
LED.setpixel(newh, newv, r, g, b)
if LED.DotMatrix[h][v] == 1:
LED.setpixel(h, v, LED.DotR, LED.DotG, LED.DotB)
elif LED.DotMatrix[h][v] == 2:
LED.setpixel(h, v, LED.PillR, LED.PillG, LED.PillB)
else:
LED.setpixel(h, v, 0, 0, 0)
return newh, newv, direction
if item == "pacdot":
if PowerPillActive == 0:
# Latch onto Pac's train; stay put until train update places us
AttachGhost(ghost_id)
return h, v, direction
return h, v, direction
def MovePacDot(h, v, direction, r, g, b, dots_eaten):
global Pacmoves, PowerPillActive, PacDotScore
global Ghost1Alive, Ghost2Alive, Ghost3Alive, Ghost4Alive
global Ghost1H, Ghost1V, Ghost2H, Ghost2V, Ghost3H, Ghost3V, Ghost4H, Ghost4V
Pacmoves += 1
newh, newv, direction = CalculateMovement(h, v, direction)
item = ScanDot(newh, newv)
if item == "dot":
dots_eaten += 1
Pacmoves = 0
PacDotScore += DOT_POINTS
LED.setpixel(newh, newv, r, g, b)
LED.setpixel(h, v, 0, 0, 0)
LED.DotMatrix[newh][newv] = 0
elif item == "pill":
Pacmoves = 0
PacDotScore += PILL_POINTS
LED.setpixel(newh, newv, r, g, b)
LED.setpixel(h, v, 0, 0, 0)
LED.DotMatrix[newh][newv] = 0
PowerPillActive = 1
# Power pill shakes free any attached ghosts
ReleaseAllAttachedGhosts()
if Ghost1Alive == 1:
DrawGhost(Ghost1H, Ghost1V, LED.Ghost1R, LED.Ghost1G, LED.Ghost1B)
if Ghost2Alive == 1:
DrawGhost(Ghost2H, Ghost2V, LED.Ghost2R, LED.Ghost2G, LED.Ghost2B)
if Ghost3Alive == 1:
DrawGhost(Ghost3H, Ghost3V, LED.Ghost3R, LED.Ghost3G, LED.Ghost3B)
if Ghost4Alive == 1:
DrawGhost(Ghost4H, Ghost4V, LED.Ghost4R, LED.Ghost4G, LED.Ghost4B)
elif item in ("wall", "boundary"):
Pacmoves = 0
direction = randint(1, 4)
newh, newv = h, v
elif item == "blueghost":
Pacmoves = 0
PacDotScore += BLUE_GHOST_POINTS
LED.setpixel(h, v, 0, 0, 0)
LED.DotMatrix[newh][newv] = 0
KillGhost(newh, newv)
ShowBlueGhostExplosion(newh, newv)
LED.setpixel(newh, newv, r, g, b)
elif item == "ghost":
gid = IdentifyGhost(newh, newv)
if PowerPillActive == 1:
Pacmoves = 0
PacDotScore += BLUE_GHOST_POINTS
LED.setpixel(h, v, 0, 0, 0)
KillGhost(newh, newv)
ShowBlueGhostExplosion(newh, newv)
LED.setpixel(newh, newv, r, g, b)
else:
# Ghost latches on; Pac keeps moving (train follows on next tick)
if gid:
AttachGhost(gid)
# Pac can still move onto empty-looking logic: stay put one step
# so we don't stack pixels; turn slightly scared
direction = LED.TurnLeftOrRight(direction)
newh, newv = h, v
elif item == "empty":
LED.setpixel(newh, newv, r, g, b)
LED.setpixel(h, v, 0, 0, 0)
return newh, newv, direction, dots_eaten
#------------------------------------------------------------------------------
# Title intro — letters slide in from left/right (Skyfall-inspired)
#------------------------------------------------------------------------------
def _title_letter_sprite(char):
ch = char.upper()
if not ("A" <= ch <= "Z"):
return None
idx = ord(ch) - ord("A")
return LED.TrimSprite(copy.deepcopy(LED.AlphaSpriteList[idx]))
def _sprite_pixels_zoomed(sprite, zoom, rgb, shadow_rgb):
pixels = []
shadow_pixels = []
sw, sh = sprite.width, sprite.height
for count in range(sw * sh):
if sprite.grid[count] == 0:
continue
y, x = divmod(count, sw)
for zv in range(zoom):
for zh in range(zoom):
pixels.append((x * zoom + zh, y * zoom + zv, rgb))
shadow_pixels.append((x * zoom + zh + 1, y * zoom + zv + 1, shadow_rgb))
return pixels, shadow_pixels, sw * zoom, sh * zoom
class SlideLetter:
"""Banner letter that slides horizontally from a screen edge to rest_x."""
def __init__(self, char, pixels, shadow_pixels, width, height,
rest_x, rest_y, start_x, drop_delay, from_left):
self.char = char
self.pixels = pixels
self.shadow_pixels = shadow_pixels
self.width = width
self.height = height
self.rest_x = float(rest_x)
self.rest_y = float(rest_y)
self.x = float(start_x)
self.y = float(rest_y)
self.drop_delay = drop_delay
self.from_left = from_left
self.started = False
self.settled = False
def update(self, step, elapsed, speed):
if self.settled:
self.x = self.rest_x
return
if elapsed < self.drop_delay:
return
self.started = True
# Ease-out approach toward rest_x
dx = self.rest_x - self.x
if abs(dx) < 0.5:
self.x = self.rest_x
self.settled = True
return
# Move a fraction of remaining distance + min step so we always progress
move = max(0.4, abs(dx) * 0.22) * speed * step
if move > abs(dx):
move = abs(dx)
self.x += move if dx > 0 else -move
if abs(self.rest_x - self.x) < 0.5:
self.x = self.rest_x
self.settled = True
def force_settle(self):
self.x = self.rest_x
self.y = self.rest_y
self.started = True
self.settled = True
def draw(self, canvas, panel_w, panel_h):
sx = int(round(self.x))
sy = int(round(self.y))
set_pixel = canvas.SetPixel
for dx, dy, rgb in self.shadow_pixels:
px, py = sx + dx, sy + dy
if 0 <= px < panel_w and 0 <= py < panel_h:
set_pixel(px, py, *rgb)
for dx, dy, rgb in self.pixels:
px, py = sx + dx, sy + dy
if 0 <= px < panel_w and 0 <= py < panel_h:
set_pixel(px, py, *rgb)
def _build_title_letters(panel_w, panel_h):
specs = []
for char in TITLE_WORD:
sprite = _title_letter_sprite(char)
if sprite is None:
continue
pixels, shadow_pixels, letter_w, letter_h = _sprite_pixels_zoomed(
sprite, TITLE_LETTER_ZOOM, TITLE_LETTER_RGB, TITLE_LETTER_SHADOW_RGB,
)
specs.append((char, pixels, shadow_pixels, letter_w, letter_h))
if not specs:
return []
total_width = sum(s[3] for s in specs) + TITLE_LETTER_GAP * max(0, len(specs) - 1)
start_x = max(0, (panel_w - total_width) // 2)
letter_height = max(s[4] for s in specs)
rest_y = max(0, (panel_h - letter_height) // 2)
letters = []
x_cursor = start_x
for index, (char, pixels, shadow_pixels, letter_w, letter_h) in enumerate(specs):
from_left = (index % 2 == 0)
if from_left:
spawn_x = -letter_w - 4
else:
spawn_x = panel_w + 4
y_off = letter_height - letter_h
letters.append(SlideLetter(
char, pixels, shadow_pixels, letter_w, letter_h,
rest_x=x_cursor,
rest_y=rest_y + y_off,
start_x=spawn_x,
drop_delay=index * TITLE_LETTER_STAGGER,
from_left=from_left,
))
x_cursor += letter_w + TITLE_LETTER_GAP
return letters
def PlayPacDotTitleIntro(StopEvent=None):
"""Slide PACDOT letters in from left/right until centered, then hold."""
panel_w = LED.HatWidth
panel_h = LED.HatHeight
letters = _build_title_letters(panel_w, panel_h)
if not letters:
return
if StopEvent is not None and StopEvent.is_set():
print("[PacDot] Title intro skipped (StopEvent)")
return
print("[PacDot] Title intro — sliding letters from sides")
try:
canvas = LED.TheMatrix.CreateFrameCanvas()
except Exception:
canvas = None
start = time.time()
last_frame = start
hold_start = None
try:
while True:
if StopEvent is not None and StopEvent.is_set():
print("[PacDot] Title intro — StopEvent")
break
now = time.time()
elapsed = now - start
if elapsed >= TITLE_INTRO_MAX_SECONDS:
for letter in letters:
letter.force_settle()
break
frame_dt = max(0.001, now - last_frame)
last_frame = now
# Normalize to ~30fps step units (similar feel to Skyfall)
step = min(3.0, frame_dt * 30.0)
for letter in letters:
letter.update(step, elapsed, TITLE_SLIDE_SPEED)
if hold_start is None and all(letter.settled for letter in letters):
hold_start = now
if hold_start is not None and (now - hold_start) >= TITLE_HOLD_SECONDS:
break
if canvas is not None:
canvas.Fill(0, 0, 0)
for letter in letters:
if letter.started or letter.settled:
letter.draw(canvas, panel_w, panel_h)
canvas = LED.TheMatrix.SwapOnVSync(canvas)
else:
LED.ClearBigLED()
# Fallback: draw via setpixel
for letter in letters:
if not (letter.started or letter.settled):
continue
sx = int(round(letter.x))
sy = int(round(letter.y))
for dx, dy, rgb in letter.pixels:
LED.setpixel(sx + dx, sy + dy, *rgb)
time.sleep(0.03)
except KeyboardInterrupt:
pass
# Brief clear before game
try:
LED.ClearBigLED()
LED.ClearBuffers()
except Exception:
pass
#------------------------------------------------------------------------------
# Scores (ClockConfig.ini [scores], same pattern as SpaceDot / DotInvaders)
#------------------------------------------------------------------------------
def LoadPacDotScores():
"""Reload PacDot high score / games played from ClockConfig.ini."""
global PacDotHighScore, PacDotGamesPlayed
try:
LED.LoadConfigData()
PacDotHighScore = int(getattr(LED, "PacDotHighScore", 0) or 0)
PacDotGamesPlayed = int(getattr(LED, "PacDotGamesPlayed", 0) or 0)
except Exception as e: