-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest3.py
More file actions
888 lines (682 loc) · 30.3 KB
/
test3.py
File metadata and controls
888 lines (682 loc) · 30.3 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
# =====================================================================================
# LED ASTEROID FIELD SIMULATION — With Ship AI, Missiles, Sparks, and Black Hole Logic
#
# Author : William McEvoy
# Framework : LEDarcade (custom game and display framework for LED matrices)
# Hardware : Raspberry Pi w/ Adafruit or compatible RGB LED Matrix (e.g. 64x32)
# Dependencies : pygame, numpy, numba, LEDarcade (custom), Pillow (for fonts/images)
#
# Description :
# This is a real-time arcade-style simulation rendered on an LED matrix display. The
# game features autonomous AI ship navigation, dynamic asteroid fields, projectile
# tracking (missiles), particle effects (sparks), and gravitational anomalies (black
# holes). Entities interact with physics-inspired behaviors including momentum, elastic
# collisions, gravity fields, and boundary reflection.
#
# Core Features:
# - Autonomous AI-controlled ship with vision-based targeting and thrust behavior
# - Asteroid spawning, health, splitting, and collision physics (elastic, resolved)
# - Black holes that attract nearby objects and destroy on proximity/contact
# - Missile launching with limited lifespan and projectile targeting
# - Real-time particle system for visual effects (sparks on impact/destruction)
# - LED-safe rendering pipeline using off-screen buffers and VSync frame swapping
# - Numba-accelerated collision physics for performance on embedded hardware
# - FPS tracking and average asteroid speed diagnostics
#
# Architectural Notes:
# - GameObject is the parent class for all moving entities (Ship, Asteroids, Missiles)
# - All rendering is done via LED.setpixelCanvas() into the back buffer, followed by
# SwapOnVSync() to commit the frame
# - A viewport window into the larger virtual space is centered around the display
# - Ship behavior uses angular targeting and thrust arcs to avoid jittery movement
# - `@njit` is used where high-frequency numeric operations are required (Numba)
#
# Known Limitations / To-Do:
# - No player input; all gameplay is autonomous (demo-style)
# - Ship has no health system (can’t be destroyed)
# - No high score tracking or persistent state
#
# Version : 1.0.0
# License : Non-commercial use only. Contact william.mcevoy@gmail.com for licensing.
#
# =====================================================================================
import LEDarcade as LED
import random
import time
import math
import pygame
from datetime import datetime
LED.Initialize()
# --- Black Hole Parameters ---
BLACKHOLE_GRAVITY = 6
BLACKHOLE_MIN_SIZE = 1
BLACKHOLE_MAX_SIZE = 4
BLACKHOLE_MAX_SPEED = 2
BLACKHOLE_APPEAR_INTERVAL = 60
BLACKHOLE_LIFESPAN = 25
BLACKHOLE_GROW_DURATION = 2
# --- Ship Parameters ---
MAX_SPEED = 0.5
SHIP_THRUST = 0.01
SHIP_COLOR = (0, 255, 0)
SHIP_THRUST_DURATION = 3.0
SHIP_THRUST_COOLDOWN = 1.0
THRUST_TRAIL_LENGTH = 8
SHIP_VISION_RADIUS = 40
# --- Missile Parameters ---
MISSILE_SPEED = 1.25
MISSILE_LIFESPAN = 0.75
FIRE_CHANCE = 0.1
MISSILE_COLOR = (255, 255, 255)
MISSILE_TRAIL_MIN = 50
MISSILE_TRAIL_LENGTH = 8
MAX_MISSILES = 2
# --- Terminal Parameters ---
ScrollSleep = 0.025
MainSleep = 0.05
TerminalTypeSpeed = 0.02 #pause in seconds between characters
TerminalScrollSpeed = 0.02 #pause in seconds between new lines
CursorRGB = (0,255,0)
CursorDarkRGB = (0,50,0)
# --- Asteroid Parameters ---
ASTEROIDS = 3
ASTEROID_SPLIT_THRESHOLD = 2
MAX_ASTEROID_SIZE = 4
FRAME_DELAY = 0.03
ASTEROID_MIN_SPEED = 0.05
ASTEROID_MAX_SPEED = 0.4
THRUST_TRAIL_COLOR = (255, 0, 0)
ASTEROID_TARGET_COLOR = (255, 255, 0)
ASTEROID_CROSSHAIR_COLOR = (255, 0, 255)
ASTEROID_LIGHTING_CONTRAST = 1
ASTEROID_COLOR_MIN_BRIGHTNESS = 150
ASTEROID_COLOR_MAX_BRIGHTNESS = 250
ASTEROID_COLOR_OPTIONS = [
(ASTEROID_COLOR_MIN_BRIGHTNESS, ASTEROID_COLOR_MIN_BRIGHTNESS, ASTEROID_COLOR_MIN_BRIGHTNESS), # dark grey
(ASTEROID_COLOR_MIN_BRIGHTNESS, ASTEROID_COLOR_MIN_BRIGHTNESS, ASTEROID_COLOR_MAX_BRIGHTNESS), # deep blue
(ASTEROID_COLOR_MAX_BRIGHTNESS, 0, ASTEROID_COLOR_MAX_BRIGHTNESS) # deep purple
]
# --- Spark Effects ---
SPARK_COUNT = 10
SPARK_SPEED_MIN = 0.001
SPARK_SPEED_MAX = 0.2
SPARK_TRAIL_LENGTH = 8
SPARK_COLOR = (255, 200, 100)
# --- Display Settings ---
WIDTH = LED.HatWidth
HEIGHT = LED.HatHeight
SCROLL_FONT_SIZE = 12
# --- Virtual Playfield Size ---
PLAYFIELD_WIDTH = 68
PLAYFIELD_HEIGHT = 40
# Center the visible matrix in the virtual playfield
VIEWPORT_X_OFFSET = (PLAYFIELD_WIDTH - WIDTH) // 2
VIEWPORT_Y_OFFSET = (PLAYFIELD_HEIGHT - HEIGHT) // 2
# At the top of PlayBlasteroids, ensure b
global blackhole
blackhole = None # Initialize explicitly
#Time date
last_time_str = ""
ClockFontSize = 12
ClockRGB = (0,200,0)
clock_img = LED.GenerateClockImageWithFixedTiles(FontSize=ClockFontSize, TextColor=ClockRGB, BackgroundColor=(0, 0, 0))
from numba import njit
import numpy as np
def draw_clock_overlay(clock_image):
"""
Draws the clock image centered on the LED display.
Parameters:
clock_image (PIL.Image): The clock image to draw.
"""
pixels = clock_image.load()
img_w, img_h = clock_image.size
# Compute top-left offset to center the image
H = (WIDTH - img_w) // 2
V = (HEIGHT - img_h) // 2
for y in range(img_h):
for x in range(img_w):
r, g, b = pixels[x, y]
px = H + x
py = V + y
if 0 <= px < WIDTH and 0 <= py < HEIGHT:
if (r, g, b) != (0, 0, 0):
# Offset by 1 to nudge the clock down slightly for visual centering
LED.setpixelCanvas(px, py +1, r, g, b)
# Boundary Bounce Logic for Virtual Playfield
@njit
def enforce_bounds_and_bounce(x, y, dx, dy, width, height):
if x < 0:
x = 0
dx = -dx
elif x > width:
x = width
dx = -dx
if y < 0:
y = 0
dy = -dy
elif y > height:
y = height
dy = -dy
return x, y, dx, dy
class GameObject:
def __init__(self, x, y, dx, dy):
self.x = x
self.y = y
self.dx = dx
self.dy = dy
def update_position(self):
# Let subclasses define max speed if needed
max_speed = getattr(self, 'max_speed', None)
if max_speed is not None:
speed = math.hypot(self.dx, self.dy)
# Allow higher speed if near a black hole
if blackhole and isinstance(self, Asteroid):
dist_sq = (self.x - blackhole.x)**2 + (self.y - blackhole.y)**2
if dist_sq < 100: # Within 10 units of black hole
max_speed *= 2 # Double max speed to allow gravity to have more impact
if speed > max_speed:
scale = max_speed / speed
self.dx *= scale
self.dy *= scale
self.x += self.dx
self.y += self.dy
self.x, self.y, self.dx, self.dy = enforce_bounds_and_bounce(
self.x, self.y, self.dx, self.dy, PLAYFIELD_WIDTH, PLAYFIELD_HEIGHT
)
def update_position_old(self):
# Let subclasses define max speed if needed
max_speed = getattr(self, 'max_speed', None)
if max_speed is not None:
speed = math.hypot(self.dx, self.dy)
if speed > max_speed:
scale = max_speed / speed
self.dx *= scale
self.dy *= scale
self.x += self.dx
self.y += self.dy
self.x, self.y, self.dx, self.dy = enforce_bounds_and_bounce(
self.x, self.y, self.dx, self.dy, PLAYFIELD_WIDTH, PLAYFIELD_HEIGHT
)
class BlackHole(GameObject):
def __init__(self, x, y, radius,size=1):
self.x = x
self.y = y
self.spawn_time = time.time()
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(0.05, 0.2)
dx = math.cos(angle) * speed
dy = math.sin(angle) * speed
super().__init__(x, y, dx, dy)
self.size = 1
self.max_speed = BLACKHOLE_MAX_SPEED
self.growing = True
self.grow_start_time = time.time()
self.target_size = radius # unify
self.radius = 1 # will grow from 1 to target
def move(self):
self.update_position() # from GameObject
def expired(self):
out_of_bounds = (
self.x < -self.radius or self.x > PLAYFIELD_WIDTH + self.radius or
self.y < -self.radius or self.y > PLAYFIELD_HEIGHT + self.radius
)
return out_of_bounds or (time.time() - self.spawn_time > BLACKHOLE_LIFESPAN)
def draw(self):
grow_duration = BLACKHOLE_GROW_DURATION
elapsed = time.time() - self.grow_start_time
if self.growing:
grow_factor = elapsed / grow_duration
if elapsed >= grow_duration:
self.size = self.target_size
self.growing = False
else:
self.size = max(1, int(round(self.target_size * grow_factor)))
else:
self.size = self.target_size
self.radius = self.size # Update radius used for interaction
for dy in range(-self.radius, self.radius + 1):
for dx in range(-self.radius, self.radius + 1):
px = int(self.x + dx)
py = int(self.y + dy)
if (VIEWPORT_X_OFFSET <= px < VIEWPORT_X_OFFSET + WIDTH and
VIEWPORT_Y_OFFSET <= py < VIEWPORT_Y_OFFSET + HEIGHT):
dist = math.hypot(dx, dy)
# Fill everything within the radius — outer ring white, inner black
if dist <= self.radius:
if dist >= self.radius - 1:
LED.setpixelCanvas(px - VIEWPORT_X_OFFSET, py - VIEWPORT_Y_OFFSET, 255, 255, 255)
else:
LED.setpixelCanvas(px - VIEWPORT_X_OFFSET, py - VIEWPORT_Y_OFFSET, 1, 1, 1)
# Attraction & Collision (apply per object)
def apply_blackhole_gravity(obj, asteroids_list=None):
if not blackhole:
return
dx = blackhole.x - obj.x
dy = blackhole.y - obj.y
dist_sq = dx * dx + dy * dy
if dist_sq == 0:
return
dist = math.sqrt(dist_sq)
# Calculate effective radii (radius = 2 * size)
obj_radius = obj.size * 2 if hasattr(obj, 'size') else 0
blackhole_radius = blackhole.radius * 2
# Check for collision: distance <= sum of effective radii
if dist <= obj_radius + blackhole_radius and isinstance(obj, Asteroid) and asteroids_list is not None:
asteroids_list.remove(obj) # Remove asteroid on collision
return
# Apply gravity if no collision
force = BLACKHOLE_GRAVITY * blackhole.radius / dist_sq
if hasattr(obj, 'dx') and hasattr(obj, 'dy'):
obj.dx += force * dx / dist
obj.dy += force * dy / dist
elif hasattr(obj, 'speed_x') and hasattr(obj, 'speed_y'):
obj.speed_x += force * dx / dist
obj.speed_y += force * dy / dist
@njit
def compute_collisions(positions, velocities, sizes):
n = len(sizes)
for i in range(n):
for j in range(i + 1, n):
dx = positions[j, 0] - positions[i, 0]
dy = positions[j, 1] - positions[i, 1]
dist_sq = dx * dx + dy * dy
min_dist = sizes[i] + sizes[j]
if dist_sq < min_dist * min_dist:
dist = math.sqrt(dist_sq)
if dist == 0:
continue
nx, ny = dx / dist, dy / dist
tx, ty = -ny, nx
dpTan1 = velocities[i, 0] * tx + velocities[i, 1] * ty
dpTan2 = velocities[j, 0] * tx + velocities[j, 1] * ty
dpNorm1 = velocities[i, 0] * nx + velocities[i, 1] * ny
dpNorm2 = velocities[j, 0] * nx + velocities[j, 1] * ny
velocities[i, 0] = tx * dpTan1 + nx * dpNorm2
velocities[i, 1] = ty * dpTan1 + ny * dpNorm2
velocities[j, 0] = tx * dpTan2 + nx * dpNorm1
velocities[j, 1] = ty * dpTan2 + ny * dpNorm1
overlap = 0.5 * (min_dist - dist + 0.01)
positions[i, 0] -= nx * overlap
positions[i, 1] -= ny * overlap
positions[j, 0] += nx * overlap
positions[j, 1] += ny * overlap
def handle_collisions(asteroids):
n = len(asteroids)
positions = np.zeros((n, 2), dtype=np.float32)
velocities = np.zeros((n, 2), dtype=np.float32)
sizes = np.zeros(n, dtype=np.float32)
for i, a in enumerate(asteroids):
positions[i] = [a.x, a.y]
velocities[i] = [a.dx, a.dy]
sizes[i] = a.size
compute_collisions(positions, velocities, sizes)
for i, a in enumerate(asteroids):
a.x, a.y = positions[i]
a.dx, a.dy = velocities[i]
class Asteroid(GameObject):
def __init__(self, x=None, y=None, size=None, color=None):
x = x if x is not None else random.uniform(0, WIDTH)
y = y if y is not None else random.uniform(0, HEIGHT)
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(ASTEROID_MIN_SPEED, ASTEROID_MAX_SPEED)
dx = math.cos(angle) * speed
dy = math.sin(angle) * speed
super().__init__(x, y, dx, dy)
self.max_speed = ASTEROID_MAX_SPEED
self.target_size = size if size is not None else random.randint(2, MAX_ASTEROID_SIZE)
self.size = 1 # Start small
self.growing = True
self.grow_start_time = time.time()
self.health = self.target_size * 2
self.last_hit_time = 0
if color:
self.color = color
else:
roll = random.random()
if roll < 0.9:
self.color = ASTEROID_COLOR_OPTIONS[0]
elif roll < 0.95:
self.color = ASTEROID_COLOR_OPTIONS[1]
else:
self.color = ASTEROID_COLOR_OPTIONS[2]
@classmethod
def create(cls, **kwargs):
asteroid = cls(**kwargs)
asteroid.growing = True
asteroid.grow_start_time = time.time()
asteroid.size = 1
return asteroid
def move(self):
self.update_position()
def draw(self):
if not hasattr(self, "_debug_draw_count"):
self._debug_draw_count = 0
self._debug_draw_count += 1
if self.growing:
grow_duration = 0.5
elapsed = time.time() - self.grow_start_time
if elapsed >= grow_duration:
self.size = self.target_size
self.growing = False
else:
grow_factor = elapsed / grow_duration
self.size = max(1, int(self.target_size * grow_factor))
grow_factor = elapsed / grow_duration
if elapsed >= grow_duration:
self.size = self.target_size
self.growing = False
else:
self.size = max(1, int(self.target_size * grow_factor))
if grow_factor >= 1.0:
self.growing = False
else:
self.size = self.target_size
r, g, b = self.color
for i in range(-self.size, self.size + 1):
for j in range(-self.size, self.size + 1):
if i**2 + j**2 <= self.size**2:
px = int(self.x) + i
py = int(self.y) + j
if (VIEWPORT_X_OFFSET <= px < VIEWPORT_X_OFFSET + WIDTH and
VIEWPORT_Y_OFFSET <= py < VIEWPORT_Y_OFFSET + HEIGHT):
brightness_factor = 1.0 - ASTEROID_LIGHTING_CONTRAST * (i + j) / (2 * self.size)
brightness_factor = max(0.5, min(1.5, brightness_factor))
r_out = min(255, int(r * brightness_factor))
g_out = min(255, int(g * brightness_factor))
b_out = min(255, int(b * brightness_factor))
LED.setpixelCanvas(px - VIEWPORT_X_OFFSET, py - VIEWPORT_Y_OFFSET, r_out, g_out, b_out)
class Missile(GameObject):
def __init__(self, x, y, angle):
self.birth_time = time.time()
dx = math.cos(angle) * MISSILE_SPEED
dy = math.sin(angle) * MISSILE_SPEED
super().__init__(x, y, dx, dy)
self.angle = angle
self.speed = MISSILE_SPEED
def move(self):
self.x += self.dx
self.y += self.dy
def draw(self):
for i in range(MISSILE_TRAIL_LENGTH):
tx = int(self.x - math.cos(self.angle) * i)
ty = int(self.y - math.sin(self.angle) * i)
brightness = max(MISSILE_TRAIL_MIN, MISSILE_COLOR[0] - i * (MISSILE_COLOR[0] // MISSILE_TRAIL_LENGTH))
LED.setpixelCanvas(tx - VIEWPORT_X_OFFSET, ty - VIEWPORT_Y_OFFSET, brightness, brightness, brightness)
class Ship(GameObject):
def __init__(self):
super().__init__(PLAYFIELD_WIDTH // 2, PLAYFIELD_HEIGHT // 2, 0.0, 0.0)
self.angle = 0
self.frame = 0
self.color = SHIP_COLOR
self.speed_x = 0
self.speed_y = 0
self.target = None
self.last_thrust_time = 0
self.thrusting = False
def move(self):
if not self.target or self.target not in asteroids:
cx, cy = PLAYFIELD_WIDTH / 2, PLAYFIELD_HEIGHT / 2
visible_targets = [a for a in asteroids if (a.x - cx)**2 + (a.y - cy)**2 <= SHIP_VISION_RADIUS**2]
if visible_targets:
self.target = random.choice(visible_targets)
else:
self.target = None
dx = cx - self.x
dy = cy - self.y
angle_to_center = math.atan2(dy, dx)
self.angle = angle_to_center
ax = math.cos(self.angle) * SHIP_THRUST
ay = math.sin(self.angle) * SHIP_THRUST
self.speed_x += ax
self.speed_y += ay
if self.target:
current_time = time.time()
if self.thrusting and current_time - self.last_thrust_time > SHIP_THRUST_DURATION:
self.thrusting = False
self.last_thrust_time = current_time
elif not self.thrusting and current_time - self.last_thrust_time > SHIP_THRUST_COOLDOWN:
self.thrusting = True
self.last_thrust_time = current_time
dx = self.target.x - self.x
dy = self.target.y - self.y
desired_angle = math.atan2(dy, dx)
angle_diff = (desired_angle - self.angle + math.pi) % (2 * math.pi) - math.pi
self.angle += max(-0.5, min(0.5, angle_diff))
if self.thrusting:
if abs(angle_diff) > math.pi / 2:
thrust = SHIP_THRUST * 4.0
elif abs(angle_diff) > math.pi / 4:
thrust = SHIP_THRUST * 2.0
else:
thrust = SHIP_THRUST
ax = math.cos(self.angle) * thrust
ay = math.sin(self.angle) * thrust
self.speed_x += ax
self.speed_y += ay
speed = math.hypot(self.speed_x, self.speed_y)
if speed > MAX_SPEED:
scale = MAX_SPEED / speed
self.speed_x *= scale
self.speed_y *= scale
self.x += self.speed_x
self.y += self.speed_y
# Clamp to visible area with 1 pixel margin
self.x = max(VIEWPORT_X_OFFSET + 1, min(self.x, VIEWPORT_X_OFFSET + WIDTH - 2))
self.y = max(VIEWPORT_Y_OFFSET + 1, min(self.y, VIEWPORT_Y_OFFSET + HEIGHT - 2))
self.frame += 1
def draw(self):
cx = int(self.x)
cy = int(self.y)
r, g, b = self.color
if self.thrusting:
current_thrust = math.hypot(self.speed_x, self.speed_y)
trail_strength = int(min(current_thrust / MAX_SPEED, 1.0) * THRUST_TRAIL_LENGTH)
for i in range(1, trail_strength + 1):
tx = int(self.x - math.cos(self.angle) * i)
ty = int(self.y - math.sin(self.angle) * i)
red_intensity = max(0, THRUST_TRAIL_COLOR[0] - i * (THRUST_TRAIL_COLOR[0] // THRUST_TRAIL_LENGTH))
LED.setpixelCanvas(tx - VIEWPORT_X_OFFSET, ty - VIEWPORT_Y_OFFSET, red_intensity, 0, 0)
LED.setpixelCanvas(cx - VIEWPORT_X_OFFSET, cy - VIEWPORT_Y_OFFSET, r, g, b)
dx = int(round(math.cos(self.angle)))
dy = int(round(math.sin(self.angle)))
LED.setpixelCanvas(int(cx - dx - VIEWPORT_X_OFFSET), int(cy - dy - VIEWPORT_Y_OFFSET), r, g, b)
class Spark:
def __init__(self, x, y, angle, speed):
self.x = x
self.y = y
self.angle = angle
self.speed = speed
self.lifespan = SPARK_TRAIL_LENGTH
def move(self):
self.x += math.cos(self.angle) * self.speed
self.y += math.sin(self.angle) * self.speed
def draw(self):
for i in range(SPARK_TRAIL_LENGTH):
tx = int(self.x - math.cos(self.angle) * i)
ty = int(self.y - math.sin(self.angle) * i)
fade = max(0, SPARK_COLOR[0] - i * (SPARK_COLOR[0] // SPARK_TRAIL_LENGTH))
LED.setpixelCanvas(tx - VIEWPORT_X_OFFSET, ty - VIEWPORT_Y_OFFSET, fade, fade * 3 // 4, fade // 2)
missiles = []
ship = Ship()
asteroids = [Asteroid.create() for _ in range(ASTEROIDS)]
sparks = []
clock = pygame.time.Clock()
fps_counter = 0
fps_timer = time.time()
# Insert into test2.py: Initialization Section
last_blackhole_time = time.time() - BLACKHOLE_APPEAR_INTERVAL
blackhole = None
def hard_clear_canvas():
for y in range(HEIGHT):
for x in range(WIDTH):
LED.Canvas.SetPixel(x,y,0,0,0)
def PlayBlasteroids(Duration = 10000, StopEvent = None):
global last_blackhole_time, fps_timer, fps_counter
global blackhole
#Time date
last_time_str = ""
ClockFontSize = 12
ClockRGB = (0,200,0)
clock_img = LED.GenerateClockImageWithFixedTiles(FontSize=ClockFontSize, TextColor=ClockRGB, BackgroundColor=(0, 0, 0))
for asteroid in asteroids:
asteroid.grow_start_time = time.time()
asteroid.draw()
try:
#zoom clock into screen
clock_img = LED.GenerateClockImageWithFixedTiles(FontSize=ClockFontSize, TextColor=ClockRGB, BackgroundColor=(0, 0, 0))
LED.ZoomImageObject(clock_img, 0, 110, 0.05, 20)
Done = False
while (Done == False):
hard_clear_canvas()
if StopEvent and StopEvent.is_set():
print("\n" + "="*40)
print("[Blasteroids] StopEvent received")
print("-> Shutting down gracefully...")
print("="*40 + "\n")
Done = True
break
now = time.time()
# Paint the time as the background
current_time_str = datetime.now().strftime('%H:%M')
if current_time_str != last_time_str:
clock_img = LED.GenerateClockImageWithFixedTiles(FontSize=ClockFontSize, TextColor=ClockRGB, BackgroundColor=(0, 0, 0))
last_time_str = current_time_str
print("Time: ",current_time_str)
draw_clock_overlay(clock_img)
if now - last_blackhole_time > BLACKHOLE_APPEAR_INTERVAL:
bx = PLAYFIELD_WIDTH // 2
by = PLAYFIELD_HEIGHT // 2
bradius = random.randint(BLACKHOLE_MIN_SIZE, BLACKHOLE_MAX_SIZE)
blackhole = BlackHole(bx, by, bradius)
blackhole.growing = True
blackhole.grow_start_time = time.time()
last_blackhole_time = time.time()
if blackhole and blackhole.expired():
blackhole = None
if not asteroids:
asteroids.append(Asteroid(size=MAX_ASTEROID_SIZE))
asteroids.append(Asteroid.create(size=MAX_ASTEROID_SIZE))
handle_collisions(asteroids)
new_asteroids = []
for asteroid in asteroids[:]: # Iterate over a copy to allow removal
apply_blackhole_gravity(asteroid, asteroids) # Pass asteroids list for removal
asteroid.move()
asteroid.draw()
if asteroid == ship.target:
cx = int(asteroid.x)
cy = int(asteroid.y)
LED.setpixelCanvas(cx - VIEWPORT_X_OFFSET, cy - VIEWPORT_Y_OFFSET, ASTEROID_TARGET_COLOR[0], ASTEROID_TARGET_COLOR[1], ASTEROID_TARGET_COLOR[2])
dx = ship.x - asteroid.x
dy = ship.y - asteroid.y
if dx * dx + dy * dy < asteroid.size * asteroid.size:
if time.time() - asteroid.last_hit_time > 1.0:
asteroid.health -= 1
asteroid.last_hit_time = time.time()
if asteroid.health <= 0:
if asteroid.size > 1:
new_asteroids.append(Asteroid(asteroid.x, asteroid.y, asteroid.size - 1, color=asteroid.color))
for _ in range(SPARK_COUNT):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(SPARK_SPEED_MIN, SPARK_SPEED_MAX)
sparks.append(Spark(asteroid.x, asteroid.y, angle, speed))
asteroids.remove(asteroid)
continue
angle = math.atan2(dy, dx)
thrust = MAX_SPEED
ship.speed_x = math.cos(angle + math.pi) * thrust
ship.speed_y = math.sin(angle + math.pi) * thrust
apply_blackhole_gravity(ship)
if len(missiles) < MAX_MISSILES and random.random() < FIRE_CHANCE and ship.target:
missiles.append(Missile(ship.x, ship.y, ship.angle))
for missile in missiles[:]:
apply_blackhole_gravity(missile)
missile.move()
missile.draw()
if time.time() - missile.birth_time > MISSILE_LIFESPAN:
missiles.remove(missile)
continue
hit_index = None
for idx, asteroid in enumerate(asteroids):
dx = missile.x - asteroid.x
dy = missile.y - asteroid.y
if dx * dx + dy * dy < asteroid.size * asteroid.size:
hit_index = idx
break
if hit_index is not None:
hit = asteroids.pop(hit_index)
spark_origin_x = hit.x
spark_origin_y = hit.y
if hit.size > 1:
new_asteroids.append(Asteroid(hit.x, hit.y, hit.size - 1, color=hit.color))
new_asteroids.append(Asteroid(hit.x, hit.y, hit.size - 1))
missiles.remove(missile)
for _ in range(SPARK_COUNT):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(SPARK_SPEED_MIN, SPARK_SPEED_MAX)
sparks.append(Spark(spark_origin_x, spark_origin_y, angle, speed))
elif not (0 <= missile.x < PLAYFIELD_WIDTH and 0 <= missile.y < PLAYFIELD_HEIGHT):
missiles.remove(missile)
asteroids.extend(new_asteroids)
for spark in sparks[:]:
spark.move()
spark.draw()
spark.lifespan -= 1
if spark.lifespan <= 0:
sparks.remove(spark)
ship.move()
ship.draw()
if blackhole:
blackhole.move()
blackhole.draw()
LED.Canvas = LED.TheMatrix.SwapOnVSync(LED.Canvas)
clock.tick(60) # Cap the frame rate to 60 FPS
fps_counter += 1
if time.time() - fps_timer >= 2.0:
asteroid_speeds = [math.hypot(a.dx, a.dy) for a in asteroids]
avg_speed = sum(asteroid_speeds) / len(asteroid_speeds) if asteroid_speeds else 0
print(f"FPS: {fps_counter / (time.time() - fps_timer):.2f} | Asteroids: {len(asteroids)} | Avg Asteroid Speed: {avg_speed:.3f}")
fps_counter = 0
fps_timer = time.time()
except KeyboardInterrupt:
LED.ClearBuffers()
LED.TheMatrix.SwapOnVSync(LED.Canvas)
raise
def LaunchBlasteroids(Duration = 10000,ShowIntro=True,StopEvent=None):
print("ShowIntro:",ShowIntro)
if(ShowIntro == True):
#--------------------------------------
# M A I N P R O C E S S I N G --
#--------------------------------------
LED.LoadConfigData()
LED.ShowTitleScreen(
BigText = 'BLAST!',
BigTextRGB = LED.HighOrange,
BigTextShadowRGB = LED.ShadowOrange,
LittleText = '',
LittleTextZoom = 1,
LittleTextRGB = LED.MedGreen,
LittleTextShadowRGB = (0,10,0),
ScrollText = 'Get those space rocks before they get you...',
ScrollTextRGB = LED.MedYellow,
ScrollSleep = 0.03, # time in seconds to control the scrolling (0.005 is fast, 0.1 is kinda slow)
DisplayTime = 1, # time in seconds to wait before exiting
ExitEffect = 0, # 0=Random / 1=shrink / 2=zoom out / 3=bounce / 4=fade /5=fallingsand
BigText2RGB = LED.HighBlue,
BigText2ShadowRGB = LED.ShadowRed,
)
LED.ClearBigLED()
LED.ClearBuffers()
CursorH = 0
CursorV = 0
LED.ScreenArray,CursorH,CursorV = LED.TerminalScroll(LED.ScreenArray,"somebody is chucking space rocks at your home planet....",CursorH=CursorH,CursorV=CursorV,MessageRGB=(225,0,0),CursorRGB=(0,255,0),CursorDarkRGB=(0,50,0),StartingLineFeed=1,TypeSpeed=TerminalTypeSpeed,ScrollSpeed=TerminalTypeSpeed)
LED.BlinkCursor(CursorH= CursorH,CursorV=CursorV,CursorRGB=CursorRGB,CursorDarkRGB=CursorDarkRGB,BlinkSpeed=0.5,BlinkCount=2)
LED.ScreenArray,CursorH,CursorV = LED.TerminalScroll(LED.ScreenArray,"go blast them, young pilot!",CursorH=CursorH,CursorV=CursorV,MessageRGB=(150,150,0),CursorRGB=(0,255,0),CursorDarkRGB=(0,50,0),StartingLineFeed=1,TypeSpeed=TerminalTypeSpeed,ScrollSpeed=TerminalTypeSpeed)
LED.BlinkCursor(CursorH= CursorH,CursorV=CursorV,CursorRGB=CursorRGB,CursorDarkRGB=CursorDarkRGB,BlinkSpeed=0.5,BlinkCount=2)
PlayBlasteroids(Duration=Duration,StopEvent=StopEvent)
#execute if this script is called direction
if __name__ == "__main__":
try:
LED.scroll_random_movie_intro()
LaunchBlasteroids(Duration=100000, ShowIntro=False, StopEvent=None)
except KeyboardInterrupt:
print("Exiting game due to Ctrl-C.")