-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_invaders.py
More file actions
265 lines (231 loc) · 9.14 KB
/
Copy pathspace_invaders.py
File metadata and controls
265 lines (231 loc) · 9.14 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
import pygame
import random
pygame.init()
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Space Invaders")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PLAYER_COLOR = (50, 150, 250)
ALIEN_COLOR = (200, 50, 50)
BULLET_COLOR = (255, 255, 0)
ALIEN_BULLET_COLOR = (255, 0, 0)
FPS = 60
PLAYER_WIDTH = 40
PLAYER_HEIGHT = 20
PLAYER_INIT_SPEED = 3
PLAYER_SHOT_COOLDOWN = 200
ALIEN_WIDTH = 30
ALIEN_HEIGHT = 20
ALIEN_INIT_SPEED_X = 3
ALIEN_DROP_Y = 20
ALIEN_SHOT_COOLDOWN = 1500
PLAYER_SPEED_INC = 1.1
PLAYER_SHOT_COOLDOWN_DEC = 0.9
ALIEN_SPEED_INC = 1.1
ALIEN_SHOT_COOLDOWN_DEC = 0.9
ALIEN_BULLETS_INC = 1.2
clock = pygame.time.Clock()
class Player:
def __init__(self, x, y, speed):
self.rect = pygame.Rect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT)
self.speed = speed
self.shoot_cooldown = PLAYER_SHOT_COOLDOWN
self.last_shot_time = 0
def draw(self, surface):
pygame.draw.rect(surface, PLAYER_COLOR, self.rect)
def move(self, dx):
self.rect.x += dx
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
def shoot(self, current_time, bullets):
if current_time - self.last_shot_time >= self.shoot_cooldown:
bullets.append(Bullet(self.rect.centerx, self.rect.top, -10))
self.last_shot_time = current_time
class Alien:
def __init__(self, x, y, speed_x):
self.rect = pygame.Rect(x, y, ALIEN_WIDTH, ALIEN_HEIGHT)
self.speed_x = speed_x
self.alive = True
def draw(self, surface):
pygame.draw.rect(surface, ALIEN_COLOR, self.rect)
class Bullet:
def __init__(self, x, y, speed_y):
self.rect = pygame.Rect(x - 2, y, 4, 10)
self.speed_y = speed_y
self.alive = True
def update(self):
self.rect.y += self.speed_y
if self.rect.bottom < 0 or self.rect.top > SCREEN_HEIGHT:
self.alive = False
def create_aliens(rows, cols, speed_x):
"""Create aliens with a slightly randomized pattern for each wave."""
aliens = []
padding_x = 20
padding_y = 20
start_x = (SCREEN_WIDTH - (cols * (ALIEN_WIDTH + padding_x)) + padding_x) // 2
for row in range(rows):
for col in range(cols):
# Randomly skip some aliens to create a varied pattern
if random.random() < 0.15: # ~15% chance to skip alien
continue
x_offset = random.randint(-5, 5)
x = start_x + col * (ALIEN_WIDTH + padding_x) + x_offset
y = 50 + row * (ALIEN_HEIGHT + padding_y)
aliens.append(Alien(x, y, speed_x))
return aliens
def draw_text(text, size, x, y, center=False):
font = pygame.font.SysFont(None, size)
surf = font.render(text, True, WHITE)
rect = surf.get_rect()
if center:
rect.center = (x, y)
else:
rect.topleft = (x, y)
screen.blit(surf, rect)
return rect
def show_menu():
options = ["Easy", "Medium", "Hard"]
selected = 0
while True:
screen.fill(BLACK)
draw_text("Space Invaders", 48, SCREEN_WIDTH//2, 100, center=True)
for i, option in enumerate(options):
color = WHITE if i == selected else (150, 150, 150)
surf = pygame.font.SysFont(None, 36).render(option, True, color)
rect = surf.get_rect(center=(SCREEN_WIDTH//2, 200 + i*50))
screen.blit(surf, rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
selected = (selected - 1) % len(options)
elif event.key == pygame.K_DOWN:
selected = (selected + 1) % len(options)
elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
return options[selected]
if event.type == pygame.MOUSEBUTTONDOWN:
mx, my = event.pos
for i, option in enumerate(options):
option_rect = pygame.Rect(SCREEN_WIDTH//2 - 50, 200 + i*50 - 18, 100, 36)
if option_rect.collidepoint(mx, my):
return options[i]
clock.tick(FPS)
def run_game(difficulty):
if difficulty == "Easy":
player_speed = PLAYER_INIT_SPEED
alien_speed_x = ALIEN_INIT_SPEED_X * 0.7
alien_shot_interval = ALIEN_SHOT_COOLDOWN * 1.5
elif difficulty == "Hard":
player_speed = PLAYER_INIT_SPEED * 1.2
alien_speed_x = ALIEN_INIT_SPEED_X * 1.5
alien_shot_interval = ALIEN_SHOT_COOLDOWN * 0.7
else:
player_speed = PLAYER_INIT_SPEED
alien_speed_x = ALIEN_INIT_SPEED_X
alien_shot_interval = ALIEN_SHOT_COOLDOWN
player = Player(SCREEN_WIDTH//2 - PLAYER_WIDTH//2, SCREEN_HEIGHT - 60, player_speed)
aliens = create_aliens(3, 6, alien_speed_x)
bullets = []
alien_bullets = []
alien_direction = 1
last_alien_shot_time = pygame.time.get_ticks()
wave = 1
score = 0
dragging = False
while True:
current_time = pygame.time.get_ticks()
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
if event.key == pygame.K_SPACE:
player.shoot(current_time, bullets)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
dragging = True
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
dragging = False
if event.type == pygame.MOUSEMOTION and dragging:
mouse_x = event.pos[0]
player.rect.centerx = mouse_x
player.shoot(current_time, bullets)
keys = pygame.key.get_pressed()
dx = 0
if keys[pygame.K_LEFT]:
dx = -player.speed
if keys[pygame.K_RIGHT]:
dx = player.speed
player.move(dx)
edge_hit = False
for alien in aliens:
if alien.alive:
alien.rect.x += alien_speed_x * alien_direction
if alien.rect.right >= SCREEN_WIDTH or alien.rect.left <= 0:
edge_hit = True
if edge_hit:
alien_direction *= -1
for alien in aliens:
alien.rect.y += ALIEN_DROP_Y # drop only one row
if current_time - last_alien_shot_time >= alien_shot_interval:
shooters = [alien for alien in aliens if alien.alive]
num_shots = max(1, int(len(shooters) * 0.3)) # increase with wave
for shooter in random.sample(shooters, num_shots):
alien_bullets.append(Bullet(shooter.rect.centerx, shooter.rect.bottom, 5))
last_alien_shot_time = current_time
for bullet in bullets + alien_bullets:
bullet.update()
bullets = [b for b in bullets if b.alive]
alien_bullets = [b for b in alien_bullets if b.alive]
for bullet in bullets:
for alien in aliens:
if alien.alive and bullet.alive and alien.rect.colliderect(bullet.rect):
alien.alive = False
bullet.alive = False
score += 10
for bullet in alien_bullets:
if bullet.alive and player.rect.colliderect(bullet.rect):
return False
for alien in aliens:
if alien.alive and alien.rect.bottom >= player.rect.top:
return False
aliens = [alien for alien in aliens if alien.alive]
player.draw(screen)
for alien in aliens:
alien.draw(screen)
for bullet in bullets:
pygame.draw.rect(screen, BULLET_COLOR, bullet.rect)
for bullet in alien_bullets:
pygame.draw.rect(screen, ALIEN_BULLET_COLOR, bullet.rect)
draw_text(f"Score: {score}", 24, 10, 10)
draw_text(f"Wave: {wave}", 24, SCREEN_WIDTH - 100, 10)
pygame.display.flip()
if not aliens:
wave += 1
player.speed *= PLAYER_SPEED_INC
player.shoot_cooldown = int(player.shoot_cooldown * PLAYER_SHOT_COOLDOWN_DEC)
alien_speed_x *= ALIEN_SPEED_INC
alien_shot_interval = int(alien_shot_interval * ALIEN_SHOT_COOLDOWN_DEC)
aliens = create_aliens(3, 6, alien_speed_x) # new random pattern
last_alien_shot_time = current_time
clock.tick(FPS)
def main():
while True:
choice = show_menu()
if choice is None:
break
run_game(choice)
pygame.quit()
if __name__ == "__main__":
main()