-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneon_asteroids.py
More file actions
975 lines (796 loc) · 36.3 KB
/
Copy pathneon_asteroids.py
File metadata and controls
975 lines (796 loc) · 36.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
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
import pygame
import sys
import math
import random
import os
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Neon Asteroids")
BLACK = (10, 10, 15)
WHITE = (255, 255, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
PURPLE = (140, 0, 255)
GREEN = (0, 255, 100)
YELLOW = (255, 255, 0)
RED = (255, 50, 50)
ORANGE = (255, 120, 0)
FPS = 60
clock = pygame.time.Clock()
script_dir = os.path.dirname(os.path.abspath(__file__))
highscore_file = os.path.join(script_dir, "neon_asteroids_highscore.txt")
def load_high_score():
if os.path.exists(highscore_file):
try:
with open(highscore_file, "r") as f:
return int(f.read().strip())
except Exception:
return 0
return 0
def save_high_score(score):
try:
with open(highscore_file, "w") as f:
f.write(str(score))
except Exception:
pass
def draw_neon_line(surf, color, start, end, width=2):
glow_color = [c // 4 for c in color]
pygame.draw.line(surf, glow_color, start, end, width + 4)
pygame.draw.line(surf, color, start, end, width)
pygame.draw.line(surf, WHITE, start, end, max(1, width - 2))
def draw_neon_polygon(surf, color, points, width=2):
if len(points) < 3:
return
glow_color = [c // 4 for c in color]
pygame.draw.polygon(surf, glow_color, points, width + 4)
pygame.draw.polygon(surf, color, points, width)
pygame.draw.polygon(surf, WHITE, points, max(1, width - 2))
def draw_neon_circle(surf, color, center, radius, width=2):
glow_color = [c // 4 for c in color]
pygame.draw.circle(surf, glow_color, center, radius, width + 4 if width > 0 else 0)
pygame.draw.circle(surf, color, center, radius, width)
if width > 0:
pygame.draw.circle(surf, WHITE, center, radius, max(1, width - 2))
def draw_text(surf, text, size, x, y, color=WHITE, center=False, bold=False):
try:
font = pygame.font.SysFont("Arial", size, bold=bold)
except Exception:
font = pygame.font.SysFont(None, size)
text_surf = font.render(text, True, color)
rect = text_surf.get_rect()
if center:
rect.center = (x, y)
else:
rect.topleft = (x, y)
surf.blit(text_surf, rect)
return rect
def wrap_position(pos):
pos.x = pos.x % WIDTH
pos.y = pos.y % HEIGHT
def get_safe_position(ship_pos, min_distance=180):
while True:
x = random.randint(50, WIDTH - 50)
y = random.randint(50, HEIGHT - 50)
pos = pygame.math.Vector2(x, y)
if pos.distance_to(ship_pos) > min_distance:
return pos
class Star:
def __init__(self):
self.pos = pygame.math.Vector2(random.randint(0, WIDTH), random.randint(0, HEIGHT))
self.speed = random.uniform(0.1, 0.4)
self.brightness = random.randint(80, 200)
self.size = random.choice([1, 2]) if self.speed > 0.25 else 1
def update(self, ship_vel):
# Parallax background scrolling relative to ship movement
self.pos -= ship_vel * self.speed * 0.15
self.pos.x = self.pos.x % WIDTH
self.pos.y = self.pos.y % HEIGHT
def draw(self, surf):
color = (self.brightness, self.brightness, self.brightness)
if self.size == 1:
surf.set_at((int(self.pos.x), int(self.pos.y)), color)
else:
pygame.draw.circle(surf, color, (int(self.pos.x), int(self.pos.y)), self.size)
class Particle:
def __init__(self, pos, vel, color, size=3, life=30, decay_size=True):
self.pos = pygame.math.Vector2(pos)
self.vel = pygame.math.Vector2(vel)
self.color = list(color)
self.start_life = life
self.life = life
self.size = size
self.decay_size = decay_size
def update(self):
self.pos += self.vel
self.vel *= 0.95
self.life -= 1
def draw(self, surf):
alpha_ratio = self.life / self.start_life
fade_color = [int(c * alpha_ratio) for c in self.color[:3]]
current_size = self.size
if self.decay_size:
current_size = max(1, int(self.size * alpha_ratio))
pygame.draw.circle(surf, fade_color, (int(self.pos.x), int(self.pos.y)), current_size)
class Bullet:
def __init__(self, pos, angle, speed=12):
self.pos = pygame.math.Vector2(pos)
self.vel = pygame.math.Vector2(speed, 0).rotate(angle)
self.life = 60
self.radius = 3
def update(self):
self.pos += self.vel
wrap_position(self.pos)
self.life -= 1
def draw(self, surf):
draw_neon_circle(surf, YELLOW, (int(self.pos.x), int(self.pos.y)), self.radius, 2)
class EnemyBullet:
def __init__(self, pos, vel_vec):
self.pos = pygame.math.Vector2(pos)
self.vel = vel_vec
self.life = 120
self.radius = 3.5
def update(self):
self.pos += self.vel
wrap_position(self.pos)
self.life -= 1
def draw(self, surf):
draw_neon_circle(surf, RED, (int(self.pos.x), int(self.pos.y)), int(self.radius), 2)
class Ship:
def __init__(self, x, y):
self.pos = pygame.math.Vector2(x, y)
self.vel = pygame.math.Vector2(0, 0)
self.angle = -90
self.radius = 15
self.rotation_speed = 4.5
self.acceleration = 0.16
self.friction = 0.985
self.max_speed = 8.5
self.fire_cooldown = 0
self.base_cooldown = 14
self.lives = 3
self.invulnerable = 120
# Power-up status timers (in frames)
self.shield = 0
self.triple_shot = 0
self.rapid_fire = 0
def reset(self, x, y):
self.pos = pygame.math.Vector2(x, y)
self.vel = pygame.math.Vector2(0, 0)
self.angle = -90
self.invulnerable = 120
def get_points(self):
# Coordinates relative to center, rotated
nose = self.pos + pygame.math.Vector2(self.radius * 1.3, 0).rotate(self.angle)
left_back = self.pos + pygame.math.Vector2(-self.radius, -self.radius * 0.8).rotate(self.angle)
right_back = self.pos + pygame.math.Vector2(-self.radius, self.radius * 0.8).rotate(self.angle)
center_back = self.pos + pygame.math.Vector2(-self.radius * 0.4, 0).rotate(self.angle)
return [nose, left_back, center_back, right_back]
def update(self, keys):
# Rotation
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.angle -= self.rotation_speed
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.angle += self.rotation_speed
# Thrust
is_thrusting = keys[pygame.K_UP] or keys[pygame.K_w]
if is_thrusting:
thrust_force = pygame.math.Vector2(self.acceleration, 0).rotate(self.angle)
self.vel += thrust_force
if self.vel.length() > self.max_speed:
self.vel = self.vel.normalize() * self.max_speed
else:
self.vel *= self.friction
self.pos += self.vel
wrap_position(self.pos)
# Timers decrement
if self.fire_cooldown > 0:
self.fire_cooldown -= 1
if self.invulnerable > 0:
self.invulnerable -= 1
if self.shield > 0:
self.shield -= 1
if self.triple_shot > 0:
self.triple_shot -= 1
if self.rapid_fire > 0:
self.rapid_fire -= 1
return is_thrusting
def shoot(self, bullets):
if self.fire_cooldown > 0:
return False
nose_pos = self.pos + pygame.math.Vector2(self.radius * 1.4, 0).rotate(self.angle)
cooldown = self.base_cooldown // 3 if self.rapid_fire > 0 else self.base_cooldown
self.fire_cooldown = cooldown
if self.triple_shot > 0:
bullets.append(Bullet(nose_pos, self.angle))
bullets.append(Bullet(nose_pos, self.angle - 15))
bullets.append(Bullet(nose_pos, self.angle + 15))
else:
bullets.append(Bullet(nose_pos, self.angle))
return True
def draw(self, surf):
if self.invulnerable > 0 and (self.invulnerable // 6) % 2 == 0:
return
points = self.get_points()
draw_neon_polygon(surf, CYAN, points, 2)
if self.shield > 0:
pulse = math.sin(pygame.time.get_ticks() * 0.01) * 3
shield_r = int(self.radius * 1.8 + pulse)
pygame.draw.circle(surf, (0, 60, 100), (int(self.pos.x), int(self.pos.y)), shield_r, 0)
draw_neon_circle(surf, CYAN, (int(self.pos.x), int(self.pos.y)), shield_r, 2)
class Asteroid:
def __init__(self, pos, size, speed_mult=1.0):
self.pos = pygame.math.Vector2(pos)
self.size = size # 3 = Large, 2 = Medium, 1 = Small
if size == 3:
self.radius = 45
self.color = MAGENTA
base_speed = random.uniform(0.7, 1.4)
self.points = 100
elif size == 2:
self.radius = 24
self.color = PURPLE
base_speed = random.uniform(1.3, 2.1)
self.points = 200
else:
self.radius = 12
self.color = RED
base_speed = random.uniform(2.0, 3.2)
self.points = 500
self.vel = pygame.math.Vector2(random.uniform(-1, 1), random.uniform(-1, 1))
if self.vel.length() == 0:
self.vel = pygame.math.Vector2(1, 0)
self.vel = self.vel.normalize() * base_speed * speed_mult
self.angle = random.uniform(0, 360)
self.rot_speed = random.uniform(-1.8, 1.8)
self.vertices = random.randint(8, 12)
self.vertex_offsets = [random.uniform(0.75, 1.25) for _ in range(self.vertices)]
def update(self):
self.pos += self.vel
self.angle += self.rot_speed
wrap_position(self.pos)
def draw(self, surf):
points = []
for i in range(self.vertices):
v_angle = math.radians(self.angle + (i * 360 / self.vertices))
r = self.radius * self.vertex_offsets[i]
x = self.pos.x + r * math.cos(v_angle)
y = self.pos.y + r * math.sin(v_angle)
points.append(pygame.math.Vector2(x, y))
draw_neon_polygon(surf, self.color, points, 2)
class UFO:
def __init__(self, side):
# side: 0 = left edge, 1 = right edge
self.radius = 16
self.color = GREEN
self.points = 1000
y_spawn = random.randint(100, HEIGHT - 100)
if side == 0:
self.pos = pygame.math.Vector2(-40, y_spawn)
self.vel = pygame.math.Vector2(2.4, 0)
else:
self.pos = pygame.math.Vector2(WIDTH + 40, y_spawn)
self.vel = pygame.math.Vector2(-2.4, 0)
self.wobble_timer = random.uniform(0, 50)
self.fire_cooldown = random.randint(70, 110)
def update(self, player_pos):
self.pos += self.vel
self.wobble_timer += 0.06
self.pos.y += math.sin(self.wobble_timer) * 1.8
self.pos.y = max(40, min(HEIGHT - 40, self.pos.y))
self.fire_cooldown -= 1
if self.fire_cooldown <= 0:
self.fire_cooldown = random.randint(85, 130)
# Find direction to player
to_player = player_pos - self.pos
if to_player.length() > 0:
to_player = to_player.normalize()
else:
to_player = pygame.math.Vector2(0, 1)
to_player = to_player.rotate(random.uniform(-10, 10))
return EnemyBullet(self.pos, to_player * 6.5)
return None
def draw(self, surf):
x, y = int(self.pos.x), int(self.pos.y)
pygame.draw.ellipse(surf, (0, 45, 15), (x - 22, y - 7, 44, 14), 0)
draw_neon_circle(surf, GREEN, (x, y), 8, 2)
pygame.draw.line(surf, GREEN, (x - 22, y), (x + 22, y), 2)
pygame.draw.line(surf, WHITE, (x - 22, y), (x + 22, y), 1)
pygame.draw.arc(surf, GREEN, (x - 12, y - 13, 24, 14), 0, math.pi, 2)
pygame.draw.arc(surf, WHITE, (x - 12, y - 13, 24, 14), 0, math.pi, 1)
pulse = (pygame.time.get_ticks() // 150) % 2
lit_color = YELLOW if pulse == 0 else ORANGE
for offset in [-12, 0, 12]:
pygame.draw.circle(surf, lit_color, (x + offset, y + 4), 2)
class PowerUp:
def __init__(self, pos):
self.pos = pygame.math.Vector2(pos)
self.vel = pygame.math.Vector2(random.uniform(-0.6, 0.6), random.uniform(-0.6, 0.6))
self.radius = 11
self.life = 600 # 10 Seconds
# Decide type: shield (35%), triple (30%), rapid (25%), bomb (10%)
self.type = random.choices(
['shield', 'triple', 'rapid', 'bomb'],
weights=[35, 30, 25, 10],
k=1
)[0]
if self.type == 'shield':
self.color = CYAN
self.char = 'S'
elif self.type == 'triple':
self.color = GREEN
self.char = 'T'
elif self.type == 'rapid':
self.color = YELLOW
self.char = 'R'
else:
self.color = MAGENTA
self.char = 'B'
def update(self):
self.pos += self.vel
wrap_position(self.pos)
self.life -= 1
def draw(self, surf):
pulse = math.sin(pygame.time.get_ticks() * 0.01) * 2.0
r = int(self.radius + pulse)
draw_neon_circle(surf, self.color, (int(self.pos.x), int(self.pos.y)), r, 2)
draw_text(surf, self.char, 13, self.pos.x, self.pos.y, WHITE, center=True, bold=True)
class Button:
def __init__(self, x, y, width, height, text, color, action_val):
self.rect = pygame.Rect(x - width // 2, y - height // 2, width, height)
self.text = text
self.color = color
self.action_val = action_val
self.is_hovered = False
def update(self, mouse_pos):
self.is_hovered = self.rect.collidepoint(mouse_pos)
def draw(self, surf):
border_w = 2
bg_alpha = 15
draw_color = self.color
if self.is_hovered:
border_w = 3
bg_alpha = 45
draw_color = (min(255, self.color[0] + 50), min(255, self.color[1] + 50), min(255, self.color[2] + 50))
bg_surf = pygame.Surface((self.rect.width, self.rect.height), pygame.SRCALPHA)
bg_surf.fill((*draw_color, bg_alpha))
surf.blit(bg_surf, self.rect.topleft)
pygame.draw.rect(surf, [c // 4 for c in draw_color], self.rect, border_w + 2)
pygame.draw.rect(surf, draw_color, self.rect, border_w)
pygame.draw.rect(surf, WHITE, self.rect, max(1, border_w - 1))
draw_text(surf, self.text, 22, self.rect.centerx, self.rect.centery, WHITE, center=True, bold=True)
class GameManager:
STATE_MENU = 0
STATE_PLAYING = 1
STATE_PAUSED = 2
STATE_GAMEOVER = 3
def __init__(self):
self.state = self.STATE_MENU
self.score = 0
self.high_score = load_high_score()
self.wave = 1
self.difficulty = "Medium"
self.difficulty_speed_mult = 1.0
self.stars = [Star() for _ in range(100)]
self.particles = []
self.bullets = []
self.enemy_bullets = []
self.asteroids = []
self.powerups = []
self.ship = Ship(WIDTH // 2, HEIGHT // 2)
self.ufo = None
self.ufo_spawn_timer = 900 # Frame-based delay (~15s)
self.screenshake = 0.0
self.shake_decay = 0.90
self.menu_buttons = [
Button(WIDTH // 2, HEIGHT // 2 - 50, 160, 45, "Easy", CYAN, "Easy"),
Button(WIDTH // 2, HEIGHT // 2 + 10, 160, 45, "Medium", GREEN, "Medium"),
Button(WIDTH // 2, HEIGHT // 2 + 70, 160, 45, "Hard", MAGENTA, "Hard"),
Button(WIDTH // 2, HEIGHT // 2 + 140, 160, 45, "Quit", RED, "Quit")
]
self.paused_buttons = [
Button(WIDTH // 2, HEIGHT // 2 - 20, 180, 45, "Resume", CYAN, "Resume"),
Button(WIDTH // 2, HEIGHT // 2 + 40, 180, 45, "Quit to Menu", RED, "QuitMenu")
]
self.gameover_buttons = [
Button(WIDTH // 2, HEIGHT // 2 + 60, 180, 45, "Play Again", GREEN, "Restart"),
Button(WIDTH // 2, HEIGHT // 2 + 120, 180, 45, "Quit to Menu", RED, "QuitMenu")
]
self.menu_asteroids = [Asteroid((random.randint(0, WIDTH), random.randint(0, HEIGHT)), random.choice([1, 2, 3])) for _ in range(5)]
def trigger_explosion(self, pos, color, count=15, speed_min=1, speed_max=5, life=30, size=3):
for _ in range(count):
angle = random.uniform(0, 360)
speed = random.uniform(speed_min, speed_max)
vel = pygame.math.Vector2(speed, 0).rotate(angle)
self.particles.append(Particle(pos, vel, color, size, life))
def trigger_emp_bomb(self):
self.screenshake = 30.0
retained = []
for a in self.asteroids:
if a.size in [1, 2]:
self.score += int(a.points * 0.5) # 50% points for bomb clear
self.trigger_explosion(a.pos, a.color, count=12, speed_min=2, speed_max=6, life=25, size=2)
else:
retained.append(a)
self.asteroids = retained
if self.ufo:
self.score += 500
self.trigger_explosion(self.ufo.pos, self.ufo.color, count=25, speed_min=3, speed_max=8, life=40, size=4)
self.ufo = None
for angle in range(0, 360, 10):
vel = pygame.math.Vector2(8, 0).rotate(angle)
self.particles.append(Particle(self.ship.pos, vel, MAGENTA, size=4, life=45, decay_size=False))
def start_game(self, diff):
self.state = self.STATE_PLAYING
self.difficulty = diff
self.score = 0
self.wave = 1
if diff == "Easy":
self.difficulty_speed_mult = 0.8
self.ship.lives = 3
num_asteroids = 3
elif diff == "Medium":
self.difficulty_speed_mult = 1.1
self.ship.lives = 3
num_asteroids = 4
else: # Hard
self.difficulty_speed_mult = 1.45
self.ship.lives = 3
num_asteroids = 6
self.ship.reset(WIDTH // 2, HEIGHT // 2)
self.bullets.clear()
self.enemy_bullets.clear()
self.asteroids.clear()
self.powerups.clear()
self.particles.clear()
self.ufo = None
self.ufo_spawn_timer = 900
self.spawn_asteroids_wave(num_asteroids)
def spawn_asteroids_wave(self, count):
for _ in range(count):
safe_pos = get_safe_position(self.ship.pos)
self.asteroids.append(Asteroid(safe_pos, 3, self.difficulty_speed_mult))
def next_wave(self):
self.wave += 1
count = 3 + self.wave
count = min(12, count)
speed_mult = self.difficulty_speed_mult * (1.0 + (self.wave - 1) * 0.08)
self.spawn_asteroids_wave(count)
for i in range(120):
vel = pygame.math.Vector2(random.uniform(2, 6), 0).rotate(random.uniform(0, 360))
self.particles.append(Particle(self.ship.pos, vel, CYAN, size=2, life=40))
def handle_menu_click(self, action):
if action == "Quit":
pygame.quit()
sys.exit()
elif action in ["Easy", "Medium", "Hard"]:
self.start_game(action)
def handle_paused_click(self, action):
if action == "Resume":
self.state = self.STATE_PLAYING
elif action == "QuitMenu":
self.state = self.STATE_MENU
if self.score > self.high_score:
self.high_score = self.score
save_high_score(self.high_score)
def handle_gameover_click(self, action):
if action == "Restart":
self.start_game(self.difficulty)
elif action == "QuitMenu":
self.state = self.STATE_MENU
def run(self):
running = True
while running:
dt = clock.tick(FPS)
mouse_pos = pygame.mouse.get_pos()
mouse_clicked = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_clicked = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_p:
if self.state == self.STATE_PLAYING:
self.state = self.STATE_PAUSED
elif self.state == self.STATE_PAUSED:
self.state = self.STATE_PLAYING
elif event.key == pygame.K_q and self.state in [self.STATE_PAUSED, self.STATE_GAMEOVER]:
self.state = self.STATE_MENU
elif event.key == pygame.K_r and self.state == self.STATE_GAMEOVER:
self.start_game(self.difficulty)
elif event.key == pygame.K_h or event.key == pygame.K_LSHIFT:
if self.state == self.STATE_PLAYING and self.ship.visible:
self.ship.pos = pygame.math.Vector2(random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50))
self.ship.vel = pygame.math.Vector2(0, 0)
self.ship.invulnerable = 60
self.trigger_explosion(self.ship.pos, CYAN, count=20, speed_min=2, speed_max=5)
if self.state == self.STATE_MENU:
for btn in self.menu_buttons:
btn.update(mouse_pos)
if mouse_clicked and btn.is_hovered:
self.handle_menu_click(btn.action_val)
elif self.state == self.STATE_PAUSED:
for btn in self.paused_buttons:
btn.update(mouse_pos)
if mouse_clicked and btn.is_hovered:
self.handle_paused_click(btn.action_val)
elif self.state == self.STATE_GAMEOVER:
for btn in self.gameover_buttons:
btn.update(mouse_pos)
if mouse_clicked and btn.is_hovered:
self.handle_gameover_click(btn.action_val)
if self.state == self.STATE_MENU:
for star in self.stars:
star.update(pygame.math.Vector2(0.5, 0.2)) # Slow drift for menu depth
for a in self.menu_asteroids:
a.update()
for p in self.particles:
p.update()
self.particles = [p for p in self.particles if p.life > 0]
elif self.state == self.STATE_PLAYING:
self.update_gameplay(mouse_clicked)
elif self.state == self.STATE_PAUSED:
pass # Draw freeze frame
elif self.state == self.STATE_GAMEOVER:
for star in self.stars:
star.update(pygame.math.Vector2(0.2, 0.1))
for p in self.particles:
p.update()
self.particles = [p for p in self.particles if p.life > 0]
for a in self.asteroids:
a.update()
self.draw()
pygame.display.flip()
pygame.quit()
sys.exit()
def update_gameplay(self, mouse_clicked):
keys = pygame.key.get_pressed()
is_thrusting = self.ship.update(keys)
if keys[pygame.K_SPACE] or mouse_clicked:
if self.ship.shoot(self.bullets):
# Spawn shooting particles
nose = self.ship.pos + pygame.math.Vector2(self.ship.radius * 1.5, 0).rotate(self.ship.angle)
spark_vel = pygame.math.Vector2(2, 0).rotate(self.ship.angle) + self.ship.vel * 0.5
self.particles.append(Particle(nose, spark_vel, YELLOW, size=2, life=15))
for star in self.stars:
star.update(self.ship.vel)
if is_thrusting and random.random() > 0.2:
exhaust_angle = self.ship.angle + 180 + random.uniform(-18, 18)
exhaust_speed = random.uniform(2.5, 5.0)
exhaust_vel = self.ship.vel + pygame.math.Vector2(exhaust_speed, 0).rotate(exhaust_angle)
exhaust_pos = self.ship.pos + pygame.math.Vector2(-self.ship.radius * 0.9, 0).rotate(self.ship.angle)
self.particles.append(Particle(exhaust_pos, exhaust_vel, ORANGE, size=3, life=20))
if not self.ufo:
self.ufo_spawn_timer -= 1
if self.ufo_spawn_timer <= 0:
side = random.choice([0, 1])
self.ufo = UFO(side)
self.ufo_spawn_timer = random.randint(900, 1500) # Next UFO in 15-25 seconds
else:
enemy_shot = self.ufo.update(self.ship.pos)
if enemy_shot:
self.enemy_bullets.append(enemy_shot)
if self.ufo.pos.x < -60 or self.ufo.pos.x > WIDTH + 60:
self.ufo = None
for a in self.asteroids:
a.update()
for b in self.bullets:
b.update()
for eb in self.enemy_bullets:
eb.update()
for p in self.particles:
p.update()
for pu in self.powerups:
pu.update()
self.bullets = [b for b in self.bullets if b.life > 0]
self.enemy_bullets = [eb for eb in self.enemy_bullets if eb.life > 0]
self.particles = [p for p in self.particles if p.life > 0]
self.powerups = [pu for pu in self.powerups if pu.life > 0]
self.screenshake *= self.shake_decay
if self.screenshake < 0.1:
self.screenshake = 0.0
self.check_collisions()
if not self.asteroids:
self.next_wave()
def check_collisions(self):
for b in self.bullets[:]:
for a in self.asteroids[:]:
dist = b.pos.distance_to(a.pos)
if dist < a.radius + b.radius:
if b in self.bullets:
self.bullets.remove(b)
self.score += a.points
self.screenshake = max(self.screenshake, 7.0 + a.size * 3)
self.trigger_explosion(a.pos, a.color, count=12 + a.size*4, speed_min=1.5, speed_max=5.0)
drop_roll = random.random()
if a.size == 3 and drop_roll < 0.25:
self.powerups.append(PowerUp(a.pos))
elif a.size == 2 and drop_roll < 0.10:
self.powerups.append(PowerUp(a.pos))
if a.size > 1:
# Spawn 2 smaller
self.asteroids.append(Asteroid(a.pos, a.size - 1, self.difficulty_speed_mult))
self.asteroids.append(Asteroid(a.pos, a.size - 1, self.difficulty_speed_mult))
if a in self.asteroids:
self.asteroids.remove(a)
break
if self.ufo:
for b in self.bullets[:]:
if b.pos.distance_to(self.ufo.pos) < self.ufo.radius + b.radius:
if b in self.bullets:
self.bullets.remove(b)
self.score += self.ufo.points
self.screenshake = max(self.screenshake, 18.0)
self.trigger_explosion(self.ufo.pos, self.ufo.color, count=24, speed_min=2.0, speed_max=6.5)
self.powerups.append(PowerUp(self.ufo.pos))
self.ufo = None
break
if self.ship.invulnerable <= 0:
for a in self.asteroids[:]:
dist = self.ship.pos.distance_to(a.pos)
if dist < a.radius + self.ship.radius * 0.8:
self.handle_ship_hit()
# Destroy the asteroid they crashed into to clear path
self.trigger_explosion(a.pos, a.color, count=15, speed_min=1, speed_max=4)
if a in self.asteroids:
self.asteroids.remove(a)
break
if self.ship.invulnerable <= 0:
if self.ufo and self.ship.pos.distance_to(self.ufo.pos) < self.ufo.radius + self.ship.radius:
self.handle_ship_hit()
self.trigger_explosion(self.ufo.pos, self.ufo.color, count=20, speed_min=2, speed_max=6)
self.ufo = None
for eb in self.enemy_bullets[:]:
if self.ship.pos.distance_to(eb.pos) < eb.radius + self.ship.radius:
if eb in self.enemy_bullets:
self.enemy_bullets.remove(eb)
self.handle_ship_hit()
break
for pu in self.powerups[:]:
if self.ship.pos.distance_to(pu.pos) < pu.radius + self.ship.radius:
# Apply powerup
if pu.type == 'shield':
self.ship.shield = 480 # 8 Seconds
elif pu.type == 'triple':
self.ship.triple_shot = 360 # 6 Seconds
elif pu.type == 'rapid':
self.ship.rapid_fire = 480 # 8 Seconds
elif pu.type == 'bomb':
self.trigger_emp_bomb()
# Pick up effect sparks
self.trigger_explosion(pu.pos, pu.color, count=18, speed_min=2.0, speed_max=6.0, life=20, size=2)
# Remove
if pu in self.powerups:
self.powerups.remove(pu)
def handle_ship_hit(self):
if self.ship.shield > 0:
# Shield Absorbs Damage
self.ship.shield = 0
self.ship.invulnerable = 60 # short recovery invulnerability
self.screenshake = max(self.screenshake, 18.0)
self.trigger_explosion(self.ship.pos, CYAN, count=25, speed_min=2, speed_max=5)
else:
# Ship Destroyed
self.ship.lives -= 1
self.screenshake = max(self.screenshake, 32.0)
# Massive ship ring explosion
self.trigger_explosion(self.ship.pos, CYAN, count=20, speed_min=1.0, speed_max=5.0, life=40, size=3)
self.trigger_explosion(self.ship.pos, ORANGE, count=20, speed_min=1.5, speed_max=6.5, life=35, size=4)
self.trigger_explosion(self.ship.pos, WHITE, count=15, speed_min=2.0, speed_max=8.0, life=25, size=2)
if self.ship.lives <= 0:
self.state = self.STATE_GAMEOVER
if self.score > self.high_score:
self.high_score = self.score
save_high_score(self.high_score)
else:
self.ship.reset(WIDTH // 2, HEIGHT // 2)
def draw(self):
# Create game rendering canvas buffer
canvas = pygame.Surface((WIDTH, HEIGHT))
canvas.fill(BLACK)
# 1. Background stars
for star in self.stars:
star.draw(canvas)
# 2. Debris particles
for p in self.particles:
p.draw(canvas)
# 3. Power-ups
for pu in self.powerups:
pu.draw(canvas)
# 4. Asteroids
for a in self.asteroids:
a.draw(canvas)
# 5. Player Bullets
for b in self.bullets:
b.draw(canvas)
# 6. UFO
if self.ufo:
self.ufo.draw(canvas)
# 7. Enemy Bullets
for eb in self.enemy_bullets:
eb.draw(canvas)
# 8. Ship
if self.state == self.STATE_PLAYING or self.state == self.STATE_PAUSED:
self.ship.draw(canvas)
# State Specific HUD Overlay / Text
if self.state == self.STATE_MENU:
# Draw decorative floating menu asteroids
for a in self.menu_asteroids:
a.draw(canvas)
# Giant Title with pulsing core
pulse = math.sin(pygame.time.get_ticks() * 0.005) * 4
title_size = int(62 + pulse)
# Layered neon shadow title
draw_text(canvas, "NEON ASTEROIDS", title_size + 4, WIDTH // 2, HEIGHT // 4 - 30, color=(0, 60, 60), center=True, bold=True)
draw_text(canvas, "NEON ASTEROIDS", title_size, WIDTH // 2, HEIGHT // 4 - 30, color=CYAN, center=True, bold=True)
draw_text(canvas, "NEON ASTEROIDS", title_size - 4, WIDTH // 2, HEIGHT // 4 - 30, color=WHITE, center=True, bold=True)
draw_text(canvas, f"HIGH SCORE: {self.high_score}", 20, WIDTH // 2, HEIGHT // 4 + 30, color=YELLOW, center=True, bold=True)
# Draw Main Menu buttons
for btn in self.menu_buttons:
btn.draw(canvas)
# Instructions Footer
draw_text(canvas, "Controls: WASD/Arrows to Drive | Space/Mouse Click to Shoot | LShift/H to Warp", 15, WIDTH // 2, HEIGHT - 30, color=(150, 150, 180), center=True)
elif self.state == self.STATE_PLAYING:
# Draw standard HUD
draw_text(canvas, f"SCORE: {self.score}", 22, 20, 20, color=WHITE, bold=True)
draw_text(canvas, f"WAVE: {self.wave}", 22, WIDTH // 2, 20, color=WHITE, center=True, bold=True)
# Draw lives
for i in range(self.ship.lives):
x = 24 + i * 22
y = 54
# Small ship outline
pts = [
pygame.math.Vector2(x, y - 8),
pygame.math.Vector2(x - 6, y + 6),
pygame.math.Vector2(x - 2, y + 3),
pygame.math.Vector2(x + 2, y + 3),
pygame.math.Vector2(x + 6, y + 6)
]
draw_neon_polygon(canvas, CYAN, pts, 1)
# Draw Active Power-ups Bar
py = 20
if self.ship.shield > 0:
duration_sec = self.ship.shield / 60
draw_text(canvas, f"SHIELD: {duration_sec:.1f}s", 16, WIDTH - 160, py, color=CYAN, bold=True)
py += 22
if self.ship.triple_shot > 0:
duration_sec = self.ship.triple_shot / 60
draw_text(canvas, f"TRIPLE SHOT: {duration_sec:.1f}s", 16, WIDTH - 160, py, color=GREEN, bold=True)
py += 22
if self.ship.rapid_fire > 0:
duration_sec = self.ship.rapid_fire / 60
draw_text(canvas, f"RAPID FIRE: {duration_sec:.1f}s", 16, WIDTH - 160, py, color=YELLOW, bold=True)
elif self.state == self.STATE_PAUSED:
# Dark freeze overlay
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 160))
canvas.blit(overlay, (0, 0))
draw_text(canvas, "PAUSED", 48, WIDTH // 2, HEIGHT // 3, color=WHITE, center=True, bold=True)
for btn in self.paused_buttons:
btn.draw(canvas)
elif self.state == self.STATE_GAMEOVER:
# Dark freeze overlay
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 190))
canvas.blit(overlay, (0, 0))
draw_text(canvas, "GAME OVER", 54, WIDTH // 2, HEIGHT // 4, color=RED, center=True, bold=True)
draw_text(canvas, f"FINAL SCORE: {self.score}", 24, WIDTH // 2, HEIGHT // 3 + 10, color=WHITE, center=True, bold=True)
if self.score >= self.high_score and self.score > 0:
pulse = math.sin(pygame.time.get_ticks() * 0.01) * 3
draw_text(canvas, "NEW HIGH SCORE!", int(22 + pulse), WIDTH // 2, HEIGHT // 3 + 50, color=YELLOW, center=True, bold=True)
for btn in self.gameover_buttons:
btn.draw(canvas)
# Apply Screen Shake offset onto final display blit
shake_offset_x = 0
shake_offset_y = 0
if self.screenshake > 0.0:
shake_offset_x = random.randint(-int(self.screenshake), int(self.screenshake))
shake_offset_y = random.randint(-int(self.screenshake), int(self.screenshake))
screen.fill(BLACK)
screen.blit(canvas, (shake_offset_x, shake_offset_y))
# Executable entry point
if __name__ == "__main__":
game = GameManager()
game.run()