-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpaceInvaders.py
392 lines (326 loc) · 13.6 KB
/
SpaceInvaders.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
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
import sys, pygame
from Player import player
from Bullet import bullet
from Enemy import enemy
from Text import text
from file_func import create_file, file_exists, write_on_file, read_file
from Wall import wall
from Menu import menu
import SoundEffect
pygame.init()
window_width = 670
window_height = 900
def check_for_inputs(bullets): #checks for inputs i think
global Player
global window
global window_width
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
Player.shoot(bullets)
if event.key == pygame.K_ESCAPE:
Menu.menu_loop(game_loop)
keys = pygame.key.get_pressed()
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
Player.move(0, window_width)
elif keys[pygame.K_d] or keys[pygame.K_RIGHT]:
Player.move(1, window_width)
def pauseEnemies(frames, enemies):
for column in enemies:
for enemy in column:
enemy[1].is_paused = True
enemy[1].pause_timer_length = frames
#turns all sprites red, of makes sprites stop being red and going back to normal
def turnAllRedOn_Off(on_or_off, enemies, texts):
# 0 = On
# 1 = Off
if on_or_off == 0:
for column in enemies:
for enemy in column:
enemy[1].isRed = True
for text in texts:
text.isRed = True
return
if on_or_off == 1:
for column in enemies:
for enemy in column:
enemy[1].isRed = False
for text in texts:
text.isRed = False
def draw_game_window(window, bullets, enemies, enemy_bullets, texts, score, hi_score, walls, deadEnemiesList):
global Player
window.fill((0,0,0))
for text in texts:
text.draw(window)
texts[0].update_text(f"SCORE: {score}")
texts[1].update_text(f"{Player.lifes}")
texts[2].update_text(f"HI-SCORE: {hi_score}")
for bullet in enemy_bullets:
bullet.draw(window,1)
for bullet in bullets:
bullet.draw(window,2)
for wall in walls:
wall.draw(window)
for deadEnemy in deadEnemiesList:
deadEnemy.updateDeadEnemy(deadEnemiesList.index(deadEnemy), deadEnemiesList, window)
for column in enemies:
for enemy in column:
enemy[1].draw(window)
for lifes in range(0, Player.lifes - 1):
if not Player.was_hit:
window.blit(pygame.transform.scale(pygame.image.load("Sprites/PlayerSpritev2.png"), (13 * Player.scale, 8 * Player.scale)), ((50 + 50 * lifes, window_height - 35), (13 * Player.scale, 8 * Player.scale)))
else:
window.blit(pygame.transform.scale(pygame.image.load("Sprites/PlayerSpritev2red.png"), (13 * Player.scale, 8 * Player.scale)), ((50 + 50 * lifes, window_height - 35), (13 * Player.scale, 8 * Player.scale)))
#pygame.draw.line(window, (255,0,0), (0, window_height - 50), (window_width, window_height - 50))
Player.draw(window)
pygame.display.update()
def check_for_bullets(bullets, enemies, deadEnemies,se): #checks if bullet hit the wall and if bullet hit enemy
score = 0
for bullet in bullets:
if bullet.y > 150 and bullet.y < window_height:
bullet.move(1)
else:
bullets.pop(bullets.index(bullet))
for column in enemies:
for enemy in column:
for bullet in bullets:
if is_colliding(bullet.rect, enemy[1].rect):
bullets.pop(bullets.index(bullet))
enemy[1].life -= 1
if enemy[1].life <= 0:
score += enemies[enemies.index(column)][column.index(enemy)][2]
#removing enemy from normal list and adding to deadEnemiesList
enemies[enemies.index(column)][column.index(enemy)][1].has_died = True
deadEnemy = enemies[enemies.index(column)][column.index(enemy)][1]
deadEnemy.rect[0] -= 6
deadEnemies.append(deadEnemy)
enemies[enemies.index(column)].pop(enemies[enemies.index(column)].index(enemy))
se.timerLength -= 1
se.invaderKilled.play()
pauseEnemies(8, enemies)
#updating normal enemy list stuff after removing enemy from list
if len(column) == 0:
enemies.pop(enemies.index(column))
update_columns_list(enemies)
update_enemy_speed(enemies)
update_shooter(enemies)
return score
def check_enemy_bullets(enemy_bullets, Player, enemies, texts): #checks if enemy bullet hit the wall or the player
for enemy_bullet in enemy_bullets:
if is_colliding(enemy_bullet.rect, Player.rect):
Player.damage(pauseEnemies, enemies, turnAllRedOn_Off, texts)
enemy_bullets.pop(enemy_bullets.index(enemy_bullet))
if enemy_bullet.y > 0 and enemy_bullet.y < window_height - 60:
enemy_bullet.move(2)
else:
enemy_bullets.pop(enemy_bullets.index(enemy_bullet))
def check_if_bullet_hit_bullet(enemy_bullets, player_bullets):
for enemy_bullet in enemy_bullets:
for player_bullet in player_bullets:
if is_colliding(enemy_bullet.rect, player_bullet.rect):
enemy_bullets.pop(enemy_bullets.index(enemy_bullet))
player_bullets.pop(player_bullets.index(player_bullet))
def enemy_hit_wall(enemies): #checks if one enemy hits the wall, and returns boolean value
hit = False
wall_distance = 25
for column in enemies:
for enemy in column:
if enemy[1].rect[0] + enemy[1].rect[2] > window_width - wall_distance or enemy[1].rect[0] < wall_distance:
hit = True
break
return hit
def create_enemies(won_counter):
x = 50
y = 230 + 45 * won_counter
arr = [[[0] for i in range(5)] for j in range(11)]
column = 0
element = 0
for columns in arr:
column += 1
x += 45
for elements in columns:
y += 45
element += 1
arr[column-1][element-1].append(enemy(x,y))
if element == 1:
arr[column-1][element-1][1].type = 2
arr[column-1][element-1].append(30)
elif element > 1 and element < 4:
arr[column-1][element-1][1].type = 1
arr[column-1][element-1].append(20)
else:
arr[column-1][element-1][1].type = 3
arr[column-1][element-1].append(10)
if element == 5:
y = 230 + 45 * won_counter
arr[column-1][element-1][0] = 1
element = 0
update_shooter(arr)
return arr
def is_colliding(rect1, rect2): # checks if 2 rects are colliding, useful as fuck
is_colliding = rect1.colliderect(rect2)
if is_colliding == 1:
return True
else:
return False
def update_enemy_speed(enemies): # updates enemy speed every time one enemy dies, if there is only 1 enemy left, he becomes sonic
for column in enemies:
for enemy in column:
enemy[1].speed += 0.1
enemy[1].timer_length *= 0.97
if len(enemies) == 1:
if len(column) == 1:
enemy[1].speed *= 1.3
enemy[1].timer_length = 2
def update_shooter(enemies): # sets "is_shooter" value based on the value in enemies list
for column in enemies:
for enemy in column:
if enemies[enemies.index(column)][column.index(enemy)][0] == 1:
enemy[1].is_shooter = True
else:
enemy[1].is_shooter = False
def check_for_game_over(Player, enemies):
if Player.lifes <= 0:
return True
for column in enemies:
for enemy in column:
if is_colliding(enemy[1].rect, Player.rect):
return True
return False
def check_for_win(enemies):
if enemies == []:
return True
return False
def update_columns_list(enemies): # checks if list needs to update the enemy shooter
column = -1
for columns in enemies:
column += 1
if enemies[column][len(columns)-1][0] != 1:
enemies[column][len(columns)-1][0] = 1
def create_walls(): # create the fucking walls
global window_width
y = 700
arr = []
space_between_walls = window_width / 5
for i in range(4):
arr.append(wall(space_between_walls*(i+1) - 17*3/2, y))
for Wall in arr:
Wall.create_pixel_rects()
return arr
def check_wall_colliding_with_bullet(walls, bullets): # checks if player bullets are colliding with wall and destroys wall
for wall in walls:
for row in wall.pixels:
for pixel in row:
for bullet in bullets:
if pixel[0] == 1 and is_colliding(bullet.rect, pixel[1]):
index = [wall.pixels.index(row), row.index(pixel)]
bullets.pop(bullets.index(bullet))
wall.destroy_nearby_pixels(index)
pixel[0] = 0
def check_wall_colliding_with_enemy_bullet(walls, enemy_bullets): # checks if enemy bullets are colliding with wall and destroys wall
for wall in walls:
for row in wall.pixels:
for pixel in row:
for enemy_bullet in enemy_bullets:
if pixel[0] == 1 and is_colliding(enemy_bullet.rect, pixel[1]):
index = [wall.pixels.index(row), row.index(pixel)]
enemy_bullets.pop(enemy_bullets.index(enemy_bullet))
wall.destroy_nearby_pixels(index)
pixel[0] = 0
def check_wall_colliding_with_enemies(walls, enemies):
for row in enemies:
for enemy in row:
if enemy[1].rect[1] >= 655 - 20:
for wall in walls:
for row in wall.pixels:
for pixel in row:
if pixel[0] == 1 and is_colliding(enemy[1].rect, pixel[1]):
pixel[0] = 0
def reset_player_variables(Player):
Player.lifes = 4
Player.was_hit = False
Player.not_draw = False
Player.x = 322
Player.rect[0] = 322
def game_loop():
global Player
global clock
global window
global se
texts = []
score = 0
if not(file_exists("hi_score.txt")):
create_file("hi_score.txt")
write_on_file("0", "hi_score.txt")
hi_score = read_file("hi_score.txt")
texts.append(text(f"SCORE: {score}", 120, 60, (0,255,255), (0,0,0), "pixelated.ttf", 45))
texts.append(text(f"{Player.lifes}", 25, 876, (0,255,255), (0,0,0), "pixelated.ttf", 28))
texts.append(text(f"HI-SCORE: {hi_score}", 470, 60, (0,0,255), (0,0,0), "pixelated.ttf", 45))
bullets = []
enemy_bullets = []
walls = create_walls()
can_move_down_timer = 60
won_counter = 0
enemies = create_enemies(won_counter)
deadEnemies = []
se = SoundEffect.SoundEffect()
while True:
se.playSong()
draw_game_window(window, bullets, enemies, enemy_bullets, texts, score, hi_score, walls, deadEnemies)
check_for_inputs(bullets)
check_if_bullet_hit_bullet(enemy_bullets, bullets)
score += check_for_bullets(bullets, enemies, deadEnemies, se)
check_enemy_bullets(enemy_bullets, Player, enemies, texts)
check_wall_colliding_with_enemies(walls, enemies)
if bullets != []:
check_wall_colliding_with_bullet(walls, bullets)
if enemy_bullets != []:
check_wall_colliding_with_enemy_bullet(walls, enemy_bullets)
if can_move_down_timer == 60:
enemy_move_down = enemy_hit_wall(enemies)
else:
can_move_down_timer += 1
for column in enemies:
for enemy in column:
enemy[1].check_move_down(enemies)
enemy[1].move(enemy_move_down, Player.x, texts)
enemy[1].shoot(enemy_bullets)
if enemy_move_down:
enemy_move_down = False
can_move_down_timer = 0
check_if_bullet_hit_bullet(enemy_bullets, bullets)
if check_for_win(enemies):
if won_counter < 3:
won_counter += 1
se.timer = 59
se.timerLength = 60
se.currentSongIndex = 3
walls = create_walls()
bullets = []
enemy_bullets = []
deadEnemies = []
enemies = create_enemies(won_counter)
if int(read_file("hi_score.txt")) < score:
write_on_file(str(score), "hi_score.txt")
hi_score = score
if check_for_game_over(Player, enemies):
reset_player_variables(Player)
turnAllRedOn_Off(1, enemies, texts)
enemies = create_enemies(won_counter)
walls = create_walls()
for t in texts:
t.isRed = False
bullets = []
enemy_bullets = []
deadEnemies = []
Menu.game_over = True
Menu.menu_loop(game_loop)
clock.tick(60)
clock = pygame.time.Clock()
Player = player(322, 780)
window = pygame.display.set_mode((window_width, window_height)) #creates window and stores window object
pygame.display.set_caption("Space Invaders") #sets up window name
Menu = menu()
Menu.menu_loop(game_loop)