-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_rocks.py
More file actions
360 lines (296 loc) · 11.5 KB
/
space_rocks.py
File metadata and controls
360 lines (296 loc) · 11.5 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
import pygame
import math
import sys
import time
import random
def distance(x1,y1,x2,y2):
return math.sqrt( (x2-x1)**2 + (y2-y1)**2 )
def detect_collisions():
global player_score, missile_list, asteroid_list, player_lives
new_asteroids = []
for missile in reversed(missile_list):
for asteroid in reversed(asteroid_list):
dist = distance(missile[X],missile[Y],asteroid[X],asteroid[Y])
if dist < asteroid[SIZE]:
player_score += asteroid[SIZE]*5
bang_large.play()
if missile in missile_list:
missile_list.remove(missile)
if asteroid in asteroid_list:
asteroid_list.remove(asteroid)
if asteroid[SIZE] == ASTEROID_SMALL:
break
for i in range( asteroid[SIZE]//16 + 1):
x = asteroid[X] + random.randint(-10,10)
y = asteroid[Y] + random.randint(-10,10)
delta_x = asteroid[DELTA_X] * random.uniform(-2,2)
delta_y = asteroid[DELTA_Y] * random.uniform(-2,2)
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
if asteroid[SIZE] == 64:
size = 16
else:
size = 8
new_asteroid = [x,y,delta_x,delta_y, [red,green,blue], size]
new_asteroids.append(new_asteroid)
break
#check for asteroid collisions with the players ship
for asteroid in asteroid_list:
left = int(asteroid[X])- asteroid[SIZE]
top = int(asteroid[Y]) + asteroid[SIZE]
width = height = asteroid[SIZE]*2
asteroid_rect = pygame.Rect(left,top,width,height)
if shipRect.colliderect(asteroid_rect):
#player_lives -= 1
pass
#add the new asteroids in to the asteroid list
for asteroid in new_asteroids:
asteroid_list.append(asteroid)
return
def spawn_random_asteroid():
global asteroid_list
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
trans = random.randint(75,200)
size = random.randint(0,2)
x = shipRect.centerx
y = shipRect.centery
while distance(x,y,shipRect.centerx, shipRect.centery) < 200:
x = random.randint(64,SCREEN_WIDTH-64)
y = random.randint(64,SCREEN_HEIGHT-64)
delta_x = delta_y = 0
while delta_x == 0 and delta_y == 0:
delta_x = random.randint(-3,3)
delta_y = random.randint(-3,3)
asteroid = [x,y,delta_x,delta_y, [red,green,blue, trans], ASTEROID_SIZES[size]]
asteroid_list.append(asteroid)
def check_pygame_events():
global ship_angle, ship_delta_x, ship_delta_y, game_state
keys = pygame.key.get_pressed()
event = pygame.event.poll()
if event.type == pygame.QUIT:
game_state = GAME_STATE_QUIT
if keys[pygame.K_SPACE] and game_state == GAME_STATE_RUNNING:
fire_sound.play()
spawn_missile()
if keys[pygame.K_p] and game_state == GAME_STATE_RUNNING:
game_state = GAME_STATE_PAUSED
if keys[pygame.K_SPACE] and game_state == GAME_STATE_TITLE:
game_state = GAME_STATE_RUNNING
reset_game()
if keys[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
if keys[pygame.K_LEFT]:
ship_angle += 5
if keys[pygame.K_RIGHT]:
ship_angle -= 5
if keys[pygame.K_DOWN]:
ship_delta_x -= math.cos(math.radians(ship_angle))*ship_accel
ship_delta_y -= - (math.sin(math.radians(ship_angle)))*ship_accel
if keys[pygame.K_UP]:
thrust_sound.play()
ship_delta_x += math.cos(math.radians(ship_angle))*ship_accel
ship_delta_y += - (math.sin(math.radians(ship_angle)))*ship_accel
return
def rot_center(image, rect, ship_angle):
rot_image = pygame.transform.rotate(image, ship_angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image,rot_rect
def spawn_missile():
global last_shot_time, missile_list
if time.time() - last_shot_time < shot_time:
return
last_shot_time = time.time()
rect = shipRect
x = rect.centerx + math.cos(math.radians(ship_angle))
y = rect.centery + (-math.sin(math.radians(ship_angle)))
delta_x = math.cos(math.radians(ship_angle))*4 + ship_delta_x
delta_y = -(math.sin(math.radians(ship_angle))*4) + ship_delta_y
missile = [x,y,delta_x,delta_y]
missile_list.append(missile)
def decelerate_player():
global ship_delta_x, ship_delta_y
if ship_delta_x != 0:
ship_delta_x *= ship_friction
if ship_delta_y !=0:
ship_delta_y *= ship_friction
return
def move_objects():
global shipRect, newshipImg, missilelist, asteroid_list
#move player
shipRect.centerx += int(ship_delta_x)
shipRect.centery += int(ship_delta_y)
newshipImg,shipRect = rot_center(shipImg, shipRect, ship_angle)
#move missiles
for missile in missile_list:
missile[X] += missile[DELTA_X]
missile[Y] += missile[DELTA_Y]
for asteroid in asteroid_list:
asteroid[X] = asteroid[X] + asteroid[DELTA_X]
asteroid[Y] = asteroid[Y] + asteroid[DELTA_Y]
return
def check_bounds():
global ship_delta_x, ship_delta_y, missile_list, asteroid_list
global missile_remove
global missile_remove
if shipRect.top <0:
shipRect.top = 1
ship_delta_y = -(ship_delta_y)
if shipRect.bottom > SCREEN_HEIGHT:
shipRect.bottom = SCREEN_HEIGHT-1
ship_delta_y = -(ship_delta_y)
if shipRect.left < 0:
shipRect.left = 0
ship_delta_x = -(ship_delta_x)
if shipRect.right > SCREEN_WIDTH:
shipRect.right = SCREEN_WIDTH-1
ship_delta_x = -(ship_delta_x)
#missile_copy = copy.deepcopy(missile_list)
for missile in missile_list:
if missile[X] <0 or missile[X]>SCREEN_WIDTH or missile[Y] < 0 or missile[Y] > SCREEN_HEIGHT:
missile_list.remove(missile)
for asteroid in asteroid_list:
if asteroid[X] < asteroid[SIZE] or asteroid[X] > SCREEN_WIDTH-asteroid[SIZE]:
asteroid[DELTA_X] = -asteroid[DELTA_X]
if asteroid[Y] < asteroid[SIZE] or asteroid[Y] > SCREEN_HEIGHT-asteroid[SIZE] :
asteroid[DELTA_Y] = -asteroid[DELTA_Y]
def draw_scene():
screen.fill( (0,0,0,128) )
screen.blit(background_image,(0,0) )
score_text = font.render("SCORE: "+str(player_score), 1, GREEN)
screen.blit(score_text,(0, 5))
for asteroid in asteroid_list:
pygame.draw.circle(screen, asteroid[COLOR], (int(asteroid[X]), int(asteroid[Y])), asteroid[SIZE], 0)
for missile in missile_list:
x = missile[X]
y = missile[Y]
pygame.draw.circle( screen, (255,255,255) , ( int(missile[X]),int(missile[Y]) ),1)
screen.blit(newshipImg,shipRect)
left = player_lives*32
for x in range(player_lives):
rect = (SCREEN_WIDTH-left+(x*32),10,32,32)
screen.blit(shipImg, rect)
return
def reset_game():
global player_score
player_score = 0
for x in range(20):
spawn_random_asteroid()
pygame.mixer.music.play(-1)
time.sleep(1)
def check_extra_life():
global extra_life_score, player_lives
if player_score > extra_life_score and player_score< max_lives_score:
extra_ship_sound.play()
player_lives += 1
extra_life_score = extra_life_score + 1000
def game_paused():
global game_state
pygame.mixer.pause()
pygame.mixer.music.pause()
while game_state == GAME_STATE_PAUSED:
keys = pygame.key.get_pressed()
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if keys[pygame.K_u] and game_state == GAME_STATE_PAUSED:
game_state = GAME_STATE_RUNNING
pygame.mixer.unpause()
pygame.mixer.music.unpause()
return
def title_screen():
screen.blit(title_image, (0,0))
#======================================================================#
# GAME SETUP AND VARIABLES #
#======================================================================#
#setup colors
BROWN = 193,154,107
BLACK = 0,0,0
RED = 255,0,0
GREEN = 0,255,0
BLUE = 0,0,255
WHITE = 255,255,255
# Init pygame and set up the drawing window
pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize
pygame.init()
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 28)
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 900
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT], pygame.SRCALPHA, 32)
#setup player/ship variables and information
shipImg = pygame.image.load('spaceship5.png')
shipRect = shipImg.get_rect(topleft=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
ship_angle = 0
ship_delta_x = 0
ship_delta_y = 0
ship_friction = .98
ship_accel = 1.001
#score variables
player_score = 0
player_lives = 3
extra_life_score = 1000
max_lives_score = 3000
#missile information
missile_list = []
shot_time = .1 #time between sucessive shots
last_shot_time = time.time()
#Asteroid related variables
asteroid_list = []
X = 0
Y = 1
DELTA_X = 2
DELTA_Y = 3
COLOR = 4
SIZE = 5
ASTEROID_LARGE = 64
ASTEROID_MED = 16
ASTEROID_SMALL = 8
ASTEROID_SIZES =[ASTEROID_SMALL, ASTEROID_MED, ASTEROID_LARGE]
#constants to track game state
GAME_STATE_TITLE = 0
GAME_STATE_RUNNING = 1
GAME_STATE_END = 2
GAME_STATE_PAUSED = 3
GAME_STATE_QUIT = 4
game_state = GAME_STATE_TITLE #set initial game state
#LOAD GAME SOUNDS AND GRAPHICS
title_image = pygame.image.load('asteroidback.png')
background_image = pygame.image.load('background2.png')
fire_sound = pygame.mixer.Sound("fire.wav")
fire_sound.set_volume(.2)
bang_small = pygame.mixer.Sound("bangSmall.wav")
bang_med = pygame.mixer.Sound("bangMedium.wav")
thrust_sound = pygame.mixer.Sound("thrust.wav")
thrust_sound.set_volume(1)
bang_large = pygame.mixer.Sound("bangLarge.wav")
extra_ship_sound = pygame.mixer.Sound("extraShip.wav")
game_music = pygame.mixer.music.load('music.wav')
asteroid_small_image = pygame.image.load("asteroid_small.png")
asteroid_medium_image = pygame.image.load("asteroid_medium.png")
asteroid_large_image = pygame.image.load("asteroid_large.png")
#======================================================================#
# MAIN GAME LOOP #
#======================================================================#
while game_state != GAME_STATE_QUIT:
check_pygame_events()
if game_state == GAME_STATE_RUNNING:
move_objects()
check_bounds()
detect_collisions()
decelerate_player()
check_extra_life()
draw_scene()
elif game_state == GAME_STATE_TITLE:
title_screen()
elif game_state == GAME_STATE_PAUSED:
game_paused()
pygame.display.update()
clock.tick(30)
print("Thanks for playing!")
pygame.quit()
sys.exit()