-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRallyDot.py
More file actions
3941 lines (3264 loc) · 417 KB
/
Copy pathRallyDot.py
File metadata and controls
3941 lines (3264 loc) · 417 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
#------------------------------------------------------------------------------
# RALLYDOT — Rally-X style chase game for LEDarcade
#
# Ported from ArcadeRetroClockHD (16x16 Unicorn HD viewport) to 64x32.
# Large world map + camera centered on the player car.
#------------------------------------------------------------------------------
import copy
import math
import time
import random
from random import randint
import LEDarcade as LED
# Viewport = full panel (read live so ClockConfig 64x32 is honored after Initialize)
def _view_w():
return int(getattr(LED, "HatWidth", 64) or 64)
def _view_h():
return int(getattr(LED, "HatHeight", 32) or 32)
# Back-compat names used throughout ported code (updated each frame via refresh_view_size)
VIEW_W = 64
VIEW_H = 32
def refresh_view_size():
global VIEW_W, VIEW_H
VIEW_W = _view_w()
VIEW_H = _view_h()
# Tunables
KEYBOARD_SPEED = 25
CHECK_CLOCK_SPEED = 60 # moves between clock-sprite checks (HD global)
CHECK_TIME = 60 # seconds between clock displays
FRAME_SLEEP = 0.008
CPU_MODIFIER = 1
SCROLL_SLEEP = getattr(LED, "ScrollSleep", 0.03)
FLASH_SLEEP = getattr(LED, "FlashSleep", 0.01)
# Tick slowdown: cars/AI act when (moves % round(period * TICK_SCALE)) == 0.
# Higher = slower. Was 4; 3.2 = +25% object speed (player, enemies, AI).
TICK_SCALE = 3.2
# Extra slowdown for red enemy dots only (2 = half as fast as player-relative enemy pace)
ENEMY_SPEED_SCALE = 2
# Display: ColorList walls use "Low/Dark" RGB (~45–100). Run hot on the panel.
MATRIX_BRIGHTNESS = 100
DISPLAY_GAMMA = 2.55 # scale palette toward full LED output (clamped at 255)
def on_tick(moves_count, period):
"""True when this global move counter is an action tick for `period`."""
p = max(1, int(round(float(period) * TICK_SCALE)))
return (moves_count % p) == 0
def _apply_full_brightness():
"""Matrix at 100% + mark LED.Gamma for any late color uses."""
try:
LED.Gamma = 1.0
except Exception:
pass
try:
LED.TheMatrix.brightness = MATRIX_BRIGHTNESS
except Exception:
pass
print("[RallyDot] brightness={} display_gamma={}".format(
MATRIX_BRIGHTNESS, DISPLAY_GAMMA))
# HD aliases used by ported loop
CheckClockSpeed = CHECK_CLOCK_SPEED
CheckTime = CHECK_TIME
ClockOnDuration = 3
ClockOffDuration = max(1, CheckTime - ClockOnDuration)
ClockSlideSpeed = 1
# HD used a module-level move counter shared by helpers (e.g. TurnTowardsFuel...)
moves = 0
# Color aliases from LEDarcade
SDLowYellowR = LED.SDLowYellowR
SDLowYellowG = LED.SDLowYellowG
SDLowYellowB = LED.SDLowYellowB
SDLowRedR = LED.SDLowRedR
SDLowRedG = LED.SDLowRedG
SDLowRedB = LED.SDLowRedB
SDLowGreenR = LED.SDLowGreenR
SDLowGreenG = LED.SDLowGreenG
SDLowGreenB = LED.SDLowGreenB
SDMedPurpleR = getattr(LED, "SDMedPurpleR", 100)
SDMedPurpleG = getattr(LED, "SDMedPurpleG", 0)
SDMedPurpleB = getattr(LED, "SDMedPurpleB", 100)
def _stop(StopEvent):
return StopEvent is not None and StopEvent.is_set()
def poll_keyboard_safe():
"""Keyboard poll that is a no-op when not attached to a real TTY (nohup/sudo)."""
import sys
if not sys.stdin.isatty():
return ""
try:
return LED.PollKeyboard()
except Exception:
return ""
def _gamma_rgb(r, g, b):
if r or g or b:
r = min(255, int(r * DISPLAY_GAMMA))
g = min(255, int(g * DISPLAY_GAMMA))
b = min(255, int(b * DISPLAY_GAMMA))
return r, g, b
def _show():
"""Present the back-buffer canvas once (no direct TheMatrix writes)."""
try:
LED.Canvas = LED.TheMatrix.SwapOnVSync(LED.Canvas)
except Exception:
try:
LED.TheMatrix.SwapOnVSync(LED.Canvas)
except Exception:
pass
def setpixel(h, v, r, g, b):
"""Plot into canvas only (never TheMatrix.SetPixel — that causes double-draw/ghosts)."""
if 0 <= h < VIEW_W and 0 <= v < VIEW_H:
r, g, b = _gamma_rgb(r, g, b)
try:
LED.Canvas.SetPixel(h, v, r, g, b)
except Exception:
try:
LED.setpixelCanvas(h, v, r, g, b)
except Exception:
pass
try:
LED.ScreenArray[v][h] = (r, g, b)
except Exception:
pass
# Clock time overlay: dark translucent bar behind digits
CLOCK_BACKDROP_ALPHA = 0.62
CLOCK_BACKDROP_RGB = (0, 0, 0)
CLOCK_BACKDROP_PAD = 2
# Always-on clock: upper-right corner (panel coords)
CLOCK_MARGIN_H = 1
CLOCK_MARGIN_V = 1
CLOCK_ALWAYS_ON = True
def _draw_dark_backdrop(canvas, x, y, w, h, alpha=0.6, color=(0, 0, 0), pad=2):
"""Blend a darker rectangle over current ScreenArray/canvas (fake transparency)."""
x0 = max(0, int(x) - pad)
y0 = max(0, int(y) - pad)
x1 = min(VIEW_W, int(x) + int(w) + pad)
y1 = min(VIEW_H, int(y) + int(h) + pad)
cr, cg, cb = color
a = max(0.0, min(1.0, float(alpha)))
inv = 1.0 - a
set_px = canvas.SetPixel
for py in range(y0, y1):
for px in range(x0, x1):
try:
br, bg, bb = LED.ScreenArray[py][px]
except Exception:
br = bg = bb = 0
r = int(br * inv + cr * a)
g = int(bg * inv + cg * a)
b = int(bb * inv + cb * a)
set_px(px, py, r, g, b)
try:
LED.ScreenArray[py][px] = (r, g, b)
except Exception:
pass
def _draw_circle(canvas, cx, cy, radius, rgb, panel_w=None, panel_h=None):
"""Midpoint circle (outline) in rgb."""
panel_w = panel_w if panel_w is not None else VIEW_W
panel_h = panel_h if panel_h is not None else VIEW_H
r, g, b = rgb
x = int(radius)
y = 0
err = 0
set_px = canvas.SetPixel
def plot(px, py):
if 0 <= px < panel_w and 0 <= py < panel_h:
set_px(px, py, r, g, b)
while x >= y:
plot(cx + x, cy + y)
plot(cx + y, cy + x)
plot(cx - y, cy + x)
plot(cx - x, cy + y)
plot(cx - x, cy - y)
plot(cx - y, cy - x)
plot(cx + y, cy - x)
plot(cx + x, cy - y)
y += 1
if err <= 0:
err += 2 * y + 1
if err > 0:
x -= 1
err -= 2 * x + 1
def _draw_text_line(canvas, text, x, y, rgb, panel_w=None, panel_h=None, gap=1):
"""Draw uppercase banner text via AlphaSpriteList (1×)."""
panel_w = panel_w if panel_w is not None else VIEW_W
panel_h = panel_h if panel_h is not None else VIEW_H
r, g, b = rgb
cursor = int(x)
set_px = canvas.SetPixel
for ch in text.upper():
if ch == " ":
cursor += 3
continue
if not ("A" <= ch <= "Z"):
cursor += 3
continue
try:
spr = LED.TrimSprite(copy.deepcopy(LED.AlphaSpriteList[ord(ch) - ord("A")]))
except Exception:
cursor += 4
continue
for count in range(spr.width * spr.height):
if spr.grid[count] == 0:
continue
ly, lx = divmod(count, spr.width)
px, py = cursor + lx, int(y) + ly
if 0 <= px < panel_w and 0 <= py < panel_h:
set_px(px, py, r, g, b)
cursor += spr.width + gap
return cursor
def _draw_out_of_fuel_overlay(canvas, screen_x, screen_y):
"""Red circle around the car + OUT OF FUEL label."""
cx = int(round(screen_x))
cy = int(round(screen_y))
# pulsing ring
radius = 5 + int((time.time() * 4) % 2)
_draw_circle(canvas, cx, cy, radius, (255, 20, 20))
_draw_circle(canvas, cx, cy, radius + 1, (180, 0, 0))
# Label above car (dark bar + text)
label = "OUT OF FUEL"
# rough width: ~4px/letter * 10 + gaps
text_w = 10 * 4 + 9
tx = max(0, min(VIEW_W - text_w, cx - text_w // 2))
ty = max(0, cy - 12)
_draw_dark_backdrop(canvas, tx, ty, text_w, 7, alpha=0.7, color=(0, 0, 0), pad=1)
_draw_text_line(canvas, label, tx, ty, (255, 40, 40))
class EmptyObject(object):
def __init__(self, name="EmptyObject"):
self.name = name
self.alive = 0
self.lives = 0
self.r = 0
self.g = 0
self.b = 0
self.exploding = 0
self.h = 0
self.v = 0
self.direction = 1
self.scandirection = 1
self.speed = 1
self.destination = ""
self.radarrange = 0
def camera_for_car(car, world):
"""Center viewport on car, clamped to map bounds."""
refresh_view_size()
cam_h = int(car.h) - VIEW_W // 2
cam_v = int(car.v) - VIEW_H // 2
max_h = max(0, world.width - VIEW_W)
max_v = max(0, world.height - VIEW_H)
if cam_h < 0:
cam_h = 0
elif cam_h > max_h:
cam_h = max_h
if cam_v < 0:
cam_v = 0
elif cam_v > max_v:
cam_v = max_v
return cam_h, cam_v
def IncreaseColor(Car):
#Make player car more blue
if (Car.name == "Player"):
Car.b = Car.b + 20
if (Car.b >= 255):
Car.b = 255
#Make enemy more red
else:
Car.r = Car.r + 50
if (Car.r >= 255):
Car.r = 255
#print ("Carname rgb",Car.name,Car.r,Car.g,Car.b)
def DecreaseColor(Car):
#Make player car less blue
if (Car.name == "Player"):
Car.b = Car.b - 1
if (Car.b <= 60):
Car.b = 60
#Make player car less blue
else:
Car.r = Car.r - 1
if (Car.r <= 60):
Car.r = 60
class GameWorld(object):
def __init__(self,name,width,height,Map,Playfield,CurrentRoomH,CurrentRoomV,DisplayH, DisplayV):
self.name = name
self.width = width
self.height = height
self.Map = ([[]])
self.Playfield = ([[]])
self.CurrentRoomH = 0
self.CurrentRoomV = 0
self.DisplayH = 0
self.DisplayV = 0
#print ("RD - Initialize map and playfield width height: ",self.width, self.height)
self.Map = [[0 for i in range(self.width)] for i in range(self.height)]
self.Playfield = [[EmptyObject('EmptyObject') for i in range(self.width)] for i in range(self.height)]
#print ("--Initializing map--")
#print (*self.Map[0])
#print (*self.Map[2])
#print ("Map Length: ",len(self.Map[0]))
#print ("Playfield Length",len(self.Playfield[0]))
#print ("-------------------")
def DisplayExplodingObjects(self,h,v):
#This function accepts h,v coordinates for the entire map (e.gv. 1,8 20,20, 64,64)
#Displays what is on the playfield currently, including walls, cars, etc.
r = 0
g = 0
b = 0
count = 0
for V in range(0,VIEW_H):
for H in range (0,VIEW_W):
if (v+V < self.height and h+H < self.width):
name = self.Playfield[v+V][h+H].name
if (name in ("Enemy") and self.Playfield[v+V][h+H].exploding == 1):
#print("Exploding Object - h,v,name ",h,v,name)
r = 0
g = 0
b = 0
#EXPLODE ENEMY CAR BOMBS
#Source Car blows up
self.Playfield[v+V][h+H].exploding = 0
self.Playfield[v+V][h+H].lives = 0
self.Playfield[v+V][h+H].alive = 0
setpixel(H,V,255,255,255)
#remove dead object from playfield
self.Playfield[v+V][h+H] = EmptyObject('EmptyObject')
_show()
#SendBufferPacket(RemoteDisplay,VIEW_H,VIEW_W)
return;
def DisplayWindow(self, h, v, do_swap=True):
"""
Camera window (h,v) = upper-left on virtual map → full panel.
Always paints every viewport pixel (no leftovers / ghost maps).
Single canvas path + optional one VSync swap.
"""
refresh_view_size()
try:
LED.Canvas.Clear()
except Exception:
pass
# Clamp camera so we never sample with negative indices
if h < 0:
h = 0
if v < 0:
v = 0
pf = self.Playfield
ph, pw = self.height, self.width
canvas = LED.Canvas
set_px = canvas.SetPixel
for V in range(0, VIEW_H):
mv = v + V
for H in range(0, VIEW_W):
mh = h + H
if 0 <= mv < ph and 0 <= mh < pw:
cell = pf[mv][mh]
if cell.name == "EmptyObject":
r = g = b = 0
else:
r, g, b = _gamma_rgb(cell.r, cell.g, cell.b)
else:
r = g = b = 0
set_px(H, V, r, g, b)
try:
LED.ScreenArray[V][H] = (r, g, b)
except Exception:
pass
if do_swap:
_show()
return
def DisolveWindow(self, h, v, sleep=0):
# Instant clear (no per-pixel sleep)
try:
LED.Canvas.Clear()
_show()
except Exception:
pass
try:
LED.ClearBigLED()
LED.ClearBuffers()
except Exception:
pass
return
def DisplayWindowWithSprite(self, h, v, TheSprite):
# One frame: map + dark translucent bar + sprite, then one swap
self.DisplayWindow(h, v, do_swap=False)
try:
sw = int(getattr(TheSprite, "width", 12) or 12)
sh_h = int(getattr(TheSprite, "height", 7) or 7)
sh = max(0, min(VIEW_W - sw, VIEW_W // 2 - sw // 2))
sv = max(0, int(getattr(TheSprite, "v", 1) or 1))
if sv + sh_h > VIEW_H:
sv = max(0, VIEW_H - sh_h)
TheSprite.h = sh
TheSprite.v = sv
# Dark translucent rectangle behind the clock so time stays readable
_draw_dark_backdrop(
LED.Canvas, sh, sv, sw, sh_h,
alpha=CLOCK_BACKDROP_ALPHA,
color=CLOCK_BACKDROP_RGB,
pad=CLOCK_BACKDROP_PAD,
)
if hasattr(TheSprite, "Display"):
try:
TheSprite.Display(sh, sv)
except TypeError:
try:
TheSprite.Display()
except Exception:
pass
except Exception:
pass
_show()
return
def UpdateObjectDisplayCoordinates(self,h,v):
#This function looks at a window (an 8x8 display grid for the unicorn hat)
#and updates the dh,dv location information for objects in that grid
#This is useful if we want to blow something up on screen
#scroll off
for V in range(0,VIEW_H):
for H in range (0,VIEW_W):
name = self.Playfield[v+V][h+H].name
if (name == "Player" or name == "Enemy" or name == "Fuel"):
self.Playfield[v+V][h+H].dh = H
self.Playfield[v+V][h+H].dv = V
def CopyMapToPlayfield(self):
#This function is run once to populate the playfield with wall objects, based on the map drawing
#XY is actually implemented as YX. Counter intuitive, but it works.
width = self.width
height = self.height
#print ("RD - CopyMapToPlayfield - Width Height: ", width,height)
x = 0
y = 0
#print ("width height: ",width,height)
for y in range (0,height):
#print ("-------------------")
#print (*self.Map[y])
for x in range(0,width):
#print ("RD xy color: ",x,y, self.Map[y][x])
SDColor = self.Map[y][x]
if (SDColor != 0):
try:
r,g,b = LED.ColorList[SDColor]
except (IndexError, TypeError):
r,g,b = (40, 40, 80)
self.Playfield[y][x] = LED.Wall(x,y,r,g,b,1,1,'Wall')
else:
self.Playfield[y][x] = EmptyObject('EmptyObject')
def ScrollMapDots(self,direction,dots,speed):
#we only want to scroll the number of dots, not the whole room
#DisplayWindow has HV starting in upper left hand corner
x = 0
ScrollH = self.DisplayH
ScrollV = self.DisplayV
#print("ScrollMapDots - ScrollH ScrollV direction width",ScrollH,ScrollV, direction, self.width)
#Scroll Up
if (direction == 1):
if (ScrollV - dots >= 0):
for x in range (ScrollV-1,ScrollV-dots-1,-1):
#print ("ScrollMapDots up: ScrollH x",ScrollH,x)
self.DisplayWindow(ScrollH,x)
ScrollV = x
#Scroll Down
if (direction == 3):
if (ScrollV + VIEW_W + dots <= self.height):
for x in range (ScrollV+1,ScrollV+dots+1):
#print ("ScrollMapDots down: ScrollH x",ScrollH,x)
self.DisplayWindow(ScrollH,x)
ScrollV = x
#Scroll right
if (direction == 2):
if (ScrollH + VIEW_W + dots <= self.width):
for x in range (ScrollH+1,ScrollH+dots+1):
#print ("ScrollMapDots right: x ScrollV",x,ScrollV)
self.DisplayWindow(x,ScrollV)
ScrollH = x
#Scroll left
elif (direction == 4):
if (ScrollH - dots >= 0):
for x in range (ScrollH-1,ScrollH-dots-1,-1):
#print ("ScrollMapDots left: x ScrollV",x,ScrollV)
self.DisplayWindow(x,ScrollV)
ScrollH = x
#Set current room number
self.CurrentRoomH,r = divmod(ScrollH,8)
self.CurrentRoomV,r = divmod(ScrollV,8)
self.DisplayH = ScrollH
self.DisplayV = ScrollV
#time.sleep(speed)
def ScrollMapDots8Way(self,direction,dots,speed):
#we only want to scroll the number of dots, not the whole room
#DisplayWindow has HV starting in upper left hand corner
x = 0
ScrollH = self.DisplayH
ScrollV = self.DisplayV
#print("ScrollMapDots8Way - ScrollH ScrollV direction width",ScrollH,ScrollV, direction, self.width)
#Scroll N
if (direction == 1):
if (ScrollV - dots >= 0):
for x in range (ScrollV-1,ScrollV-dots-1,-1):
self.DisplayWindow(ScrollH,x)
ScrollV = x
#Scroll NE
if (direction == 2):
#Scroll up and right
if (ScrollV - dots >= 0):
for x in range (ScrollV-1,ScrollV-dots-1,-1):
self.DisplayWindow(ScrollH,x)
ScrollV = x
if (ScrollH + 8 + dots <= self.width):
for x in range (ScrollH+1,ScrollH+dots+1):
self.DisplayWindow(x,ScrollV)
ScrollH = x
#Scroll E
if (direction == 3):
if (ScrollH + 8 + dots <= self.width):
for x in range (ScrollH+1,ScrollH+dots+1):
self.DisplayWindow(x,ScrollV)
ScrollH = x
#Scroll SE
#Scroll right then down
if (direction == 4):
if (ScrollH + 8 + dots <= self.width):
for x in range (ScrollH+1,ScrollH+dots+1):
self.DisplayWindow(x,ScrollV)
ScrollH = x
if (ScrollV + 8 + dots <= self.height):
for x in range (ScrollV+1,ScrollV+dots+1):
self.DisplayWindow(ScrollH,x)
ScrollV = x
#Scroll S
if (direction == 5):
if (ScrollV + 8 + dots <= self.height):
for x in range (ScrollV+1,ScrollV+dots+1):
self.DisplayWindow(ScrollH,x)
ScrollV = x
#Scroll SW
#Scroll down then left
elif (direction == 6):
if (ScrollH - dots >= 0):
for x in range (ScrollH-1,ScrollH-dots-1,-1):
self.DisplayWindow(x,ScrollV)
ScrollH = x
if (ScrollV + 8 + dots <= self.height):
for x in range (ScrollV+1,ScrollV+dots+1):
self.DisplayWindow(ScrollH,x)
ScrollV = x
#Scroll W
elif (direction == 7):
if (ScrollH - dots >= 0):
for x in range (ScrollH-1,ScrollH-dots-1,-1):
self.DisplayWindow(x,ScrollV)
ScrollH = x
#Scroll NW
#Scroll upd then left
elif (direction == 8):
if (ScrollV - dots >= 0):
for x in range (ScrollV-1,ScrollV-dots-1,-1):
self.DisplayWindow(ScrollH,x)
ScrollV = x
if (ScrollH - dots >= 0):
for x in range (ScrollH-1,ScrollH-dots-1,-1):
self.DisplayWindow(x,ScrollV)
ScrollH = x
#time.sleep(0.5)
#Set current room number
self.CurrentRoomH,r = divmod(ScrollH,8)
self.CurrentRoomV,r = divmod(ScrollV,8)
self.DisplayH = ScrollH
self.DisplayV = ScrollV
# -------------------------
# -- Cars --
# -------------------------
class CarDot(object):
def __init__(self,h,v,dh,dv,r,g,b,direction,scandirection,gear,currentgear,speed,alive,lives,name,score,exploding,radarrange,destination):
self.h = h # location on playfield (e.gv. 10,35)
self.v = v # location on playfield (e.gv. 10,35)
self.dh = dh # location on display (e.gv. 3,4)
self.dv = dv # location on display (e.gv. 3,4)
self.r = r
self.g = g
self.b = b
self.direction = direction #direction of travel
self.scandirection = scandirection #direction of scanners, if equipped
self.currentgear = currentgear
self.speed = speed
self.alive = 1
self.lives = 3
self.name = name
self.score = 0
self.exploding = 0
self.radarrange = 20
self.destination = ""
self.gas = PLAYER_GAS_MAX # player tank; enemies ignore
#Hold speeds in a list, acting like gears
self.gear = []
self.gear.append(5)
self.gear.append(4)
self.gear.append(3)
self.gear.append(2)
self.gear.append(1)
def Display(self):
if (self.alive == 1):
setpixel(self.h,self.v,self.r,self.g,self.b)
# print("display HV:", self.h,self.v)
_show()
#SendBufferPacket(RemoteDisplay,VIEW_H,VIEW_W)
def ShiftGear(self,direction):
#Gears is a list with X gears
#lists start counting at 0
#Min gear = 0
#Max gear = x-1
NumGears = len(self.gear)
if (direction == 'down'):
self.currentgear = self.currentgear -1
else:
self.currentgear = self.currentgear +1
#need to put in the CPUModifier here
#don't let player go too fast or too slow
if (self.name == "Player"):
if self.currentgear > NumGears -2:
self.currentgear = NumGears -2
if self.currentgear <= 3:
self.currentgear = 3
if (self.currentgear > NumGears -1):
self.currentgear = NumGears -1
elif (self.currentgear < 0):
self.currentgear = 0
#adust speed based on current gear
self.speed = self.gear[self.currentgear]
#print ("Name: ", self.name, " Current Gear:",self.currentgear, " Speed: ",self.speed)
return;
def Erase(self):
setpixel(self.h,self.v,0,0,0)
_show()
#SendBufferPacket(RemoteDisplay,VIEW_H,VIEW_W)
def AdjustSpeed(self, increment):
speed = self.speed
speed = self.speed + increment
if (speed > 1000):
speed = 1000
elif (speed <= 1):
speed = 1
self.speed = speed
return;
#------------------------------------------------------------------------------
def CheckElapsedTime(seconds):
"""Return 1 once per `seconds` wall-clock second (mod)."""
try:
return 1 if (int(time.time()) % max(1, int(seconds))) == 0 else 0
except Exception:
return 0
def _clock_upper_right_pos(ClockSprite):
"""Screen (h,v) for clock in the upper-right corner."""
refresh_view_size()
sw = int(getattr(ClockSprite, "width", 12) or 12)
sh = int(getattr(ClockSprite, "height", 7) or 7)
h = max(0, VIEW_W - sw - CLOCK_MARGIN_H)
v = max(0, min(CLOCK_MARGIN_V, max(0, VIEW_H - sh)))
return h, v
def position_clock_upper_right(ClockSprite):
"""Pin clock sprite to upper-right; keep always on."""
try:
h, v = _clock_upper_right_pos(ClockSprite)
ClockSprite.h = h
ClockSprite.v = v
ClockSprite.on = 1
except Exception:
pass
def draw_clock_overlay(ClockSprite):
"""Draw dark backdrop + clock digits at upper-right (canvas already has map)."""
try:
sw = int(getattr(ClockSprite, "width", 12) or 12)
sh_h = int(getattr(ClockSprite, "height", 7) or 7)
sh, sv = _clock_upper_right_pos(ClockSprite)
ClockSprite.h, ClockSprite.v = sh, sv
_draw_dark_backdrop(
LED.Canvas, sh, sv, sw, sh_h,
alpha=CLOCK_BACKDROP_ALPHA,
color=CLOCK_BACKDROP_RGB,
pad=CLOCK_BACKDROP_PAD,
)
ClockSprite.Display(sh, sv)
except Exception:
pass
def CheckClockTimer(ClockSprite):
"""Keep clock always on in the upper-right (no slide / on-off cycle)."""
try:
if CLOCK_ALWAYS_ON:
ClockSprite.on = 1
position_clock_upper_right(ClockSprite)
return 0
# Legacy toggle path (unused when CLOCK_ALWAYS_ON)
if not hasattr(ClockSprite, "StartTime") or ClockSprite.StartTime is None:
ClockSprite.StartTime = time.time()
elapsed_seconds = time.time() - ClockSprite.StartTime
if getattr(ClockSprite, "on", 0) == 1:
if elapsed_seconds >= ClockOnDuration and getattr(ClockSprite, "v", 0) <= -5:
ClockSprite.on = 0
ClockSprite.StartTime = time.time()
else:
if elapsed_seconds >= ClockOffDuration:
ClockSprite.on = 1
ClockSprite.StartTime = time.time()
position_clock_upper_right(ClockSprite)
except Exception:
pass
return 0
def MoveMessageSprite(moves_count, MessageSprite):
"""Slide message/clock sprite vertically (HD). Safe no-op if attrs missing."""
try:
m, r = divmod(moves_count, max(1, ClockSlideSpeed))
if r != 0:
return
if not hasattr(MessageSprite, "v"):
return
# Ensure required attributes exist
if not hasattr(MessageSprite, "DirectionIncrement"):
MessageSprite.DirectionIncrement = 1
if not hasattr(MessageSprite, "PausePositionV"):
MessageSprite.PausePositionV = VIEW_H // 2
if not hasattr(MessageSprite, "PauseTimerOn"):
MessageSprite.PauseTimerOn = 0
if not hasattr(MessageSprite, "Delay"):
MessageSprite.Delay = 2
if not hasattr(MessageSprite, "PauseStartTime"):
MessageSprite.PauseStartTime = time.time()
if MessageSprite.v == MessageSprite.PausePositionV:
if MessageSprite.PauseTimerOn == 0:
MessageSprite.PauseTimerOn = 1
MessageSprite.PauseStartTime = time.time()
elapsed_seconds = time.time() - MessageSprite.PauseStartTime
if elapsed_seconds >= MessageSprite.Delay:
if MessageSprite.DirectionIncrement >= 0:
MessageSprite.DirectionIncrement = MessageSprite.DirectionIncrement * -1
MessageSprite.v = MessageSprite.v + MessageSprite.DirectionIncrement
else:
MessageSprite.PauseTimerOn = 0
MessageSprite.v = MessageSprite.v + MessageSprite.DirectionIncrement
if MessageSprite.v >= MessageSprite.PausePositionV:
MessageSprite.DirectionIncrement = MessageSprite.DirectionIncrement * -1
height = getattr(MessageSprite, "height", 5)
width = getattr(MessageSprite, "width", 10)
if MessageSprite.v >= VIEW_H + 2 or (
MessageSprite.v < (0 - height) and MessageSprite.DirectionIncrement < 0
):
MessageSprite.h = (VIEW_W - width) // 2
MessageSprite.v = 0 - height
MessageSprite.on = 0
except Exception:
pass
#-- RallyDot --
#-- --
#-- --
#----------------------------------------------------------------------------
# - the player car will not move, but the maze around him will
# - the playfield contains all objects, including cars walls enemies and bullets
# - we loop through the playfield, examining each object
# - ignore empty
# - ignore walls
# - if player/enemy then give it a turn to use radar to find nearby items
# - make a decision on what to to
# - decisions are priority based
# - shoot opponent
# - run
# - hide
# - we still use a clock/speed value to see if a player/enemy object is going to make a decision this turn
# - objects off screen will still move, but will not be visible
# - draw window function will be used to display the current visible sqare in the map (8x8)
# Active race map: 6 = original (80x144); 7 = map6 flipped 2x2 (160x288)
ACTIVE_MAP_LEVEL = 7
# Player gas tank (Rally-X style) — drains while driving, refilled by fuel dots
PLAYER_GAS_MAX = 450
PLAYER_GAS_DRAIN = 1 # per player move tick
PLAYER_GAS_REFILL = 70 # when eating a fuel pickup
OUT_OF_FUEL_HOLD_SEC = 2.0 # show red ring + label, then lose a life
# Stock lives (credits). PlayerCar.lives is hit-points within the current life.
PLAYER_STOCK_LIVES = 3
PLAYER_HEALTH_MAX = 100