-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSkyfall.py
More file actions
2873 lines (2409 loc) · 97.4 KB
/
Copy pathSkyfall.py
File metadata and controls
2873 lines (2409 loc) · 97.4 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
#------------------------------------------------------------------------------
# SKYFALL — Falling asteroids and a bottom-patrol shooter
#
# Based on SpaceExplorer asteroid lump sprites. A 3-pixel-tall ship patrols the
# bottom of the screen, auto-fires upward, and splits rocks on impact.
#------------------------------------------------------------------------------
import copy
import math
import os
import random
import time
import LEDarcade as LED
# Panel size is resolved after LED.Initialize() in LaunchSkyfall.
WIDTH = LED.HatWidth
HEIGHT = LED.HatHeight
TARGET_FPS = 30.0
FRAME_DT = 1.0 / TARGET_FPS
SIM_REFERENCE_FPS = 30.0
SIM_REFERENCE_DT = 1.0 / SIM_REFERENCE_FPS
MAX_SIM_DT = 1.0 / 12.0
SHIP_HEIGHT = 2
SHIP_WIDTH = 3
SHIP_SPEED = 1.27
SHIP_RGB = (90, 180, 255)
SHIP_NOSE_RGB = (220, 240, 255)
# Upward triangle: 1 pixel nose, 3 pixels on the bottom row.
SHIP_SHAPE = (
(0, 1, 0),
(1, 1, 1),
)
ASTEROID_COLLIDE_SCALE = 0.95
ASTEROID_BOUNCE_COOLDOWN = 3
BULLET_SPEED = 2.99
BULLET_RGB = (255, 255, 255)
BULLET_STREAK_LEN = 4
MAX_ACTIVE_SHOTS = 2
FIRE_INTERVAL = 0.14
HUNT_SPEED = 0.71
LOOT_INTERCEPT_MAX_FRAMES = 40
SHIP_LOOKAHEAD_FRAMES = 24
SHIP_DANGER_DISTANCE = 8.0
SHIP_URGENT_DANGER = 4.5
SHIP_DODGE_SPEED = 1.01
SHIP_STEER_DEADBAND = 1.25
SHIP_HUNT_SMOOTH = 0.18
SHIP_MOVE_HOLD_BIAS = 2.8
SHIP_REVERSE_PENALTY = 4.0
SHIP_LANE_CLEARANCE = 3.0
SHIP_SURVIVAL_DANGER = 10.0
SHIP_LOOT_DANGER_LIMIT = 18.0
SHIP_EDGE_TRAP_DISTANCE = 4.0
SHIP_LANE_SAMPLE_STEP = 1.5
SHIP_RESPAWN_DURATION = 1.4
SHIP_EXPLOSION_SPARK_COUNT = 28
SHIP_EXPLOSION_DEBRIS_COUNT = 16
ASTEROID_SPAWN_INTERVAL = 1.1
ASTEROID_SPAWN_ABOVE = 10
ASTEROID_SPEED_MIN = 0.52
ASTEROID_SPEED_MAX = 1.21
ASTEROID_ANGLE_SPREAD = 0.85
ASTEROID_SIZE_TIER_MIN = 2
ASTEROID_SIZE_TIER_MAX = 7
ASTEROID_SIZE_TIER_SMALL = (3, 5) # was size 2
ASTEROID_SIZE_TIER_MEDIUM = (6, 13) # was size 3
MIN_ASTEROID_SPLIT_SIZE = 3
MAX_ASTEROIDS = 18
ROCK_SPLIT_COUNT = 2
SPLIT_FLY_APART = (0.25, 0.46)
SPLIT_PARENT_MOMENTUM = 0.40
SPLIT_PERP_SPREAD_RAD = 0.85
SPLIT_SPAWN_OFFSET = 1.6
# Small (0–7), medium (15–18), large (19–24), wide (25) — skip satellite slots 8–14.
ENEMY_SHIP_TYPES = tuple(range(8)) + tuple(range(15, 26))
ENEMY_SPAWN_INTERVAL = 3.8
MAX_ENEMIES = 4
ENEMY_SPEED_MIN = 0.40
ENEMY_SPEED_MAX = 0.83
ENEMY_BRIGHTNESS = 1.85
ENEMY_RGB_FLOOR = 52
ENEMY_ANIMATION_SLOWDOWN = 5
ASTEROID_LIGHTING_CONTRAST = 1.0
ASTEROID_COLORS = (
(210, 195, 175),
(200, 210, 255),
(138, 138, 145),
)
RED_ROCK_COLOR = (230, 50, 40)
RED_ROCK_CHANCE = 0.24
RED_ROCK_CRYSTAL_CHANCE = 0.55
RED_ROCK_SHOOT_BONUS = 18.0
BLUE_ROCK_SHOOT_BONUS = 17.0
BLUE_ROCK_COLOR = (55, 120, 230)
BLUE_ROCK_CHANCE = 0.22
BLUE_ROCK_GEM_CHANCE = 0.55
LOOT_MAX_PER_BREAK = 2
CRYSTAL_RGB = (255, 255, 0)
GEM_RGB = (30, 255, 90)
LOOT_PARENT_MOMENTUM = 0.55
LOOT_BURST_MIN = 0.32
LOOT_BURST_MAX = 0.75
LOOT_BURST_SPREAD = 0.75
LOOT_MAX_SPEED = 1.51
LOOT_GRAVITY = 0.040
LOOT_FALL_MIN = 0.51
LOOT_BOUNCE_DAMPING = 0.78
LOOT_BOUNCE_COOLDOWN = 2
LOOT_HUNT_SPEED = 0.83
CRYSTAL_POWER_COST = 5
GEM_POWER_COST = 5
SHOTGUN_BULLET_COUNT = 5
SHOTGUN_SPREAD = 0.32
SHOTGUN_DURATION = 5.0
SHOTGUN_FIRE_INTERVAL = 0.18
LIGHTNING_MIN_TARGETS = 6
LIGHTNING_DANGER_DISTANCE = 5.0
SPARK_STREAM_DURATION = 5.0
SPARK_STREAM_EMIT_FRAMES = 150
SPARK_STREAM_SPARKS_PER_BURST = 3
SPARK_STREAM_SPREAD = 1.35
SPARK_STREAM_SPEED_MIN = 3.22
SPARK_STREAM_SPEED_MAX = 5.98
HOT_SPARK_TRAIL_LEN = 5
HOT_SPARK_MAX_AGE = 90
SPARK_COUNT = 8
TINY_ROCK_SPARK_COUNT = 12
SPARK_TRAIL_LENGTH = 5
SPARK_COLOR = (255, 200, 100)
ENEMY_PARTICLE_GRAVITY = 0.021
DEBRIS_SPEED_MIN = 0.23
DEBRIS_SPEED_MAX = 1.38
EXPLOSION_SPARK_SPEED_MIN = 0.40
EXPLOSION_SPARK_SPEED_MAX = 1.27
SHIP_EXPLOSION_SPARK_SPEED_MIN = 1.03
SHIP_EXPLOSION_SPARK_SPEED_MAX = 3.22
SHIP_EXPLOSION_SPARK_BURST_MIN = 1.61
SHIP_EXPLOSION_SPARK_BURST_MAX = 4.60
SHIP_DEBRIS_SPEED_XY = 2.07
SHIP_DEBRIS_SPEED_Y = 2.88
ENEMY_PARTICLE_LIFESPAN = 76
PARALLAX_LAYER_HEIGHT_MULT = 12
FAR_STAR_STARCHANCE = 170
NEAR_STAR_STARCHANCE = 72
FAR_SCROLL_SPEED = 0.18
NEAR_SCROLL_SPEED = 0.55
GAS_GIANT_SCROLL_SPEED = 0.90
GAS_GIANT_MIN_RADIUS = 20
GAS_GIANT_MAX_RADIUS = 36
GAS_GIANT_COUNT = 3
GAS_GIANT_APPEAR_INTERVAL = 20.0
PLANET_SCROLL_SPEED = 0.72
PLANET_COUNT = 5
PLANET_MIN_RADIUS = 5
PLANET_MAX_RADIUS = 15
PLANET_APPEAR_INTERVAL = 20.0
STAR_DIM_FACTOR = 0.7
# Resolved via LEDarcade.ResolveFontPath when used; keep a portable default name
CLOCK_FONT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fonts", "CHECKBK0.TTF")
CLOCK_DIGIT_RGB = (48, 200, 140)
CLOCK_SIZE_FACTOR = 0.7425
CLOCK_RESPAWN_DELAY = 10.0
CLOCK_SLIDE_DURATION = 0.52
TITLE_WORD = "SKYFALL"
TITLE_LETTER_ZOOM = 2
TITLE_LETTER_GAP = 1
TITLE_LETTER_RGB = (90, 180, 255)
TITLE_LETTER_SHADOW_RGB = (15, 35, 70)
TITLE_LETTER_STAGGER = 0.25
TITLE_LETTER_GRAVITY = 0.62
TITLE_LETTER_BOUNCE_DAMP = 0.44
TITLE_LETTER_SETTLE_V = 0.38
TITLE_LETTER_MAX_BOUNCES = 3
TITLE_HOLD_SECONDS = 2.0
TITLE_INTRO_MAX_SECONDS = 18.0
def _panel_size():
return LED.HatWidth, LED.HatHeight
def _motion_step(frame_dt):
"""Scale per-frame motion so speeds stay consistent without a frame sleep."""
if frame_dt <= 0.0:
return 1.0
return min(frame_dt, MAX_SIM_DT) / SIM_REFERENCE_DT
def _generate_asteroid_lumps():
lumps = []
for _ in range(random.randint(3, 6)):
angle = random.uniform(0, 2 * math.pi)
distance_frac = random.uniform(0, 0.5)
lump_radius_frac = random.uniform(0.2, 0.5)
lumps.append((
math.cos(angle) * distance_frac,
math.sin(angle) * distance_frac,
lump_radius_frac,
))
return lumps
def _shade_asteroid_color(color, brightness_factor):
r, g, b = color
return (
min(255, int(r * brightness_factor)),
min(255, int(g * brightness_factor)),
min(255, int(b * brightness_factor)),
)
def _build_asteroid_sprite_pixels(size, color, lumps, dim_factor=1.0):
pixels = []
bounding_size = int(size * 1.2)
for j in range(-bounding_size, bounding_size + 1):
for i in range(-bounding_size, bounding_size + 1):
max_depth = -1.0
selected_lump = None
for frac_dx, frac_dy, frac_r in lumps:
effective_dx = frac_dx * size
effective_dy = frac_dy * size
effective_radius = frac_r * size
distance = math.sqrt((i - effective_dx) ** 2 + (j - effective_dy) ** 2)
if distance < effective_radius:
depth = effective_radius - distance
if depth > max_depth:
max_depth = depth
selected_lump = (frac_dx, frac_dy, frac_r)
if not selected_lump:
continue
frac_dx, frac_dy, frac_r = selected_lump
effective_dx = frac_dx * size
effective_dy = frac_dy * size
effective_radius = frac_r * size
rel_i = i - effective_dx
rel_j = j - effective_dy
brightness = 1.0 - ASTEROID_LIGHTING_CONTRAST * (rel_i + rel_j) / (2 * max(effective_radius, 0.5))
brightness = max(0.64, min(1.35, brightness)) * dim_factor
pixels.append((i, j, _shade_asteroid_color(color, brightness)))
return pixels
def _roll_asteroid_spawn_size():
"""Only the two smallest tiers are enlarged; sizes 4–7 stay original."""
tier = random.randint(ASTEROID_SIZE_TIER_MIN, ASTEROID_SIZE_TIER_MAX)
if tier == 2:
return random.randint(*ASTEROID_SIZE_TIER_SMALL)
if tier == 3:
return random.randint(*ASTEROID_SIZE_TIER_MEDIUM)
return tier
def _split_child_pair(parent_size):
"""Always two children, each smaller than the parent."""
first = max(2, parent_size // 2)
second = max(2, parent_size - first)
if first >= parent_size or second >= parent_size:
return None
return first, second
def _split_fragment_angles(impact_angle, count):
angles = []
for _ in range(count):
side = random.choice((-1, 1))
angles.append(
impact_angle
+ side * (
math.pi / 2
+ random.uniform(-SPLIT_PERP_SPREAD_RAD, SPLIT_PERP_SPREAD_RAD)
)
)
return angles
def _random_fall_velocity():
angle = (math.pi / 2) + random.uniform(-ASTEROID_ANGLE_SPREAD, ASTEROID_ANGLE_SPREAD)
speed = random.uniform(ASTEROID_SPEED_MIN, ASTEROID_SPEED_MAX)
return math.cos(angle) * speed, math.sin(angle) * speed
def _velocity_toward(x, y, target_x, target_y, speed):
angle = math.atan2(target_y - y, target_x - x)
return math.cos(angle) * speed, math.sin(angle) * speed
class Spark:
"""Short-lived explosion streak in screen coordinates."""
def __init__(self, x, y, angle, speed, length):
self.x = float(x)
self.y = float(y)
self.angle = angle
self.speed = speed
self.length = max(1, min(length, 8))
self.lifespan = SPARK_TRAIL_LENGTH
def move(self, step=1.0):
self.x += math.cos(self.angle) * self.speed * step
self.y += math.sin(self.angle) * self.speed * step
self.lifespan -= 1
@property
def alive(self):
return self.lifespan > 0
def draw(self, canvas):
for i in range(self.length):
px = int(round(self.x - math.cos(self.angle) * i))
py = int(round(self.y - math.sin(self.angle) * i))
if not (0 <= px < WIDTH and 0 <= py < HEIGHT):
continue
fade = max(32, SPARK_COLOR[0] - i * (SPARK_COLOR[0] // max(1, SPARK_TRAIL_LENGTH * 2)))
canvas.SetPixel(px, py, fade, fade * 3 // 4, fade // 2)
class DebrisParticle:
"""Defender-style debris — one colored sprite pixel with drift and gravity."""
def __init__(self, x, y, r, g, b, vx, vy):
self.x = float(x)
self.y = float(y)
self.r, self.g, self.b = r, g, b
self.vx = vx
self.vy = vy
self.lifespan = ENEMY_PARTICLE_LIFESPAN
def move(self, step=1.0):
self.vy += ENEMY_PARTICLE_GRAVITY * step
self.x += self.vx * step
self.y += self.vy * step
self.lifespan -= 1
@property
def alive(self):
return self.lifespan > 0
def draw(self, canvas):
px = int(round(self.x))
py = int(round(self.y))
if 0 <= px < WIDTH and 0 <= py < HEIGHT:
fade = max(16, self.lifespan * 5)
canvas.SetPixel(
px, py,
min(255, self.r * fade // 255),
min(255, self.g * fade // 255),
min(255, self.b * fade // 255),
)
def _pick_asteroid_type(is_red=None, is_blue=None):
if is_red is True:
return RED_ROCK_COLOR, True, False
if is_blue is True:
return BLUE_ROCK_COLOR, False, True
if is_red is False and is_blue is False:
return random.choice(ASTEROID_COLORS), False, False
roll = random.random()
if roll < RED_ROCK_CHANCE:
return RED_ROCK_COLOR, True, False
if roll < RED_ROCK_CHANCE + BLUE_ROCK_CHANCE:
return BLUE_ROCK_COLOR, False, True
return random.choice(ASTEROID_COLORS), False, False
def _clamp_loot_speed(vx, vy):
speed = math.hypot(vx, vy)
if speed > LOOT_MAX_SPEED:
scale = LOOT_MAX_SPEED / speed
return vx * scale, vy * scale
return vx, vy
class LootPixel:
"""Single-pixel collectible — momentum, gravity fall, and bounces."""
__slots__ = ("x", "y", "vx", "vy", "alive", "bounce_cooldown", "rgb")
def __init__(self, x, y, vx, vy, rgb):
self.x = float(x)
self.y = float(y)
self.rgb = rgb
vy = max(float(vy), LOOT_FALL_MIN)
self.vx, self.vy = _clamp_loot_speed(float(vx), vy)
self.alive = True
self.bounce_cooldown = 0
def move(self, step=1.0):
self.vy += LOOT_GRAVITY * step
self.vx, self.vy = _clamp_loot_speed(self.vx, self.vy)
self.x += self.vx * step
self.y += self.vy * step
if self.bounce_cooldown > 0:
self.bounce_cooldown -= 1
def pixel(self):
return int(round(self.x)), int(round(self.y))
def collision_radius(self):
return 0.45
def off_screen(self, width, height):
margin = 2
return (
self.y > height + margin
or self.x < -margin
or self.x > width + margin
)
def draw(self, canvas, tick):
px, py = self.pixel()
if 0 <= px < WIDTH and 0 <= py < HEIGHT:
r, g, b = self.rgb
pulse = 20 if (tick + px + py) % 4 == 0 else 0
canvas.SetPixel(px, py, min(255, r + pulse), min(255, g + pulse), min(255, b + pulse))
class Crystal(LootPixel):
def __init__(self, x, y, vx, vy):
super().__init__(x, y, vx, vy, CRYSTAL_RGB)
class Gem(LootPixel):
def __init__(self, x, y, vx, vy):
super().__init__(x, y, vx, vy, GEM_RGB)
class FallingAsteroid:
"""Screen-space asteroid with its own drift trajectory."""
def __init__(self, x, y, size=None, color=None, vx=None, vy=None, is_red=None, is_blue=None):
self.x = float(x)
self.y = float(y)
self.size = size if size is not None else _roll_asteroid_spawn_size()
if color is None:
self.color, self.is_red, self.is_blue = _pick_asteroid_type(is_red, is_blue)
else:
self.color = color
self.is_red = is_red if is_red is not None else color == RED_ROCK_COLOR
self.is_blue = is_blue if is_blue is not None else color == BLUE_ROCK_COLOR
self.lumps = _generate_asteroid_lumps()
self.sprite_pixels = _build_asteroid_sprite_pixels(self.size, self.color, self.lumps, 1.0)
if vx is None or vy is None:
self.vx, self.vy = _random_fall_velocity()
else:
self.vx = vx
self.vy = vy
self.alive = True
self.bounce_cooldown = 0
def move(self, step=1.0):
self.x += self.vx * step
self.y += self.vy * step
if self.bounce_cooldown > 0:
self.bounce_cooldown -= 1
def collision_radius(self):
return self.size * 0.85
def off_screen(self, width, height):
margin = self.size + 3
if self.y - margin > height:
return True
if self.x < -margin or self.x > width + margin:
return True
return False
def hit_test(self, px, py):
cx = int(round(self.x))
cy = int(round(self.y))
dx = px - cx
dy = py - cy
reach = self.collision_radius() + 1
if dx * dx + dy * dy > reach * reach:
return False
for i, j, _ in self.sprite_pixels:
if cx + i == px and cy + j == py:
return True
return False
def draw(self, canvas):
cx = int(round(self.x))
cy = int(round(self.y))
for i, j, rgb in self.sprite_pixels:
px = cx + i
py = cy + j
if 0 <= px < WIDTH and 0 <= py < HEIGHT:
canvas.SetPixel(px, py, *rgb)
def _split_asteroid(asteroid, impact_angle):
"""Break a rock into exactly two smaller pieces, or sparks if too small."""
new_asteroids = []
sparks = []
child_sizes = None
if asteroid.size >= MIN_ASTEROID_SPLIT_SIZE:
child_sizes = _split_child_pair(asteroid.size)
if child_sizes:
for angle, child_size in zip(
_split_fragment_angles(impact_angle, ROCK_SPLIT_COUNT),
child_sizes,
):
burst = random.uniform(*SPLIT_FLY_APART)
offset = SPLIT_SPAWN_OFFSET + child_size * 0.2
cx = asteroid.x + math.cos(angle) * offset
cy = asteroid.y + math.sin(angle) * offset
vx = asteroid.vx * SPLIT_PARENT_MOMENTUM + math.cos(angle) * burst
vy = asteroid.vy * SPLIT_PARENT_MOMENTUM + math.sin(angle) * burst
speed = math.hypot(vx, vy)
max_speed = ASTEROID_SPEED_MAX * 1.4
if speed > max_speed:
scale = max_speed / speed
vx *= scale
vy *= scale
new_asteroids.append(FallingAsteroid(
cx, cy,
size=child_size,
color=asteroid.color,
vx=vx,
vy=vy,
is_red=asteroid.is_red,
is_blue=asteroid.is_blue,
))
spark_count = TINY_ROCK_SPARK_COUNT if not child_sizes else SPARK_COUNT
for _ in range(spark_count):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(DEBRIS_SPEED_MIN, DEBRIS_SPEED_MAX)
sparks.append(Spark(asteroid.x, asteroid.y, angle, speed, int(asteroid.size * 1.5)))
crystals = _spawn_loot_from_rock(asteroid, impact_angle, Crystal, RED_ROCK_CRYSTAL_CHANCE, asteroid.is_red)
gems = _spawn_loot_from_rock(asteroid, impact_angle, Gem, BLUE_ROCK_GEM_CHANCE, asteroid.is_blue)
return new_asteroids, sparks, crystals, gems
def _spawn_loot_from_rock(asteroid, impact_angle, loot_cls, drop_chance, should_drop):
loot = []
if not should_drop or random.random() > drop_chance:
return loot
for _ in range(random.randint(1, LOOT_MAX_PER_BREAK)):
side = random.choice((-1, 1))
angle = (
impact_angle
+ side * (math.pi / 2 + random.uniform(-LOOT_BURST_SPREAD, LOOT_BURST_SPREAD))
)
burst = random.uniform(LOOT_BURST_MIN, LOOT_BURST_MAX)
offset = random.uniform(0.8, 2.4)
vx = asteroid.vx * LOOT_PARENT_MOMENTUM + math.cos(angle) * burst
vy = asteroid.vy * LOOT_PARENT_MOMENTUM + math.sin(angle) * burst
loot.append(loot_cls(
asteroid.x + math.cos(angle) * offset,
asteroid.y + math.sin(angle) * offset,
vx,
vy,
))
return loot
def _brighten_enemy_rgb(r, g, b):
if r == 0 and g == 0 and b == 0:
return 0, 0, 0
return (
min(255, max(ENEMY_RGB_FLOOR, int(r * ENEMY_BRIGHTNESS))),
min(255, max(ENEMY_RGB_FLOOR, int(g * ENEMY_BRIGHTNESS))),
min(255, max(ENEMY_RGB_FLOOR, int(b * ENEMY_BRIGHTNESS))),
)
class SkyfallEnemy:
"""Animated UFO with its own angled descent."""
def __init__(self, x, y, sprite_type=None):
self.sprite = copy.deepcopy(LED.ShipSprites[sprite_type or random.choice(ENEMY_SHIP_TYPES)])
self.x = float(x)
self.y = float(y)
speed = random.uniform(ENEMY_SPEED_MIN, ENEMY_SPEED_MAX)
target_x = random.uniform(WIDTH * 0.2, WIDTH * 0.8)
self.vx, self.vy = _velocity_toward(x, y, target_x, HEIGHT, speed)
self.alive = True
self.ticks = 0
self.currentframe = 1
self._pixel_cache_frame = 0
self._pixel_cache = []
def move(self, step=1.0):
self.x += self.vx * step
self.y += self.vy * step
self.ticks += 1
framerate = max(1, self.sprite.framerate) * ENEMY_ANIMATION_SLOWDOWN
if self.ticks >= framerate:
self.currentframe += 1
self.ticks = 0
if self.currentframe > self.sprite.frames:
self.currentframe = 1
def off_screen(self, width, height):
margin = max(self.sprite.width, self.sprite.height) + 2
if self.y - margin > height:
return True
if self.x < -margin or self.x > width + margin:
return True
return False
def sprite_pixels(self):
frame = self.currentframe
if frame > self.sprite.frames or frame == 0:
frame = 1
if frame == self._pixel_cache_frame:
return self._pixel_cache
grid = self.sprite.grid[frame]
pixels = []
sw = self.sprite.width
for count in range(sw * self.sprite.height):
y, x = divmod(count, sw)
r, g, b = LED.ColorList[grid[count]]
if r > 0 or g > 0 or b > 0:
pixels.append((x, y, _brighten_enemy_rgb(r, g, b)))
self._pixel_cache_frame = frame
self._pixel_cache = pixels
return pixels
def hit_test(self, px, py):
sx = int(round(self.x))
sy = int(round(self.y))
sw = self.sprite.width
sh = self.sprite.height
if px < sx or px >= sx + sw or py < sy or py >= sy + sh:
return False
for x, y, _ in self.sprite_pixels():
if sx + x == px and sy + y == py:
return True
return False
def draw(self, canvas):
sx = int(round(self.x))
sy = int(round(self.y))
for x, y, rgb in self.sprite_pixels():
px = sx + x
py = sy + y
if 0 <= px < WIDTH and 0 <= py < HEIGHT:
canvas.SetPixel(px, py, *rgb)
def _enemy_to_particles(enemy):
"""Convert a UFO sprite into colored debris — Defender-style particles."""
particles = []
sx = int(round(enemy.x))
sy = int(round(enemy.y))
for x, y, rgb in enemy.sprite_pixels():
px = sx + x
py = sy + y
vel_x = random.uniform(-0.9, 0.9) * 1.15
vel_y = random.uniform(-0.5, 0.8) * 1.15
particles.append(DebrisParticle(px, py, *rgb, vel_x, vel_y))
return particles
class Bullet:
__slots__ = ("x", "y", "vx", "vy", "alive", "shotgun")
def __init__(self, x, y, vx=0.0, vy=None, shotgun=False):
self.x = float(x)
self.y = float(y)
self.vx = float(vx)
self.vy = float(-BULLET_SPEED if vy is None else vy)
self.alive = True
self.shotgun = shotgun
def move(self, step=1.0):
self.x += self.vx * step
self.y += self.vy * step
def draw(self, canvas):
speed = math.hypot(self.vx, self.vy) or 1.0
trail_x = self.vx / speed
trail_y = self.vy / speed
for i in range(BULLET_STREAK_LEN):
px = int(round(self.x + trail_x * i))
py = int(round(self.y + trail_y * i))
if not (0 <= px < WIDTH and 0 <= py < HEIGHT):
continue
if i == 0:
canvas.SetPixel(px, py, 255, 255, 255)
else:
fade = max(48, 220 - i * 55)
canvas.SetPixel(px, py, fade, fade, min(255, fade + 20))
def _ship_pixels(ship_x, ship_y):
pixels = []
half = SHIP_WIDTH // 2
for row, mask in enumerate(SHIP_SHAPE):
for col, on in enumerate(mask):
if on:
pixels.append((int(ship_x) - half + col, ship_y + row))
return pixels
def _draw_ship(canvas, ship_x, ship_y):
half = SHIP_WIDTH // 2
for row, mask in enumerate(SHIP_SHAPE):
for col, on in enumerate(mask):
if not on:
continue
px = int(ship_x) - half + col
py = ship_y + row
if not (0 <= px < WIDTH and 0 <= py < HEIGHT):
continue
if row == 0:
canvas.SetPixel(px, py, *SHIP_NOSE_RGB)
else:
canvas.SetPixel(px, py, *SHIP_RGB)
def _asteroids_overlap(a, b):
touch_dist = (a.collision_radius() + b.collision_radius()) * ASTEROID_COLLIDE_SCALE
dx = b.x - a.x
dy = b.y - a.y
dist = math.hypot(dx, dy)
return dist < touch_dist, dx, dy, dist, touch_dist
def _bounce_asteroid_pair(a, b):
"""Elastic bounce — rocks ricochet apart on contact."""
overlap, dx, dy, dist, touch_dist = _asteroids_overlap(a, b)
if not overlap:
return
if dist < 0.01:
nx = random.choice((-1.0, 1.0))
ny = random.uniform(-0.4, 0.4)
norm = math.hypot(nx, ny) or 1.0
nx /= norm
ny /= norm
dist = 1.0
else:
nx = dx / dist
ny = dy / dist
separation = touch_dist - dist
if separation > 0:
push = separation * 0.52
a.x -= nx * push
a.y -= ny * push
b.x += nx * push
b.y += ny * push
m1 = a.size * a.size
m2 = b.size * b.size
total_mass = m1 + m2
rel_vn = (a.vx - b.vx) * nx + (a.vy - b.vy) * ny
if rel_vn < 0:
impulse = 2 * rel_vn / total_mass
a.vx -= impulse * m2 * nx
a.vy -= impulse * m2 * ny
b.vx += impulse * m1 * nx
b.vy += impulse * m1 * ny
a.bounce_cooldown = ASTEROID_BOUNCE_COOLDOWN
b.bounce_cooldown = ASTEROID_BOUNCE_COOLDOWN
def _resolve_asteroid_bounces(asteroids):
alive = [a for a in asteroids if a.alive]
for i in range(len(alive)):
for j in range(i + 1, len(alive)):
a, b = alive[i], alive[j]
if a.bounce_cooldown > 0 and b.bounce_cooldown > 0:
continue
_bounce_asteroid_pair(a, b)
def _enemy_hits_asteroid(enemy, asteroid):
reach = asteroid.collision_radius() + max(enemy.sprite.width, enemy.sprite.height)
if math.hypot(asteroid.x - enemy.x, asteroid.y - enemy.y) > reach:
return False
sx = int(round(enemy.x))
sy = int(round(enemy.y))
for x, y, _ in enemy.sprite_pixels():
if asteroid.hit_test(sx + x, sy + y):
return True
return False
def _resolve_enemy_rock_collisions(enemies, asteroids):
"""UFOs shatter into debris when they strike a falling rock."""
particles = []
for enemy in enemies:
if not enemy.alive:
continue
for asteroid in asteroids:
if not asteroid.alive:
continue
if _enemy_hits_asteroid(enemy, asteroid):
particles.extend(_enemy_to_particles(enemy))
enemy.alive = False
break
return particles
def _star_rgb(brightness, purple=False):
"""Blue-tinted star field — SpaceExplorer palette."""
brightness = max(1, int(brightness * STAR_DIM_FACTOR))
if purple:
return (
max(0, min(255, brightness * 45 // 100)),
max(0, min(255, brightness * 22 // 100)),
max(0, min(255, brightness * 88 // 100)),
)
return (
max(0, brightness // 5),
max(0, brightness // 3),
brightness,
)
def _create_parallax_star_map(width, layer_height, starchance, brightness_range):
"""Tall star tile — sampled each frame as the field scrolls downward."""
layer = [[(0, 0, 0) for _ in range(width)] for _ in range(layer_height)]
bmin, bmax = brightness_range
purple_positions = []
for y in range(layer_height):
for x in range(width):
if random.randint(0, starchance) != 1:
continue
brightness = random.randint(bmin, bmax)
layer[y][x] = _star_rgb(brightness)
purple_positions.append((x, y, brightness))
for x, y, brightness in random.sample(
purple_positions, min(3, len(purple_positions)),
):
layer[y][x] = _star_rgb(brightness, purple=True)
return layer
class Planet:
"""Small water-world with smooth cloud cover — no rings."""
OCEAN_DEEP = (18, 55, 105)
OCEAN_MID = (35, 95, 155)
OCEAN_SHALLOW = (55, 130, 185)
CLOUD_WISP = (165, 200, 225)
CLOUD_BRIGHT = (205, 228, 245)
def __init__(self, cx, cy, radius=None, seed=None):
self.cx = cx
self.cy = cy
self.radius = radius if radius is not None else random.randint(
PLANET_MIN_RADIUS, PLANET_MAX_RADIUS,
)
self.seed = seed if seed is not None else random.random() * 1000.0
@property
def extent(self):
return self.radius, self.radius
def _cloud_cover(self, u, v):
"""Smooth sinusoidal cloud field in normalized disk coordinates."""
s = self.seed
cover = (
0.50
+ 0.26 * math.sin(u * 2.35 + v * 1.85 + s)
+ 0.18 * math.cos(u * 3.80 - v * 2.95 + s * 1.55)
+ 0.14 * math.sin((u + v) * 5.10 + s * 0.85)
+ 0.10 * math.cos(u * 6.40 + v * 4.20 - s * 1.20)
)
return max(0.0, min(1.0, cover))
def _ocean_rgb(self, u, v, r_norm):
lat = math.sin(v * 2.65 + self.seed * 0.4) * 0.5 + 0.5
if lat < 0.34:
base = self.OCEAN_DEEP
elif lat < 0.68:
base = self.OCEAN_MID
else:
base = self.OCEAN_SHALLOW
limb = 1.0 - 0.38 * r_norm ** 1.18
light = 1.0 + 0.14 * max(0.0, (-u * 0.55 - v * 0.70))
factor = max(0.32, min(1.25, limb * light))
return tuple(min(255, int(channel * factor)) for channel in base)
def _blend_rgb(self, ocean, cover):
if cover < 0.58:
return ocean
blend = min(1.0, (cover - 0.58) / 0.42)
cloud_rgb = self.CLOUD_BRIGHT if cover > 0.82 else self.CLOUD_WISP
return tuple(
min(255, int(ocean[i] * (1.0 - blend) + cloud_rgb[i] * blend))
for i in range(3)
)
def rgb_at(self, dx, dy):
dist = math.hypot(dx, dy)
if dist > self.radius:
return None
u = dx / max(self.radius, 1)
v = dy / max(self.radius, 1)
r_norm = dist / max(self.radius, 1)
ocean = self._ocean_rgb(u, v, r_norm)
cover = self._cloud_cover(u, v)
return self._blend_rgb(ocean, cover)
def paint_to_map(self, layer, width, layer_height):
pad = 2
for dy in range(-self.radius - pad, self.radius + pad + 1):
for dx in range(-self.radius - pad, self.radius + pad + 1):
x = self.cx + dx
y = self.cy + dy
if not (0 <= x < width and 0 <= y < layer_height):
continue
rgb = self.rgb_at(dx, dy)
if rgb is not None:
layer[y][x] = rgb
def _gas_giant_rgb(dx, dy, radius, colors):
dist = math.hypot(dx, dy)
if dist > radius:
return None
band_idx = int((dy / max(1.0, radius * 0.42)) + 1.5) % len(colors)
r, g, b = colors[band_idx]
limb = 1.0 - 0.42 * (dist / max(radius, 1)) ** 1.15
lit = 1.0 + 0.22 * max(0.0, (-dx * 0.65 - dy * 0.75) / radius)
factor = max(0.38, min(1.3, limb * lit))
return tuple(min(255, int(channel * factor)) for channel in (r, g, b))
def _paint_gas_giant_to_map(layer, width, layer_height, cx, cy, radius, colors, ringed):
for dy in range(-radius - 4, radius + 5):
for dx in range(-radius - 8, radius + 9):
x = cx + dx
y = cy + dy
if not (0 <= x < width and 0 <= y < layer_height):
continue
if ringed and abs(dy) <= max(2, radius // 7):
ring_dist = abs(math.hypot(dx, dy) - radius * 0.92)
if ring_dist < 2.2 and abs(dx) > radius * 0.35:
ring_rgb = tuple(
min(255, int(channel * 0.72 + colors[1][i] * 0.28))
for i, channel in enumerate(colors[0])
)
layer[y][x] = ring_rgb
continue
rgb = _gas_giant_rgb(dx, dy, radius, colors)
if rgb is not None:
layer[y][x] = rgb
def _gas_giant_extent(radius, ringed):
if ringed:
return radius + 8, radius + 4
return radius, radius
def _gas_giant_layer_height(display_height):
"""Tall scroll map for gas giants, spaced ~20s apart."""
max_extent_y = GAS_GIANT_MAX_RADIUS + 4
scroll_gap = int(GAS_GIANT_APPEAR_INTERVAL * GAS_GIANT_SCROLL_SPEED * TARGET_FPS)
slot_height = display_height + 2 * max_extent_y
return GAS_GIANT_COUNT * (scroll_gap + slot_height)
def _planet_layer_height(display_height):
"""Tall scroll map for water worlds, spaced ~20s apart."""
max_extent_y = PLANET_MAX_RADIUS + 2
scroll_gap = int(PLANET_APPEAR_INTERVAL * PLANET_SCROLL_SPEED * TARGET_FPS)
slot_height = display_height + 2 * max_extent_y
return PLANET_COUNT * (scroll_gap + slot_height)
def _place_gas_giant(layer, width, layer_height, slot_start, slot_height):
radius = random.randint(GAS_GIANT_MIN_RADIUS, GAS_GIANT_MAX_RADIUS)
palettes = (
((185, 145, 95), (150, 105, 65), (205, 165, 105)),
((215, 185, 135), (165, 135, 95), (110, 85, 60)),
((75, 115, 175), (50, 85, 140), (115, 150, 205)),
((120, 85, 150), (85, 55, 110), (160, 120, 185)),
)
colors = random.choice(palettes)