-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlasteroids.py
More file actions
1312 lines (988 loc) · 45.6 KB
/
Blasteroids.py
File metadata and controls
1312 lines (988 loc) · 45.6 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
# =====================================================================================
# 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.
#
# The engine runs on a virtual playfield larger than the visible LED matrix, allowing
# for a scrolling effect and spatial simulation beyond the constrained screen size.
#
# 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
# --- Black Hole Parameters ---
BLACKHOLE_GRAVITY = 6
BLACKHOLE_MIN_SIZE = 1
BLACKHOLE_MAX_SIZE = 15
BLACKHOLE_MAX_SPEED = 2
BLACKHOLE_APPEAR_INTERVAL = 120
BLACKHOLE_LIFESPAN = 30
BLACKHOLE_GROW_DURATION = 2
# --- Ship Parameters ---
MAX_SPEED = 0.4
SHIP_THRUST = 0.006
SHIP_TURN_RATE = 0.3
SHIP_COLOR = (0, 155, 255)
SHIP_THRUST_DURATION = 3.0
SHIP_THRUST_COOLDOWN = 0.5
THRUST_TRAIL_LENGTH = 6
SHIP_VISION_RADIUS = 30
# --- Missile Parameters ---
MISSILE_SPEED = 1.25
MISSILE_LIFESPAN = 0.75
MISSILE_GRAVITY_FORCE = 0.1
FIRE_CHANCE = 0.1
MISSILE_COLOR = (255, 255, 255)
MISSILE_TRAIL_MIN_BRIGHTNESS = 10
MISSILE_TRAIL_LENGTH = 8
MAX_MISSILES = 1
# --- 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 = 2
ASTEROIDS_MERGE_CHANCE = 0.75
MAX_ASTEROID_SIZE = 15
MIN_ASTEROID_SIZE = 2
ASTEROID_GROWTH_DURATION = 0.25
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 = 100
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 = 6
SPARK_TRAIL_MAX_LENGTH = 8 #length gets modified by size of objects exploding, this clamps it
SPARK_COLOR = (255, 200, 100)
# --- Display Settings ---
WIDTH = LED.HatWidth
HEIGHT = LED.HatHeight
SCROLL_FONT_SIZE = 12
# --- Virtual Playfield Size ---
PLAYFIELD_WIDTH = 78
PLAYFIELD_HEIGHT = 50
# 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
from PIL import Image
def draw_clock_overlay(clock_image):
img_w, img_h = clock_image.size
H = (WIDTH - img_w) // 2
V = (HEIGHT - img_h) // 2
if not blackhole:
# Just draw centered
for y in range(img_h):
for x in range(img_w):
r, g, b = clock_image.getpixel((x, y))
if (r, g, b) != (0, 0, 0):
px = H + x
py = V + y
if 0 <= px < WIDTH and 0 <= py < HEIGHT:
LED.setpixelCanvas(px, py, r, g, b)
return
# Create a new blank image the same size as the LED matrix
stretched = Image.new("RGB", (WIDTH, HEIGHT), (0, 0, 0))
bhx = blackhole.x - VIEWPORT_X_OFFSET
bhy = blackhole.y - VIEWPORT_Y_OFFSET
# Paste original clock centered into LED-space image
stretched.paste(clock_image, (H, V))
# Warp pixels outward toward black hole
source = np.array(stretched)
target = np.zeros_like(source)
for y in range(HEIGHT):
for x in range(WIDTH):
dx = bhx - x
dy = bhy - y
dist_sq = dx * dx + dy * dy
if dist_sq == 0:
src_x, src_y = x, y
else:
force = BLACKHOLE_GRAVITY * 2 * blackhole.radius / dist_sq
src_x = int(x - dx * force)
src_y = int(y - dy * force)
if 0 <= src_x < WIDTH and 0 <= src_y < HEIGHT:
target[y, x] = source[src_y, src_x]
# Draw final warped image
for y in range(HEIGHT):
for x in range(WIDTH):
r, g, b = target[y, x]
if (r, g, b) != (0, 0, 0):
LED.setpixelCanvas(x, y+1, r, g, b)
def draw_clock_overlay_old(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):
global sparks
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_diameter = blackhole.radius * 2
# If asteroid collides with Blackhole, it is destroyed
if dist <= ((obj_radius + blackhole_diameter) / 3) and isinstance(obj, Asteroid) and asteroids_list is not None:
for _ in range(1,20):
angle = random.uniform(0, 2 * math.pi)
speed = random.uniform(SPARK_SPEED_MIN, SPARK_SPEED_MAX * 2)
sparks.append(Spark(obj.x, obj.y, angle, speed,int(obj.size * 2)))
asteroids_list.remove(obj) # Remove asteroid on collision
return
# Apply gravity if no collision
force = BLACKHOLE_GRAVITY * blackhole.radius / dist_sq
# Scale it down for missiles
if isinstance(obj, Missile):
force *= MISSILE_GRAVITY_FORCE
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):
collision_scale = 0.5
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]) * collision_scale
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):
merged_indices = set()
new_asteroids = []
for i in range(len(asteroids)):
if i in merged_indices:
continue
a1 = asteroids[i]
for j in range(i + 1, len(asteroids)):
if j in merged_indices:
continue
a2 = asteroids[j]
if hasattr(a1, 'born_time') and hasattr(a2, 'born_time'):
if time.time() - a1.born_time < 0.5 or time.time() - a2.born_time < 0.5:
continue # Skip merging newly spawned asteroids
dx = a2.x - a1.x
dy = a2.y - a1.y
dist_sq = dx * dx + dy * dy
min_dist = (a1.size + a2.size) * 0.5
if dist_sq < min_dist * min_dist:
if random.random() < ASTEROIDS_MERGE_CHANCE:
total_mass = a1.size + a2.size
new_x = (a1.x * a1.size + a2.x * a2.size) / total_mass
new_y = (a1.y * a1.size + a2.y * a2.size) / total_mass
new_dx = (a1.dx * a1.size + a2.dx * a2.size) / total_mass
new_dy = (a1.dy * a1.size + a2.dy * a2.size) / total_mass
new_size = min(MAX_ASTEROID_SIZE, total_mass)
new_color = a1.color if random.random() < 0.5 else a2.color
#new_asteroids.append(Asteroid(new_x, new_y, size=new_size, color=new_color))
new_a = Asteroid(new_x, new_y, size=new_size, color=new_color)
new_a.growing = False
new_a.size = new_a.target_size
new_asteroids.append(new_a)
merged_indices.update([i, j])
else:
# Fallback to bounce
dist = math.sqrt(dist_sq)
if dist == 0:
continue
nx, ny = dx / dist, dy / dist
tx, ty = -ny, nx
dpTan1 = a1.dx * tx + a1.dy * ty
dpTan2 = a2.dx * tx + a2.dy * ty
dpNorm1 = a1.dx * nx + a1.dy * ny
dpNorm2 = a2.dx * nx + a2.dy * ny
a1.dx = tx * dpTan1 + nx * dpNorm2
a1.dy = ty * dpTan1 + ny * dpNorm2
a2.dx = tx * dpTan2 + nx * dpNorm1
a2.dy = ty * dpTan2 + ny * dpNorm1
overlap = 0.5 * (min_dist - dist + 0.01)
a1.x -= nx * overlap
a1.y -= ny * overlap
a2.x += nx * overlap
a2.y += ny * overlap
# Remove merged ones and add new ones
asteroids[:] = [a for idx, a in enumerate(asteroids) if idx not in merged_indices]
asteroids.extend(new_asteroids)
def handle_collisions_old(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]
import random
import math
import time
# Assuming these constants are defined elsewhere
# WIDTH, HEIGHT, VIEWPORT_X_OFFSET, VIEWPORT_Y_OFFSET, ASTEROID_MIN_SPEED,
# ASTEROID_MAX_SPEED, MAX_ASTEROID_SIZE, ASTEROID_COLOR_OPTIONS,
# ASTEROID_LIGHTING_CONTRAST, LED, GameObject
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.target_size = size if size is not None else random.randint(MIN_ASTEROID_SIZE, 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
self.alive = True
self.born_time = time.time()
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]
# Generate lumps for lumpy shape
self.lumps = []
number_of_lumps = random.randint(3, 6)
for _ in range(number_of_lumps):
angle = random.uniform(0, 2 * math.pi)
distance_frac = random.uniform(0, 0.5)
lump_radius_frac = random.uniform(0.2, 0.5)
frac_dx = math.cos(angle) * distance_frac
frac_dy = math.sin(angle) * distance_frac
self.lumps.append((frac_dx, frac_dy, lump_radius_frac))
@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
# Handle growth
if self.growing:
grow_duration = ASTEROID_GROWTH_DURATION
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))
# Define bounding box, slightly larger to account for protruding lumps
bounding_size = int(self.size * 1.2)
for i in range(-bounding_size, bounding_size + 1):
for j in range(-bounding_size, bounding_size + 1):
max_depth = -1
selected_lump = None
# Check each lump
for lump in self.lumps:
frac_dx, frac_dy, frac_r = lump
effective_dx = frac_dx * self.size
effective_dy = frac_dy * self.size
effective_radius = frac_r * self.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 = lump
if selected_lump:
# Calculate shading using the selected lump
frac_dx, frac_dy, frac_r = selected_lump
effective_dx = frac_dx * self.size
effective_dy = frac_dy * self.size
effective_radius = frac_r * self.size
rel_i = i - effective_dx
rel_j = j - effective_dy
brightness_factor = 1.0 - ASTEROID_LIGHTING_CONTRAST * (rel_i + rel_j) / (2 * effective_radius)
brightness_factor = max(0.5, min(1.5, brightness_factor))
r, g, b = self.color
r_out = min(255, int(r * brightness_factor))
g_out = min(255, int(g * brightness_factor))
b_out = min(255, int(b * brightness_factor))
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):
LED.setpixelCanvas(px - VIEWPORT_X_OFFSET, py - VIEWPORT_Y_OFFSET, r_out, g_out, b_out)
class Asteroid_old(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
self.alive = True
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 = ASTEROID_GROWTH_DURATION
elapsed = time.time() - self.grow_start_time
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
self.trail = [(x, y)] # Initialize trail with current position
def move(self):
self.x += self.dx
self.y += self.dy
# Append current position to trail
self.trail.append((self.x, self.y))
# Keep trail length within MISSILE_TRAIL_LENGTH
if len(self.trail) > MISSILE_TRAIL_LENGTH:
self.trail.pop(0)
def draw(self):
# Draw the missile head
tx = int(self.x)
ty = int(self.y)
if (VIEWPORT_X_OFFSET <= tx < VIEWPORT_X_OFFSET + WIDTH and
VIEWPORT_Y_OFFSET <= ty < VIEWPORT_Y_OFFSET + HEIGHT):
LED.setpixelCanvas(tx - VIEWPORT_X_OFFSET, ty - VIEWPORT_Y_OFFSET, *MISSILE_COLOR)
# Draw the trail using stored positions
for i, (tx, ty) in enumerate(self.trail):
tx, ty = int(tx), int(ty)
if (VIEWPORT_X_OFFSET <= tx < VIEWPORT_X_OFFSET + WIDTH and
VIEWPORT_Y_OFFSET <= ty < VIEWPORT_Y_OFFSET + HEIGHT):
# Fade brightness based on trail position (oldest points dimmer)
#brightness = max(MISSILE_TRAIL_MIN, MISSILE_COLOR[0] - i * (MISSILE_COLOR[0] // MISSILE_TRAIL_LENGTH))
brightness = max(
MISSILE_TRAIL_MIN_BRIGHTNESS,
MISSILE_COLOR[0] - (MISSILE_TRAIL_LENGTH - i - 1) * (MISSILE_COLOR[0] // MISSILE_TRAIL_LENGTH)
)
LED.setpixelCanvas(tx - VIEWPORT_X_OFFSET, ty - VIEWPORT_Y_OFFSET, brightness, brightness, brightness)
# [Previous imports, constants, and other classes remain unchanged]
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):
global missiles
current_thrust = SHIP_THRUST * 2 if blackhole else SHIP_THRUST
current_max_speed = MAX_SPEED * 2 if blackhole else MAX_SPEED
desired_angle = 0
# Handle thrusting timing
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
# --- Avoidance and Targeting Logic ---
avoid_dx = 0
avoid_dy = 0
visible_targets = [a for a in asteroids if (a.x - self.x)**2 + (a.y - self.y)**2 <= SHIP_VISION_RADIUS**2]
if blackhole:
# Flee from black hole with highest priority
dx = self.x - blackhole.x
dy = self.y - blackhole.y
desired_angle = math.atan2(dy, dx)
self.target = None
elif visible_targets:
# Select a target and move toward/attack it
self.target = random.choice(visible_targets)
dx = self.target.x - self.x
dy = self.target.y - self.y
desired_angle = math.atan2(dy, dx)
# Fire missile if aligned or close
distance_sq = dx**2 + dy**2
target_angle = math.atan2(dy, dx)
angle_delta = abs((target_angle - self.angle + math.pi) % (2 * math.pi) - math.pi)
if len(missiles) < MAX_MISSILES and (angle_delta < math.pi / 2 or distance_sq < 25):
missiles.append(Missile(self.x, self.y, self.angle))
# Avoid if too close
if distance_sq < (self.target.size + 2)**2 * 2:
scale = ((self.target.size + 2)**2) / (distance_sq + 1e-5) * 0.5
avoid_dx += dx * scale
avoid_dy += dy * scale
desired_angle = math.atan2(avoid_dy, avoid_dx)
else:
# No targets, move to center of visible screen
self.target = None
cx = VIEWPORT_X_OFFSET + WIDTH / 2
cy = VIEWPORT_Y_OFFSET + HEIGHT / 2
dx = cx - self.x
dy = cy - self.y
desired_angle = math.atan2(dy, dx) if dx != 0 or dy != 0 else self.angle
# Apply rotation
angle_diff = (desired_angle - self.angle + math.pi) % (2 * math.pi) - math.pi
self.angle += max(-SHIP_TURN_RATE, min(SHIP_TURN_RATE, angle_diff))
# Apply thrust
if self.thrusting:
thrust = current_thrust
if blackhole:
thrust *= 4.0 # Stronger thrust when fleeing black hole
elif visible_targets and abs(avoid_dx) > 0.01 or abs(avoid_dy) > 0.01:
thrust *= 2.0 # Moderate thrust when avoiding
ax = math.cos(self.angle) * thrust
ay = math.sin(self.angle) * thrust
self.speed_x += ax
self.speed_y += ay
# Clamp speed
speed = math.hypot(self.speed_x, self.speed_y)
if speed > current_max_speed:
scale = current_max_speed / speed
self.speed_x *= scale
self.speed_y *= scale
# Update position
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 * (2 if blackhole else 1)), 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, length):
self.x = x
self.y = y
self.angle = angle
self.speed = speed
# Clamp the length to a max value
self.length = max(1, min(length, SPARK_TRAIL_MAX_LENGTH)) # <-- clamp to a max of 10
self.lifespan = SPARK_TRAIL_LENGTH #count down timer of how many frames the spark should exist
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(self.length):
tx = int(self.x - math.cos(self.angle) * i)
ty = int(self.y - math.sin(self.angle) * i)
fade = max(32, SPARK_COLOR[0] - i * (SPARK_COLOR[0] // SPARK_TRAIL_LENGTH * 2))
#LED.setpixelCanvas(tx, ty, fade, fade * 3 // 4, fade // 2)
LED.setpixelCanvas(tx - VIEWPORT_X_OFFSET, ty - VIEWPORT_Y_OFFSET, fade, fade * 3 // 4, fade // 2)
missiles = []
ship = Ship()
#asteroids = [Asteroid() for _ in range(ASTEROIDS)]
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 = 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):