-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgalaxian.py
310 lines (256 loc) · 10.6 KB
/
galaxian.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: chat GPT -4
# Instructor: [email protected]
# Version:
# Issue Date: September 19, 2023
# Release Note:
import pygame
import random
import math
# Initialize pygame
pygame.init()
# Configuration
BACKGROUND_COLOR = (0, 0, 0)
ENEMY_COLOR = (255, 255, 255)
SHIP_COLOR = (0, 0, 255)
NUM_ENEMIES = 5
ENEMY_BULLET_CHANCE = 0.01
SPAWN_ENEMY_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(SPAWN_ENEMY_EVENT, 2000) # spawns an enemy every 2 seconds
# Screen dimensions
WIDTH = 800
HEIGHT = 600
# Enemy properties
ENEMY_WIDTH = 50
ENEMY_HEIGHT = 30
ENEMY_SPEED = 3
ENEMY_AMPLITUDE = 100
# Bullet properties
BULLET_WIDTH = 5
BULLET_HEIGHT = 10
BULLET_SPEED = 7
# Laser properties
LASER_WIDTH = 10
LASER_HEIGHT = HEIGHT
LASER_SPEED = 15
# Spaceship properties
SHIP_WIDTH = 60
SHIP_HEIGHT = 20
SHIP_SPEED = 5
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Galaxian Style Game")
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([ENEMY_WIDTH, ENEMY_HEIGHT], pygame.SRCALPHA)
self.draw_bee()
self.rect = self.image.get_rect()
self.start_x = random.randint(0, WIDTH - ENEMY_WIDTH)
self.rect.x = self.start_x
self.rect.y = random.randint(0, HEIGHT // 2 - ENEMY_HEIGHT)
self.angle = random.uniform(0, math.pi * 2)
def draw_bee(self):
pygame.draw.ellipse(self.image, ENEMY_COLOR, [0, 10, ENEMY_WIDTH, ENEMY_HEIGHT - 20])
stripe_width = ENEMY_WIDTH // 3
for i in range(3):
if i % 2 == 1:
pygame.draw.rect(self.image, (0, 0, 0), [i * stripe_width, 10, stripe_width, ENEMY_HEIGHT - 20])
pygame.draw.arc(self.image, (200, 200, 200), [0, 0, ENEMY_WIDTH // 2, ENEMY_HEIGHT // 2], 0, math.pi, 3)
pygame.draw.arc(self.image, (200, 200, 200), [ENEMY_WIDTH // 2, 0, ENEMY_WIDTH // 2, ENEMY_HEIGHT // 2], 0, math.pi, 3)
def update(self):
self.angle += 0.05
offset_x = ENEMY_AMPLITUDE * math.sin(self.angle)
offset_y = ENEMY_AMPLITUDE * math.cos(self.angle)
self.rect.x = (self.start_x + offset_x) % WIDTH
self.rect.y = (self.rect.y + ENEMY_SPEED) % HEIGHT
if random.random() < ENEMY_BULLET_CHANCE:
bullet = EnemyBullet(self.rect.centerx, self.rect.bottom)
bullets.add(bullet)
class EnemyBullet(pygame.sprite.Sprite):
ENEMY_BULLET_COLOR = (255, 255, 0)
def __init__(self, x, y, dx=0, dy=BULLET_SPEED):
super().__init__()
self.image = pygame.Surface([BULLET_WIDTH, BULLET_HEIGHT])
self.image.fill(self.ENEMY_BULLET_COLOR)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dx = dx
self.dy = dy
self.shooter = "enemy"
def update(self):
self.rect.x += self.dx
self.rect.y += self.dy
if self.rect.y > HEIGHT or self.rect.x < 0 or self.rect.x > WIDTH:
self.kill()
class Bullet(pygame.sprite.Sprite):
BULLET_COLOR = (255, 255, 255)
def __init__(self, x, y, shooter="enemy", dx=0, dy=BULLET_SPEED):
super().__init__()
self.shooter = shooter
self.image = pygame.Surface([BULLET_WIDTH, BULLET_HEIGHT])
self.image.fill(self.BULLET_COLOR)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dx = dx
self.dy = dy
def update(self):
if self.shooter == "enemy":
self.rect.y += self.dy
else:
self.rect.x += self.dx
self.rect.y -= self.dy
if self.rect.y < 0 or self.rect.y > HEIGHT:
self.kill()
class Laser(pygame.sprite.Sprite):
LASER_COLOR = (255, 255, 255)
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([LASER_WIDTH, LASER_HEIGHT], pygame.SRCALPHA)
pygame.draw.line(self.image, self.LASER_COLOR, (LASER_WIDTH // 2, 0), (LASER_WIDTH // 2, LASER_HEIGHT), LASER_WIDTH)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.y = y
def update(self):
self.rect.y -= LASER_SPEED
if self.rect.bottom < 0:
self.kill()
class Spray(pygame.sprite.Sprite):
SPRAY_COLOR = (255, 255, 255)
def __init__(self, x, y):
super().__init__()
# Create the 5 bullets in a spray pattern
spread_angles = [-60, -30, 0, 30, 60] # angles covering 120 degrees
for angle in spread_angles:
dx = BULLET_SPEED * math.sin(math.radians(angle))
dy = BULLET_SPEED * math.cos(math.radians(angle))
bullet = Bullet(x, y, "player", dx, dy)
bullet.image.fill(self.SPRAY_COLOR) # Change the bullet color to spray color
bullets.add(bullet)
class Spaceship(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([SHIP_WIDTH, SHIP_HEIGHT], pygame.SRCALPHA)
self.draw_rocket()
self.rect = self.image.get_rect()
self.rect.x = (WIDTH - SHIP_WIDTH) / 2
self.rect.y = HEIGHT - SHIP_HEIGHT - 10
self.bullet_type = "default"
def draw_rocket(self):
# Body of the airplane
pygame.draw.rect(self.image, SHIP_COLOR, [SHIP_WIDTH // 4, SHIP_HEIGHT // 4, SHIP_WIDTH // 2, SHIP_HEIGHT // 2])
# Wings of the airplane
pygame.draw.polygon(self.image, SHIP_COLOR, [(SHIP_WIDTH // 4, SHIP_HEIGHT // 2), (0, 3 * SHIP_HEIGHT // 4), (SHIP_WIDTH // 4, 3 * SHIP_HEIGHT // 4)])
pygame.draw.polygon(self.image, SHIP_COLOR, [(3 * SHIP_WIDTH // 4, SHIP_HEIGHT // 2), (SHIP_WIDTH, 3 * SHIP_HEIGHT // 4), (3 * SHIP_WIDTH // 4, 3 * SHIP_HEIGHT // 4)])
# Tail of the airplane
pygame.draw.polygon(self.image, SHIP_COLOR, [(SHIP_WIDTH // 2, 0), (SHIP_WIDTH // 2 - SHIP_WIDTH // 8, SHIP_HEIGHT // 4), (SHIP_WIDTH // 2 + SHIP_WIDTH // 8, SHIP_HEIGHT // 4)])
def update(self, keys):
if keys[pygame.K_LEFT]:
self.rect.x -= SHIP_SPEED
if keys[pygame.K_RIGHT]:
self.rect.x += SHIP_SPEED
self.rect.x = max(0, min(WIDTH - SHIP_WIDTH, self.rect.x))
def shoot(self):
if self.bullet_type == "default":
bullet1 = Bullet(self.rect.left + SHIP_WIDTH // 4, self.rect.top, "player")
bullet2 = Bullet(self.rect.right - SHIP_WIDTH // 4, self.rect.top, "player")
bullets.add(bullet1, bullet2)
elif self.bullet_type == "spray":
spray = Spray(self.rect.centerx, self.rect.top)
elif self.bullet_type == "laser":
laser = Laser(self.rect.centerx, 0)
bullets.add(laser)
def toggle_bullet_type(self):
bullet_types = ["default", "spray", "laser"]
current_index = bullet_types.index(self.bullet_type)
self.bullet_type = bullet_types[(current_index + 1) % len(bullet_types)]
def game_over_screen():
font = pygame.font.SysFont('Arial', 36)
text_surface = font.render('GAME OVER', True, (255, 0, 0))
text_rect = text_surface.get_rect(center=(WIDTH/2, HEIGHT/2))
screen.blit(text_surface, text_rect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return False # Indicate game should not restart
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
return True # Indicate game should restart
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
spaceship = Spaceship()
for i in range(NUM_ENEMIES):
enemy = Enemy()
enemies.add(enemy)
running = True
clock = pygame.time.Clock()
AUTOMATIC_SHOOT_EVENT = pygame.USEREVENT + 2
pygame.time.set_timer(AUTOMATIC_SHOOT_EVENT, 500) # spaceship shoots every 0.5 seconds
while running:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
spaceship.toggle_bullet_type()
if event.key == pygame.K_SPACE:
spaceship.shoot()
if event.type == AUTOMATIC_SHOOT_EVENT:
spaceship.shoot()
if event.type == SPAWN_ENEMY_EVENT:
enemy = Enemy()
enemies.add(enemy)
screen.fill(BACKGROUND_COLOR)
enemies.update()
bullets.update()
spaceship.update(keys)
enemies.draw(screen)
bullets.draw(screen)
screen.blit(spaceship.image, spaceship.rect)
# Check bullet collisions with enemies
enemy_bullet_collisions = pygame.sprite.groupcollide(enemies, bullets, True, True, pygame.sprite.collide_mask)
for enemy in enemy_bullet_collisions:
# You can add score or other effects here for each destroyed enemy
pass
# Check bullet collisions with spaceship (only consider enemy bullets)
enemy_bullets = [bullet for bullet in bullets if isinstance(bullet, EnemyBullet)]
enemy_bullets_group = pygame.sprite.Group(*enemy_bullets) # Convert the list into a Group
spaceship_bullet_collisions = pygame.sprite.spritecollide(spaceship, enemy_bullets_group, True, pygame.sprite.collide_mask)
# Notice the True here, this means the bullets will be removed upon collision
if spaceship_bullet_collisions:
spaceship.kill()
# Show the game over screen and check if the game should restart
if game_over_screen():
# Logic to restart the game
enemies.empty()
bullets.empty()
spaceship = Spaceship()
for i in range(NUM_ENEMIES):
enemy = Enemy()
enemies.add(enemy)
continue
else:
running = False
# Check direct collisions between enemies and spaceship
if pygame.sprite.spritecollide(spaceship, enemies, True, pygame.sprite.collide_mask):
spaceship.kill()
# Show the game over screen and check if the game should restart
if game_over_screen():
# Logic to restart the game
enemies.empty()
bullets.empty()
spaceship = Spaceship()
for i in range(NUM_ENEMIES):
enemy = Enemy()
enemies.add(enemy)
continue
else:
running = False
pygame.display.flip()
clock.tick(60)
pygame.quit()