-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_game.py
More file actions
397 lines (317 loc) · 11 KB
/
Copy pathspace_game.py
File metadata and controls
397 lines (317 loc) · 11 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
import winsound
import turtle
import random
import time
import threading
import os
import math
import pygame
pygame.mixer.init()
pygame.mixer.music.load("game_sound.wav.mp3")
pygame.mixer.music.play(-1)
laser_sound = pygame.mixer.Sound("fire_sound.wav.wav")
explosion_sound = pygame.mixer.Sound("boom.wav")
def play_laser_sound():
laser_sound.play()
def play_explosion_sound():
explosion_sound.play()
WIDTH, HEIGHT = 800, 600
FPS = 60
PINK = "pink"
WHITE = "white"
planet_colors = [
"#f8f0ff", # Soft white glow
"#d6b3ff", # Light purple
"#b56dff", # Vivid violet
"#9441e3", # Darker violet
"#6a24b6", # Deep purple
"#3f1061"
]
screen = turtle.Screen()
screen.title("🚀 Space Escape")
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.tracer(0)
screen.register_shape("spa.gif")
screen.register_shape("boom.gif")
screen.register_shape("fire1.gif")
screen.register_shape("meteorites1.2.gif")
screen.register_shape("power.gif")
screen.register_shape("explosion.gif")
paused = False
pause_label = turtle.Turtle()
pause_label.hideturtle()
pause_label.penup()
pause_label.color("white")
def toggle_pause():
global paused
paused = not paused
if paused:
pause_label.goto(0, 0)
pause_label.write("Press P to continue", align="center",
font=("Courier", 24, "bold"))
else:
pause_label.clear()
screen.onkeypress(toggle_pause, "p")
class Star:
def __init__(self):
self.x = random.randint(-WIDTH // 2, WIDTH // 2)
self.y = random.randint(-HEIGHT // 2, HEIGHT // 2)
self.size = random.uniform(1.0, 3.0)
self.twinkle_speed = random.uniform(0.05, 0.2)
self.phase = random.uniform(0, 2 * math.pi)
def draw(self, turtle_obj, frame):
size = self.size + \
math.sin(self.phase + frame * self.twinkle_speed) * 0.3
turtle_obj.penup()
turtle_obj.goto(self.x, self.y)
turtle_obj.dot(int(size), WHITE)
class NebulaLine:
def __init__(self):
self.start = (random.randint(-WIDTH // 2, WIDTH // 2),
random.randint(-HEIGHT // 2, HEIGHT // 2))
self.end = (self.start[0] + random.randint(-100, 100),
self.start[1] + random.randint(-100, 100))
self.color = PINK
def draw(self, turtle_obj):
turtle_obj.penup()
turtle_obj.goto(self.start)
turtle_obj.pendown()
turtle_obj.color(self.color)
turtle_obj.goto(self.end)
class TitleLetter:
def __init__(self, char, x, y):
self.char = char
self.x = x
self.y = y
def draw(self, turtle_obj):
turtle_obj.penup()
turtle_obj.goto(self.x, self.y)
turtle_obj.color(WHITE)
turtle_obj.write(self.char, align="center",
font=("Courier", 24, "bold"))
class Spaceship:
def __init__(self):
self.turtle = turtle.Turtle()
self.turtle.penup()
self.turtle.shape("spa.gif")
self.turtle.setheading(90)
self.turtle.goto(0, -250)
self.speed_x = 0
self.speed_y = 0
def move_left(self):
self.speed_x = -5
def move_right(self):
self.speed_x = 5
def move_up(self):
self.speed_y = 5
def move_down(self):
self.speed_y = -5
def stop_horizontal(self):
self.speed_x = 0
def stop_vertical(self):
self.speed_y = 0
def update_position(self):
new_x = self.turtle.xcor() + self.speed_x
new_y = self.turtle.ycor() + self.speed_y
if -WIDTH // 2 < new_x < WIDTH // 2:
self.turtle.setx(new_x)
if -HEIGHT // 2 < new_y < HEIGHT // 2:
self.turtle.sety(new_y)
def fire_laser(self, lasers):
laser = turtle.Turtle()
laser.penup()
laser.shape("fire1.gif")
laser.goto(self.turtle.xcor(), self.turtle.ycor() + 20)
laser.setheading(90)
lasers.append(laser)
play_laser_sound()
def spawn_power_up():
power_up = turtle.Turtle()
power_up.shape("power.gif")
power_up.penup()
power_up.goto(random.randint(-350, 350), random.randint(-250, 250))
return power_up
def play_intro_animation(stars):
nebula_lines = [NebulaLine() for _ in range(30)]
title_letters = [
TitleLetter("S", -80, 50),
TitleLetter("P", -40, 50),
TitleLetter("A", 0, 50),
TitleLetter("C", 40, 50),
TitleLetter("E", 80, 50),
]
drawer = turtle.Turtle()
drawer.hideturtle()
drawer.speed(0)
frame = 0
anim_frame = 0
while anim_frame < 120:
screen.update()
drawer.clear()
for star in stars:
star.draw(drawer, frame)
if 30 <= anim_frame < 60:
for line in nebula_lines:
if random.random() < 0.3:
line.draw(drawer)
if 60 <= anim_frame < 90:
title_frame = anim_frame - 60
for i, letter in enumerate(title_letters):
letter_start = i * 5
if title_frame > letter_start:
letter.draw(drawer)
frame += 1
anim_frame += 1
time.sleep(1 / FPS)
def start_game(stars):
chances = 3
lives = []
for i in range(chances):
life = turtle.Turtle()
life.shape("power.gif")
life.penup()
life.goto(250 + (i * 40), 260)
lives.append(life)
spaceship = Spaceship()
meteors = []
for _ in range(5):
meteor = turtle.Turtle()
meteor.shape("meteorites1.2.gif")
meteor.penup()
meteor.goto(random.randint(-350, 350), random.randint(100, 300))
meteors.append(meteor)
lasers = []
score = 0
remaining_lives = 3
score_display = turtle.Turtle()
score_display.hideturtle()
score_display.penup()
score_display.color("white")
score_display.goto(-350, 260)
score_display.write(f"Score: {score}", font=("Courier", 16, "bold"))
screen.listen()
screen.onkeypress(spaceship.move_left, "Left")
screen.onkeypress(spaceship.move_right, "Right")
screen.onkeypress(spaceship.move_up, "Up")
screen.onkeypress(spaceship.move_down, "Down")
screen.onkeyrelease(spaceship.stop_horizontal, "Left")
screen.onkeyrelease(spaceship.stop_horizontal, "Right")
screen.onkeyrelease(spaceship.stop_vertical, "Up")
screen.onkeyrelease(spaceship.stop_vertical, "Down")
screen.onkeypress(lambda: spaceship.fire_laser(
lasers), "space")
star_drawer = turtle.Turtle()
star_drawer.hideturtle()
star_drawer.speed(0)
global game_over
game_over = False
score_writer = turtle.Turtle()
score_writer.hideturtle()
score_writer.penup()
score_writer.color("white")
frame = 0
while not game_over:
screen.update()
time.sleep(1 / FPS)
if paused:
continue
star_drawer.clear()
for star in stars:
star.draw(star_drawer, frame)
spaceship.update_position()
for laser in lasers[:]:
laser.forward(10)
if laser.ycor() > HEIGHT // 2:
laser.hideturtle()
lasers.remove(laser)
for meteor in meteors:
meteor.sety(meteor.ycor() - 5)
if meteor.distance(spaceship.turtle) < 60:
meteor.hideturtle()
spaceship.turtle.hideturtle()
boom = turtle.Turtle()
boom.shape("boom.gif")
boom.penup()
boom.goto(spaceship.turtle.xcor(), spaceship.turtle.ycor())
boom.showturtle()
screen.update()
play_explosion_sound()
time.sleep(1)
time.sleep(0.5)
boom.hideturtle()
meteor.goto(random.randint(-350, 350),
random.randint(400, 600))
meteor.showturtle()
spaceship.turtle.showturtle()
if remaining_lives > 0:
remaining_lives -= 1
lives[remaining_lives - 1].hideturtle()
if remaining_lives == 0:
game_over = True
score_writer.clear()
show_game_over_window(score)
if meteor.ycor() < -300:
meteor.goto(random.randint(-350, 350),
random.randint(100, 300))
for laser in lasers[:]:
for meteor in meteors[:]:
if laser.distance(meteor) < 60:
meteor.shape("explosion.gif")
screen.update()
time.sleep(0.3)
meteor.shape("meteorites1.2.gif")
meteor.goto(random.randint(-350, 350),
random.randint(100, 300))
laser.hideturtle()
if laser in lasers:
lasers.remove(laser)
score += 1
score_display.clear()
score_display.write(
f"Score: {score}", font=("Courier", 16, "bold"))
frame += 1
pygame.mixer.music.stop()
screen.bye()
def show_game_over_window(score):
game_over_turtle = turtle.Turtle()
game_over_turtle.hideturtle()
game_over_turtle.penup()
game_over_turtle.color("white")
game_over_turtle.goto(0, 100)
game_over_turtle.write(
f"☄️ GAME OVER\nFinal Score: {int(score)}",
align="center",
font=("Courier", 24, "bold"),
)
restart_button = turtle.Turtle()
restart_button.shape("square")
restart_button.color("green")
restart_button.shapesize(stretch_wid=1, stretch_len=5)
restart_button.penup()
restart_button.goto(-100, -50)
quit_button = turtle.Turtle()
quit_button.shape("square")
quit_button.color("red")
quit_button.shapesize(stretch_wid=1, stretch_len=5)
quit_button.penup()
quit_button.goto(100, -50)
def restart_game(x, y):
if -150 < x < -50 and -75 < y < -25:
game_over_turtle.clear()
restart_button.hideturtle()
quit_button.hideturtle()
restart_button.clear()
quit_button.clear()
pygame.mixer.music.stop()
play_intro_animation(stars)
start_game(stars)
def quit_game(x, y):
if 50 < x < 150 and -75 < y < -25:
pygame.mixer.music.stop()
screen.bye()
restart_button.onclick(restart_game)
quit_button.onclick(quit_game)
stars = [Star() for _ in range(100)]
play_intro_animation(stars)
start_game(stars)