-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceInvaders.py
More file actions
120 lines (96 loc) · 3.25 KB
/
SpaceInvaders.py
File metadata and controls
120 lines (96 loc) · 3.25 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
import pygame as pg
from pygame.mixer import Sound
import random as rd
from Player import Player , PLAYER_TURNING_SPEED
from Asteroid import Asteroid
from Laser import Laser , LASER_COOLDOWN
from utils import load_image , load_sound , SCREEN_WIDTH , SCREEN_HEIGHT
# Create game window
screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pg.display.set_caption('SpaceInvaders')
pg.display.set_icon(pg.transform.scale(load_image("Player.png"), (32, 32)))
pg.mouse.set_visible(False)
# Add music
pg.mixer.init()
pg.mixer.music.load('data\\battle.wav')
pg.mixer.music.play(-1)
# Laser sound
pew_sound = pg.mixer.Sound("data\\pew.wav")
# UI font
pg.font.init()
my_font = pg.font.SysFont('Comic Sans MS', 40)
# Load and scale background
background1 = load_image("BackGround.png")
background1 = pg.transform.scale(background1, (SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate the player in the middle of the screen
player = Player((SCREEN_WIDTH/2,SCREEN_HEIGHT/2))
player_group = pg.sprite.Group()
player_group.add(player)
asteroid_timer = 0
asteroid_group = pg.sprite.Group()
# test_asteroid = Asteroid((300,300))
# asteroid_group.add(test_asteroid)
laser_timer = 0
laser_group = pg.sprite.Group()
# test_laser = Laser(player)
# laser_group.add(test_laser)
clock = pg.time.Clock()
dt = 0
running = True
game_over_timer = 0
pg.init()
# Main loop
while running:
# Event loop
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE and laser_timer <= 0 and player.alive(): # SHOOT!! PEW PEW PEW
laser_group.add(Laser(player))
laser_timer = LASER_COOLDOWN
pew_sound.play()
if pg.key.get_pressed()[pg.K_w]: # W - accelerate
player.accelerate()
if pg.key.get_pressed()[pg.K_a]: # A - turn counter-clockwise
player.angle += PLAYER_TURNING_SPEED
if pg.key.get_pressed()[pg.K_d]: # D - turn clockwise
player.angle -= PLAYER_TURNING_SPEED
# Background
screen.blit(background1,(0,0))
# screen.fill((255,255,255))
# print(f"inert: {player.inert}") # print current inertia magnitude
if asteroid_timer <= 0:
asteroid_timer = rd.randrange(1000,4000)
asteroid_group.add(Asteroid())
else:
asteroid_timer -= dt
if laser_timer > 0:
laser_timer -= dt
# check collisions
for asteroid in asteroid_group:
# player dies
if player.collides_with(asteroid):
player.kill()
game_over_timer = 2000
# asteroid is hit
for laser in laser_group:
if laser.collides_with(asteroid):
asteroid.split()
laser.kill()
asteroid.kill()
laser_group.draw(screen)
laser_group.update()
asteroid_group.draw(screen)
asteroid_group.update()
player_group.draw(screen)
player_group.update(screen)
if game_over_timer:
text_surface = my_font.render('Game Over', False, (255,255,255))
screen.blit(text_surface, (SCREEN_WIDTH/2-100,SCREEN_HEIGHT/2-50))
game_over_timer -= dt
if game_over_timer <= 20:
running = False
pg.display.flip()
dt = clock.tick(120) # framerate
pg.quit()