-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlien Invaders The Sequel to The Prequel to The Original Sequel Continuation Remastered Enhanced Edition.py
More file actions
1807 lines (1604 loc) · 78.4 KB
/
Alien Invaders The Sequel to The Prequel to The Original Sequel Continuation Remastered Enhanced Edition.py
File metadata and controls
1807 lines (1604 loc) · 78.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
def draw_boss_healthbar(surf, x, y, w, h, hp, hp_max):
# transparent background
bg = pygame.Surface((w, h), pygame.SRCALPHA)
bg.fill((0,0,0,120))
surf.blit(bg, (x, y))
# hp bar
fill = int((hp/hp_max) * (w-4))
pygame.draw.rect(surf, RED, (x+2, y+2, fill, h-4))
pygame.draw.rect(surf, WHITE, (x, y, w, h), 2)
"""
Alien Invaders The Sequel to The Prequel to The Original Sequel Continuation Remastered Enhanced Edition (with hit-flash + knockback)
- Keeps mouse-drag + keyboard movement (unchanged from your request)
- Shield pickup gives 3 hits
- Prevents enemies spawning on top of the player (uses player position when spawning)
- Repositions dynamic enemies that fly off-screen to the top instead of deleting them
- Collision removed fuck that shit
- Clean, ready-to-run single-file script
Run: python alien_invaders_fixed_knockback.py
Requires: pygame (pip install pygame)
"""
import pygame
import random
import math
import sys
import time
# --------------------
# Configuration
# --------------------
WIDTH, HEIGHT = 880, 720
FPS = 60
# Player
PLAYER_START_X = WIDTH // 2
PLAYER_Y = HEIGHT - 70
PLAYER_RADIUS = 18
PLAYER_BASE_HP = 6
PLAYER_BASE_FIRE_DELAY_MS = 220
PLAYER_SPEED = 260
# Bullets
PLAYER_BULLET_SPEED = -10
ENEMY_BULLET_SPEED_BASE = 3.0
# Buffs
BUFF_CHANCE = 0.18
BUFF_DURATION_MS = 5000
# Shield duration (ms)
SHIELD_DURATION_MS = 5000
# Ultimate
ULTIMATE_DURATION_MS = 8000
ULTIMATE_FIRE_DELAY_MS = 180
# Waves
ENEMIES_PER_ROW = 10
BOSS_EVERY = 5
ENEMY_DROP = 24
# Shop prices
SHOP_FIRE_RATE_PRICE = 800
SHOP_MAX_HP_PRICE = 1000
SHOP_SPEED_PRICE = 700
# Particles
PARTICLE_COUNT = 14
# Knockback (light = 15 px as requested)
KNOCKBACK_PIXELS = 15
# Colors
WHITE = (255,255,255)
BLACK = (10,10,18)
RED = (230,60,60)
GREEN = (80,230,120)
YELLOW = (255,220,50)
CYAN = (80,200,235)
ORANGE = (255,150,60)
GRAY = (120,120,140)
SHIELD_BLUE = (90,180,240)
FONT_NAME = "Consolas"
# --------------------
# Utility
# --------------------
def clamp(v, a, b):
return max(a, min(b, v))
# --------------------
# Background (stars & planets)
# --------------------
class Star:
def __init__(self, w, h):
self.x = random.uniform(0, w)
self.y = random.uniform(0, h)
self.speed = random.uniform(0.6, 2.2)
self.size = random.randint(1, 3)
self.phase = random.uniform(0, math.pi*2)
def update(self, dt, h):
self.y += self.speed * dt
self.phase += dt * 0.1
if self.y > h:
self.y = -2
self.x = random.uniform(0, WIDTH)
def draw(self, surf):
alpha = 150 + int(100 * math.sin(self.phase))
color = (alpha, alpha, alpha)
pygame.draw.circle(surf, color, (int(self.x), int(self.y)), self.size)
class Planet:
def __init__(self, w, h):
self.x = random.uniform(80, w - 80)
self.y = random.uniform(-2000, -300)
self.speed = random.uniform(0.02, 0.12)
self.size = random.randint(40, 120)
self.color = (
random.randint(80, 230),
random.randint(80, 230),
random.randint(80, 230)
)
self.angle = random.uniform(0, math.pi*2)
self.spin = random.uniform(-0.6, 0.6)
def update(self, dt, h):
self.y += self.speed * dt
self.angle += self.spin * dt * 0.02
if self.y > h + 200:
self.x = random.uniform(80, WIDTH - 80)
self.y = random.uniform(-1200, -300)
self.speed = random.uniform(0.02, 0.12)
self.size = random.randint(40, 120)
self.color = (
random.randint(80, 230),
random.randint(80, 230),
random.randint(80, 230)
)
def draw(self, surf):
cx = int(self.x); cy = int(self.y)
pygame.draw.circle(surf, self.color, (cx, cy), self.size)
highlight_color = (min(255, self.color[0]+30), min(255, self.color[1]+30), min(255, self.color[2]+30))
hx = cx + int(self.size * 0.25 * math.cos(self.angle))
hy = cy - int(self.size * 0.25 * math.sin(self.angle))
pygame.draw.circle(surf, highlight_color, (hx, hy), int(self.size * 0.22))
stars = [Star(WIDTH, HEIGHT) for _ in range(140)]
planets = [Planet(WIDTH, HEIGHT) for _ in range(3)]
# --------------------
# Visual globe for menu
# --------------------
def draw_spinning_globe(surf, cx, cy, radius, angle):
pygame.draw.circle(surf, (12,60,110), (cx, cy), radius)
pygame.draw.circle(surf, (24,120,200), (cx, cy), radius, 2)
rect = pygame.Rect(cx - radius, cy - radius, radius*2, radius*2)
for i in range(6):
a = angle + i * 0.9
pygame.draw.arc(surf, (80,170,220), rect, a, a + math.pi, 2)
for j in range(-2,3):
w = 2 if j==0 else 1
ry = int(cy + j * (radius * 0.35))
pygame.draw.ellipse(surf, (80,160,200), (cx - int(radius*0.85), ry - 6, int(radius*1.7), 12), w)
for k in range(12):
theta = angle * 1.6 + k * (2*math.pi/12)
r = radius * 0.65
x = int(cx + math.cos(theta) * r)
y = int(cy + math.sin(theta) * r * 0.5)
pygame.draw.circle(surf, (200,230,255), (x, y), 2)
# --------------------
# Entities
# --------------------
class Particle:
def __init__(self, x, y, color, life=0.8):
self.x = x; self.y = y
self.vx = random.uniform(-3, 3)
self.vy = random.uniform(-6, -1)
self.color = color
self.life = life; self.time = 0
def update(self, dt):
self.time += dt; self.x += self.vx; self.y += self.vy; self.vy += 10 * dt
def draw(self, surf):
alpha = clamp(1 - (self.time / self.life), 0, 1)
if alpha <= 0: return
r = int(3 * (1 - alpha) + 1)
s = pygame.Surface((r*2, r*2), pygame.SRCALPHA)
col = (*self.color, int(255 * alpha))
pygame.draw.circle(s, col, (r, r), r); surf.blit(s, (self.x - r, self.y - r))
class Explosion:
def __init__(self, x, y, color=ORANGE, num=PARTICLE_COUNT):
self.particles = [Particle(x + random.uniform(-6,6), y + random.uniform(-6,6), color, life=random.uniform(0.5,1.1)) for _ in range(num)]
def update(self, dt):
for p in self.particles: p.update(dt)
self.particles = [p for p in self.particles if p.time < p.life]
def draw(self, surf):
for p in self.particles: p.draw(surf)
class Bullet:
def __init__(self, x, y, vy, color=YELLOW, owner="player", damage=1):
self.x = x; self.y = y; self.vy = vy; self.color = color; self.owner = owner; self.damage = damage
self.radius = 4 if owner=="player" else 5
self.rect = pygame.Rect(x-self.radius, y-self.radius, self.radius*2, self.radius*2)
def update(self, dt):
self.y += self.vy * (dt * 60 if dt < 5 else dt)
self.rect.center = (int(self.x), int(self.y))
def draw(self, surf): pygame.draw.circle(surf, self.color, (int(self.x), int(self.y)), self.radius)
def offscreen(self): return self.y < -30 or self.y > HEIGHT + 30
class BuffDrop:
def __init__(self, x, y, kind='multishot'):
self.x = x; self.y = y; self.kind = kind; self.vy = 2.2; self.radius = 10
self.rect = pygame.Rect(x - self.radius, y - self.radius, self.radius*2, self.radius*2)
self.angle = 0
def update(self, dt):
self.y += self.vy * (dt * 60 if dt < 5 else dt); self.angle += dt * 5; self.rect.center = (int(self.x), int(self.y))
def draw(self, surf):
x = int(self.x); y = int(self.y)
if self.kind == 'multishot':
pygame.draw.circle(surf, YELLOW, (x, y), self.radius)
ex = int(self.x + self.radius * math.cos(self.angle)); ey = int(self.y + self.radius * math.sin(self.angle))
pygame.draw.line(surf, WHITE, (self.x, self.y), (ex, ey), 2)
elif self.kind == 'shield':
pygame.draw.circle(surf, SHIELD_BLUE, (x, y), self.radius)
ex = int(self.x + self.radius * math.cos(self.angle)); ey = int(self.y + self.radius * math.sin(self.angle))
pygame.draw.line(surf, WHITE, (self.x, self.y), (ex, ey), 2)
else: # heal / hp drop
pygame.draw.circle(surf, RED, (x, y), self.radius)
ex = int(self.x + self.radius * math.cos(self.angle))
ey = int(self.y + self.radius * math.sin(self.angle))
pygame.draw.line(surf, WHITE, (self.x, self.y), (ex, ey), 2)
# --------------------
# Player
# --------------------
class Player:
def __init__(self):
self.x = PLAYER_START_X; self.y = PLAYER_Y; self.radius = PLAYER_RADIUS
self.hp_max = PLAYER_BASE_HP; self.hp = PLAYER_BASE_HP
self.fire_delay_ms = PLAYER_BASE_FIRE_DELAY_MS; self.last_shot_time = 0
self.multishot_active = False; self.multishot_end_time = 0
self.score = 0; self.lives = 1
self.speed = PLAYER_SPEED
# shield: absorbs multiple hits while active
self.shield_active = False
self.shield_end_time = 0
self.shield_uses = 0 # time when shield effect ends
# hit-flash indicator (visual only)
self.hit_flash = False
self.hit_flash_end = 0
# micro invincibility timer
self.invincible_until = 0
# ultimate
self.ultimate_count = 0; self.ultimate_needed = 10; self.ultimate_available = False
self.ultimate_active = False; self.ultimate_end_time = 0; self.ultimate_fire_delay_ms = ULTIMATE_FIRE_DELAY_MS; self.last_ultimate_shot_time = 0
def update(self):
now = pygame.time.get_ticks()
# Shield expiration: ensure shield ends when timer passes
if getattr(self, 'shield_active', False) and getattr(self, 'shield_end_time', 0) and now > self.shield_end_time:
self.shield_active = False
self.shield_end_time = 0
self.shield_uses = 0
if hasattr(self, 'shield_uses'):
self.shield_uses = 0
if self.shield_active and now > self.shield_end_time:
self.shield_active = False
self.shield_end_time = 0
self.shield_uses = 0
if self.multishot_active and now > self.multishot_end_time:
self.multishot_active = False
if self.ultimate_active and now > self.ultimate_end_time:
self.ultimate_active = False; self.ultimate_end_time = 0; self.last_ultimate_shot_time = 0
def draw(self, surf):
x, y = int(self.x), int(self.y)
# flicker frames (visual only, no invincibility)
if self.hit_flash:
if pygame.time.get_ticks() > self.hit_flash_end:
self.hit_flash = False
else:
# simple flicker: skip every other frame
if (pygame.time.get_ticks() // 60) % 2 == 0:
return # skip drawing this frame for flicker
# normal draw
pts = [
(x, y - self.radius),
(x - self.radius, y + self.radius),
(x + self.radius, y + self.radius)
]
pygame.draw.polygon(surf, CYAN, pts)
pygame.draw.polygon(surf, WHITE, pts, 2)
# shield visual
if self.shield_active and self.shield_uses > 0:
pygame.draw.circle(surf, SHIELD_BLUE, (x,y), self.radius + 10, 3)
def can_shoot(self):
return pygame.time.get_ticks() - self.last_shot_time >= self.fire_delay_ms
def shoot(self):
self.last_shot_time = pygame.time.get_ticks(); bullets = []
if self.multishot_active:
for s in (-14,0,14): bullets.append(Bullet(self.x + s, self.y - self.radius - 4, PLAYER_BULLET_SPEED, YELLOW, "player", damage=1))
else:
bullets.append(Bullet(self.x, self.y - self.radius - 6, PLAYER_BULLET_SPEED, YELLOW, "player", damage=1))
return bullets
def take_damage(self, amount=1, source=None, knockback=0):
"""
amount: HP to remove
source: (sx, sy) tuple indicating position of hit origin (bullet or enemy)
knockback: pixels to push player away from source (visual reaction)
Returns True if player died
"""
# shield absorbs up to its uses
if self.shield_active and self.shield_uses > 0:
self.shield_uses -= amount
if self.shield_uses <= 0:
self.shield_active = False; self.shield_uses = 0
# trigger flash and knockback even if shield absorbs
self.hit_flash = True
self.hit_flash_end = pygame.time.get_ticks() + 180
if source and knockback:
sx, sy = source
dx = self.x - sx; dy = self.y - sy
dist = math.hypot(dx, dy)
if dist == 0:
# random small nudge if overlapping exactly
angle = random.uniform(0, math.pi*2)
dx = math.cos(angle); dy = math.sin(angle); dist = 1.0
nx = dx / dist; ny = dy / dist
self.x += nx * knockback
self.y += ny * knockback
self.x = clamp(self.x, self.radius, WIDTH - self.radius)
self.y = clamp(self.y, self.radius, HEIGHT - self.radius)
return False
# normal damage
self.hp -= amount
# flash feedback (NO invincibility)
self.hit_flash = True
self.hit_flash_end = pygame.time.get_ticks() + 180
# apply knockback if source provided
if source and knockback:
sx, sy = source
dx = self.x - sx; dy = self.y - sy
dist = math.hypot(dx, dy)
if dist == 0:
angle = random.uniform(0, math.pi*2)
dx = math.cos(angle); dy = math.sin(angle); dist = 1.0
nx = dx / dist; ny = dy / dist
self.x += nx * knockback
self.y += ny * knockback
self.x = clamp(self.x, self.radius, WIDTH - self.radius)
self.y = clamp(self.y, self.radius, HEIGHT - self.radius)
if self.hp <= 0:
self.hp = 0; return True
return False
def use_ultimate_once(self):
bullets = []
offsets = [-40, -20, 0, 20, 40]
for s in offsets:
bullets.append(Bullet(self.x + s, self.y - self.radius - 6, int(PLAYER_BULLET_SPEED * 1.6), YELLOW, "player", damage=2))
return bullets
# --------------------
# Enemy
# --------------------
class Enemy:
def __init__(self, x, y, etype="basic", hp=1):
self.base_x = x; self.base_y = y; self.x = x; self.y = y
self.etype = etype
self.w = 36; self.h = 30; self.hp = hp
self.hp_max = hp; self.alive = True; self.arm_state = 0
self.shoot_timer = random.uniform(0.4, 2.4); self.osc_phase = random.uniform(0, math.pi*2)
self.spin_radius = random.uniform(4, 18)
self.spin_speed = random.uniform(1.0, 3.0) * (0.6 if etype=="tank" else 1.0)
self.bob_amp = random.uniform(0.0, 4.0)
# physics for dynamic spawn
self.vx = None; self.vy = None; self.curve_phase = 0.0; self.curve_speed = 0.0; self.curve_amount = 0.0
def rect(self):
return pygame.Rect(int(self.x - self.w/2), int(self.y - self.h/2), self.w, self.h)
def toggle_arm(self): self.arm_state = 1 - self.arm_state
def update(self, step_dx, step_dy, dt, wave, elapsed):
if not self.alive: return
# dynamic (free-moving) enemies
if self.vx is not None and self.vy is not None:
self.curve_phase += self.curve_speed * dt
cx = math.sin(self.curve_phase) * self.curve_amount * (0.5 if self.etype == 'fast' else 1.0)
self.x += (self.vx * (dt * 60))
self.y += (self.vy * (dt * 60))
self.x += cx * dt * 18
if self.etype == "zig":
self.y += math.sin(elapsed*2 + self.osc_phase) * 0.8
# if enemy goes far off screen, reposition them back to top (respawn behavior)
if self.x < -200 or self.x > WIDTH + 200 or self.y > HEIGHT + 200:
# reposition to top with new parameters (keeps game fun)
self.x = random.randint(40, WIDTH - 40)
self.y = -40
self.vx = random.uniform(-0.6, 0.6)
self.vy = random.uniform(1.2, 2.5)
self.curve_phase = random.uniform(0, math.pi*2)
self.curve_speed = random.uniform(0.4, 1.2)
self.curve_amount = random.randint(8, 38)
base_rate = 1.0
if self.etype == "fast": base_rate = 0.5
elif self.etype == "tank": base_rate = 1.6
self.shoot_timer -= dt * (1.0 / base_rate)
return
# formation style
self.base_x += step_dx; self.base_y += step_dy
if self.etype == "zig":
bob = math.sin(elapsed*2 + self.osc_phase) * 6
elif self.etype == "tank":
bob = math.sin(elapsed*1.2 + self.osc_phase) * 3
else:
bob = math.sin(elapsed*1.6 + self.osc_phase) * self.bob_amp
angle = elapsed * self.spin_speed + self.osc_phase
self.x = self.base_x + math.cos(angle) * self.spin_radius
self.y = self.base_y + math.sin(angle) * (self.spin_radius * 0.6) + bob
base_rate = 1.0
if self.etype == "fast": base_rate = 0.5
elif self.etype == "tank": base_rate = 1.6
self.shoot_timer -= dt * (1.0 / base_rate)
def draw(self, surf):
if not self.alive: return
px = int(self.x); py = int(self.y)
frame1 = [" ██ ", " █ █ ", "██████", "█ ██ █", "█ █", " █ █ "]
frame2 = [" ██ ", " █ █ ", "██████", "█ ██ █", " █ █ ", "█ █"]
frame = frame1 if self.arm_state==0 else frame2
for ry, row in enumerate(frame):
for rx, ch in enumerate(row):
if ch == "█":
x = px + (rx - 3) * 3; y = py + (ry - 3) * 3
pygame.draw.rect(surf, GREEN, (x, y, 3, 3))
class Boss(Enemy):
def __init__(self, x, y, hp=18):
super().__init__(x,y,etype="boss", hp=hp)
self.w = 120; self.h = 70; self.move_timer = 0; self.dir = 1; self.shoot_timer = 1.2
def draw(self, surf):
if not self.alive: return
rect = pygame.Rect(self.x - self.w/2, self.y - self.h/2, self.w, self.h)
pygame.draw.rect(surf, (120,50,200), rect); pygame.draw.rect(surf, WHITE, rect, 3)
draw_boss_healthbar(surf, int(self.x-60), int(self.y-self.h//2-20), 120, 14, self.hp, self.hp_max)
pygame.draw.circle(surf, BLACK, (int(self.x - 24), int(self.y - 8)), 8)
pygame.draw.circle(surf, BLACK, (int(self.x + 24), int(self.y - 8)), 8)
for i in range(-3,4): tx = int(self.x + i*10); ty = int(self.y + 16); pygame.draw.rect(surf, WHITE, (tx-3, ty, 6, 8))
# --------------------
# Wave Manager (now accepts player_pos to avoid spawning on player)
# --------------------
class WaveManager:
def __init__(self, player_pos=None):
self.wave_num = 0
self.enemies = []
self.step_interval = 0.8
self.step_acc = 0.0
self.direction = 1
self.elapsed = 0.0
self.base_speed = 28.0
self.enemy_shoot_prob = 0.006
self.spawn_wave(player_pos)
def spawn_wave(self, player_pos=None):
self.wave_num += 1
self.enemies = [] # Reset enemies each wave
total = 6 + self.wave_num * 1
formation_count = max(0, min(ENEMIES_PER_ROW, total // 3))
dynamic_count = total - formation_count
# Formation spawn
if formation_count > 0:
start_x = max(80, WIDTH // 2 - (formation_count // 2) * 50)
y = 90
for i in range(formation_count):
x = start_x + i * 50
t = random.random()
if t < 0.62:
etype = 'basic'
hp = 1
elif t < 0.82:
etype = 'fast'
hp = 1
elif t < 0.94:
etype = 'zig'
hp = 1
else:
etype = 'tank'
hp = 2 + (self.wave_num // 3)
# Create enemy and add to the list
e = Enemy(x, y, etype=etype, hp=hp)
if etype == 'tank':
e.spin_radius = random.uniform(8, 22)
e.spin_speed *= 0.7
if etype == 'fast':
e.spin_radius = random.uniform(2, 8)
e.spin_speed *= 1.6
self.enemies.append(e)
# Dynamic spawn
for i in range(dynamic_count):
etype = random.choice(['basic', 'fast', 'zig', 'tank'])
side = random.choice(['top', 'left', 'right'])
placed = False
attempts = 0
while not placed and attempts < 40:
attempts += 1
# Dynamic spawn logic based on side
if side == 'top':
x = random.randint(40, WIDTH - 40)
y = -40
vx = random.uniform(-0.6, 0.6)
vy = random.uniform(1.2, 2.5)
elif side == 'left':
x = -40
y = random.randint(60, HEIGHT - 220)
vx = random.uniform(1.0, 2.6)
vy = random.uniform(-0.2, 0.4)
else:
x = WIDTH + 40
y = random.randint(60, HEIGHT - 220)
vx = random.uniform(-2.6, -1.0)
vy = random.uniform(-0.2, 0.4)
# Avoid spawning too close to the player
if player_pos is None or math.hypot(x - player_pos[0], y - player_pos[1]) > 140:
placed = True
# Add enemy to list
e = Enemy(x, y, etype=etype, hp=(2 if etype == 'tank' else 1))
e.vx = vx
e.vy = vy
e.curve_phase = random.uniform(0, math.pi * 2)
e.curve_speed = random.uniform(0.4, 1.2)
e.curve_amount = random.randint(8, 38)
if etype == 'fast':
e.vx *= 1.2
e.vy *= 1.1
e.curve_amount *= 0.6
if etype == 'tank':
e.vx *= 0.6
e.vy *= 0.75
e.curve_amount *= 1.1
self.enemies.append(e)
# Boss waves
if self.wave_num % BOSS_EVERY == 0:
boss_y = min(140, PLAYER_Y - 220)
boss_hp = 20 + int(self.wave_num * 3.5)
boss = Boss(WIDTH // 2, boss_y, hp=boss_hp)
self.enemies.append(boss)
# Print debug message for the wave and enemies spawned
print(f"Spawning wave {self.wave_num} with {len(self.enemies)} enemies.")
# Adjust difficulty for next wave
self.step_interval = max(0.95 - (self.wave_num * 0.02), 0.35)
self.base_speed = 20 + self.wave_num * 2.2
self.enemy_shoot_prob = clamp(0.004 + self.wave_num * 0.0009, 0.004, 0.02)
def update(self, dt):
self.elapsed += dt
self.step_acc += dt
if self.step_acc >= self.step_interval:
times = int(self.step_acc // self.step_interval)
self.step_acc -= times * self.step_interval
step_size = self.base_speed * self.step_interval
for _ in range(times):
dx = step_size * self.direction
will_hit = False
for e in self.enemies:
if not e.alive: continue
if getattr(e, 'vx', None) is not None: continue
nx = e.base_x + dx
if nx - e.w / 2 < 20 or nx + e.w / 2 > WIDTH - 20:
will_hit = True
break
if will_hit:
for e in self.enemies:
if e.alive and getattr(e, 'vx', None) is None:
e.base_y += ENEMY_DROP
e.toggle_arm()
self.direction *= -1
else:
for e in self.enemies:
if e.alive and getattr(e, 'vx', None) is None:
e.base_x += dx
e.toggle_arm()
def any_alive(self):
return any(e.alive for e in self.enemies)
# --------------------
# Shop
# --------------------
class Shop:
def __init__(self): pass
def open(self, screen, player):
font = pygame.font.SysFont(FONT_NAME, 24); big = pygame.font.SysFont(FONT_NAME, 40)
options = [("Faster Fire (reduce delay by 20ms)", SHOP_FIRE_RATE_PRICE, "fire"), ("Increase Max HP (+1)", SHOP_MAX_HP_PRICE, "hp"), ("Heal to Full (+heal)", 1300, "heal")]
selected = 0; clock = pygame.time.Clock()
while True:
clock.tick(FPS)
for ev in pygame.event.get():
if ev.type == pygame.QUIT: pygame.quit(); sys.exit()
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_t: return
if ev.key == pygame.K_UP: selected = (selected - 1) % len(options)
if ev.key == pygame.K_DOWN: selected = (selected + 1) % len(options)
if ev.key == pygame.K_RETURN:
_, cost, code = options[selected]
if player.score >= cost:
player.score -= cost
if code == "fire": """
Alien Invaders — Fixed Edition (with hit-flash + knockback + 15s invincibility on hit)
- Keeps mouse-drag + keyboard movement (unchanged)
- Shield pickup gives 3 hits
- Prevents enemies spawning on top of the player (uses player position when spawning)
- Repositions dynamic enemies that fly off-screen to the top instead of deleting them
- Collision: enemy body hit deals 1 HP and grants 15s temporary invincibility
- Clean, ready-to-run single-file script
Run: python alien_invaders_fixed_knockback_invincible.py
Requires: pygame (pip install pygame)
"""
import pygame
import random
import math
import sys
import time
# Optional sound: uses numpy to synthesize simple waveforms.
try:
import numpy as np
NUMPY_AVAILABLE = True
except Exception:
NUMPY_AVAILABLE = False
pygame.init()
if NUMPY_AVAILABLE:
try:
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512)
except Exception:
pass
# --------------------
# Configuration
# --------------------
WIDTH, HEIGHT = 880, 720
FPS = 60
# Player
PLAYER_START_X = WIDTH // 2
PLAYER_Y = HEIGHT - 70
PLAYER_RADIUS = 18
PLAYER_BASE_HP = 6
PLAYER_BASE_FIRE_DELAY_MS = 220
PLAYER_SPEED = 260
# Bullets
PLAYER_BULLET_SPEED = -10
ENEMY_BULLET_SPEED_BASE = 3.0
# Buffs
BUFF_CHANCE = 0.18
BUFF_DURATION_MS = 5000
# Ultimate
ULTIMATE_DURATION_MS = 8000
ULTIMATE_FIRE_DELAY_MS = 180
# Waves
ENEMIES_PER_ROW = 10
BOSS_EVERY = 5
ENEMY_DROP = 24
# Shop prices
SHOP_FIRE_RATE_PRICE = 800
SHOP_MAX_HP_PRICE = 1000
SHOP_SPEED_PRICE = 700
# Particles
PARTICLE_COUNT = 14
# Knockback (light = 15 px as requested)
KNOCKBACK_PIXELS = 15
# Invincibility (milliseconds)
INVINCIBILITY_MS = 500 # 3 seconds of invincibility after hit
# Colors
WHITE = (255,255,255)
BLACK = (10,10,18)
RED = (230,60,60)
GREEN = (80,230,120)
YELLOW = (255,220,50)
CYAN = (80,200,235)
ORANGE = (255,150,60)
GRAY = (120,120,140)
SHIELD_BLUE = (90,180,240)
FONT_NAME = "Consolas"
# --------------------
# Utility
# --------------------
def clamp(v, a, b):
return max(a, min(b, v))
# --------------------
# Background (stars & planets)
# --------------------
class Star:
def __init__(self, w, h):
self.x = random.uniform(0, w)
self.y = random.uniform(0, h)
self.speed = random.uniform(0.6, 2.2)
self.size = random.randint(1, 3)
self.phase = random.uniform(0, math.pi*2)
def update(self, dt, h):
self.y += self.speed * dt
self.phase += dt * 0.1
if self.y > h:
self.y = -2
self.x = random.uniform(0, WIDTH)
def draw(self, surf):
alpha = 150 + int(100 * math.sin(self.phase))
color = (alpha, alpha, alpha)
pygame.draw.circle(surf, color, (int(self.x), int(self.y)), self.size)
class Planet:
def __init__(self, w, h):
self.x = random.uniform(80, w - 80)
self.y = random.uniform(-2000, -300)
self.speed = random.uniform(0.02, 0.12)
self.size = random.randint(40, 120)
self.color = (
random.randint(80, 230),
random.randint(80, 230),
random.randint(80, 230)
)
self.angle = random.uniform(0, math.pi*2)
self.spin = random.uniform(-0.6, 0.6)
def update(self, dt, h):
self.y += self.speed * dt
self.angle += self.spin * dt * 0.02
if self.y > h + 200:
self.x = random.uniform(80, WIDTH - 80)
self.y = random.uniform(-1200, -300)
self.speed = random.uniform(0.02, 0.12)
self.size = random.randint(40, 120)
self.color = (
random.randint(80, 230),
random.randint(80, 230),
random.randint(80, 230)
)
def draw(self, surf):
cx = int(self.x); cy = int(self.y)
pygame.draw.circle(surf, self.color, (cx, cy), self.size)
highlight_color = (min(255, self.color[0]+30), min(255, self.color[1]+30), min(255, self.color[2]+30))
hx = cx + int(self.size * 0.25 * math.cos(self.angle))
hy = cy - int(self.size * 0.25 * math.sin(self.angle))
pygame.draw.circle(surf, highlight_color, (hx, hy), int(self.size * 0.22))
stars = [Star(WIDTH, HEIGHT) for _ in range(140)]
planets = [Planet(WIDTH, HEIGHT) for _ in range(3)]
# --------------------
# Visual globe for menu
# --------------------
def draw_spinning_globe(surf, cx, cy, radius, angle):
pygame.draw.circle(surf, (12,60,110), (cx, cy), radius)
pygame.draw.circle(surf, (24,120,200), (cx, cy), radius, 2)
rect = pygame.Rect(cx - radius, cy - radius, radius*2, radius*2)
for i in range(6):
a = angle + i * 0.9
pygame.draw.arc(surf, (80,170,220), rect, a, a + math.pi, 2)
for j in range(-2,3):
w = 2 if j==0 else 1
ry = int(cy + j * (radius * 0.35))
pygame.draw.ellipse(surf, (80,160,200), (cx - int(radius*0.85), ry - 6, int(radius*1.7), 12), w)
for k in range(12):
theta = angle * 1.6 + k * (2*math.pi/12)
r = radius * 0.65
x = int(cx + math.cos(theta) * r)
y = int(cy + math.sin(theta) * r * 0.5)
pygame.draw.circle(surf, (200,230,255), (x, y), 2)
# --------------------
# Entities
# --------------------
class Particle:
def __init__(self, x, y, color, life=0.8):
self.x = x; self.y = y
self.vx = random.uniform(-3, 3)
self.vy = random.uniform(-6, -1)
self.color = color
self.life = life; self.time = 0
def update(self, dt):
self.time += dt; self.x += self.vx; self.y += self.vy; self.vy += 10 * dt
def draw(self, surf):
alpha = clamp(1 - (self.time / self.life), 0, 1)
if alpha <= 0: return
r = int(3 * (1 - alpha) + 1)
s = pygame.Surface((r*2, r*2), pygame.SRCALPHA)
col = (*self.color, int(255 * alpha))
pygame.draw.circle(s, col, (r, r), r); surf.blit(s, (self.x - r, self.y - r))
class Explosion:
def __init__(self, x, y, color=ORANGE, num=PARTICLE_COUNT):
self.particles = [Particle(x + random.uniform(-6,6), y + random.uniform(-6,6), color, life=random.uniform(0.5,1.1)) for _ in range(num)]
def update(self, dt):
for p in self.particles: p.update(dt)
self.particles = [p for p in self.particles if p.time < p.life]
def draw(self, surf):
for p in self.particles: p.draw(surf)
class Bullet:
def __init__(self, x, y, vy, color=YELLOW, owner="player", damage=1):
self.x = x; self.y = y; self.vy = vy; self.color = color; self.owner = owner; self.damage = damage
self.radius = 4 if owner=="player" else 5
self.rect = pygame.Rect(x-self.radius, y-self.radius, self.radius*2, self.radius*2)
def update(self, dt):
self.y += self.vy * (dt * 60 if dt < 5 else dt)
self.rect.center = (int(self.x), int(self.y))
def draw(self, surf): pygame.draw.circle(surf, self.color, (int(self.x), int(self.y)), self.radius)
def offscreen(self): return self.y < -30 or self.y > HEIGHT + 30
class BuffDrop:
def __init__(self, x, y, kind='multishot'):
self.x = x; self.y = y; self.kind = kind; self.vy = 2.2; self.radius = 10
self.rect = pygame.Rect(x - self.radius, y - self.radius, self.radius*2, self.radius*2)
self.angle = 0
def update(self, dt):
self.y += self.vy * (dt * 60 if dt < 5 else dt); self.angle += dt * 5; self.rect.center = (int(self.x), int(self.y))
def draw(self, surf):
x = int(self.x); y = int(self.y)
if self.kind == 'multishot':
pygame.draw.circle(surf, YELLOW, (x, y), self.radius)
ex = int(self.x + self.radius * math.cos(self.angle)); ey = int(self.y + self.radius * math.sin(self.angle))
pygame.draw.line(surf, WHITE, (self.x, self.y), (ex, ey), 2)
elif self.kind == 'shield':
pygame.draw.circle(surf, SHIELD_BLUE, (x, y), self.radius)
ex = int(self.x + self.radius * math.cos(self.angle)); ey = int(self.y + self.radius * math.sin(self.angle))
pygame.draw.line(surf, WHITE, (self.x, self.y), (ex, ey), 2)
elif self.kind == 'heal':
pygame.draw.circle(surf, GREEN, (x, y), self.radius)
ex = int(self.x + self.radius * math.cos(self.angle)); ey = int(self.y + self.radius * math.sin(self.angle))
pygame.draw.line(surf, WHITE, (self.x, self.y), (ex, ey), 2)
# --------------------
# Player
# --------------------
class Player:
def __init__(self):
self.x = PLAYER_START_X; self.y = PLAYER_Y; self.radius = PLAYER_RADIUS
self.hp_max = PLAYER_BASE_HP; self.hp = PLAYER_BASE_HP
self.fire_delay_ms = PLAYER_BASE_FIRE_DELAY_MS; self.last_shot_time = 0
self.multishot_active = False; self.multishot_end_time = 0
self.score = 0; self.lives = 1
self.speed = PLAYER_SPEED
# shield: absorbs multiple hits while active
self.shield_active = False
self.shield_uses = 0
# hit-flash indicator (visual only)
self.hit_flash = False
self.hit_flash_end = 0
# micro invincibility timer (ms since epoch)
self.invincible_until = 0
# ultimate
self.ultimate_count = 0; self.ultimate_needed = 10; self.ultimate_available = False
self.ultimate_active = False; self.ultimate_end_time = 0; self.ultimate_fire_delay_ms = ULTIMATE_FIRE_DELAY_MS; self.last_ultimate_shot_time = 0
def update(self):
now = pygame.time.get_ticks()
# Shield expiration: ensure shield ends when timer passes
if getattr(self, 'shield_active', False) and getattr(self, 'shield_end_time', 0) and now > self.shield_end_time:
self.shield_active = False
self.shield_end_time = 0
self.shield_uses = 0
if hasattr(self, 'shield_uses'):
self.shield_uses = 0
if self.multishot_active and now > self.multishot_end_time:
self.multishot_active = False
if self.ultimate_active and now > self.ultimate_end_time:
self.ultimate_active = False; self.ultimate_end_time = 0; self.last_ultimate_shot_time = 0
def draw(self, surf):
x, y = int(self.x), int(self.y)
# show invincibility ring if active
now = pygame.time.get_ticks()
if now < self.invincible_until:
alpha = 120 + int(80 * math.sin(now * 0.01))
ring_s = pygame.Surface((self.radius*4, self.radius*4), pygame.SRCALPHA)
pygame.draw.circle(ring_s, (200,200,255,int(alpha)), (self.radius*2, self.radius*2), self.radius+14, 6)
surf.blit(ring_s, (x - self.radius*2, y - self.radius*2))
# flicker frames (visual only)
if self.hit_flash:
if pygame.time.get_ticks() > self.hit_flash_end:
self.hit_flash = False
else:
# simple flicker: skip every other frame
if (pygame.time.get_ticks() // 60) % 2 == 0:
return # skip drawing this frame for flicker
# normal draw
pts = [
(x, y - self.radius),
(x - self.radius, y + self.radius),
(x + self.radius, y + self.radius)
]
pygame.draw.polygon(surf, CYAN, pts)
pygame.draw.polygon(surf, WHITE, pts, 2)
# shield visual
if self.shield_active and self.shield_uses > 0:
pygame.draw.circle(surf, SHIELD_BLUE, (x,y), self.radius + 10, 3)
def can_shoot(self):
return pygame.time.get_ticks() - self.last_shot_time >= self.fire_delay_ms
def shoot(self):
self.last_shot_time = pygame.time.get_ticks(); bullets = []
if self.multishot_active:
for s in (-14,0,14): bullets.append(Bullet(self.x + s, self.y - self.radius - 4, PLAYER_BULLET_SPEED, YELLOW, "player", damage=1))
else:
bullets.append(Bullet(self.x, self.y - self.radius - 6, PLAYER_BULLET_SPEED, YELLOW, "player", damage=1))
return bullets
def take_damage(self, amount=1, source=None, knockback=0):
"""
amount: HP to remove
source: (sx, sy) tuple indicating position of hit origin (bullet or enemy)
knockback: pixels to push player away from source (visual reaction)
Returns True if player died
"""
now = pygame.time.get_ticks()
# If currently invincible, ignore damage
if now < self.invincible_until:
# still apply a small hit flash for feedback but no HP loss
self.hit_flash = True
self.hit_flash_end = now + 180
return False
# shield absorbs up to its uses
if self.shield_active and self.shield_uses > 0:
self.shield_uses -= amount
if self.shield_uses <= 0:
self.shield_active = False; self.shield_uses = 0
# trigger flash and knockback even if shield absorbs
self.hit_flash = True
self.hit_flash_end = now + 180
# apply invincibility to prevent immediate follow-up hits
self.invincible_until = now + INVINCIBILITY_MS
if source and knockback:
sx, sy = source
dx = self.x - sx; dy = self.y - sy