forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceinvader.py
325 lines (261 loc) · 9.74 KB
/
spaceinvader.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
import pygame
from pygame import mixer
from pygame.locals import *
import random
pygame.font.init()
pygame.mixer.pre_init(44100, -16, 2, 512)
mixer.init()
# define fps
clock = pygame.time.Clock()
fps = 60
screen_width = 600
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Space Invaders")
# define fonts
font30 = pygame.font.SysFont('Constantia', 30)
font40 = pygame.font.SysFont('Constantia', 40)
# load sounds
explosion_fx = pygame.mixer.Sound("audio/explosion.wav")
explosion_fx.set_volume(0.25)
explosion2_fx = pygame.mixer.Sound("audio/explosion2.wav")
explosion2_fx.set_volume(0.25)
laser_fx = pygame.mixer.Sound("audio/laser.wav")
laser_fx.set_volume(0.25)
# define game variables
rows = 5
cols = 5
alien_cooldown = 1000 # bullet cooldown in milliseconds
last_alien_shot = pygame.time.get_ticks()
countdown = 3
last_count = pygame.time.get_ticks()
game_over = 0
# 0 is no game over,1 means player won,-1 means player lost
# define colours
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
# load images
bg = pygame.image.load("img/bg.png")
def draw_bg():
screen.blit(bg, (0, 0))
# define function for creating text
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
# create spaceship class
class Spaceship(pygame.sprite.Sprite):
def __init__(self, x, y, health):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/spaceship.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.health_start = health
self.health_remaining = health
self.last_shot = pygame.time.get_ticks()
def update(self):
# set movement speed
speed = 5
# set a cooldown variable
cooldown = 400 # milliseconds
game_over = 0
# get key presses
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= speed
if key[pygame.K_RIGHT] and self.rect.right < screen_width:
self.rect.x += speed
# record current time
time_now = pygame.time.get_ticks()
# shoot
if key[pygame.K_SPACE] == True and self.shot == False and time_now - self.last_shot > cooldown:
laser_fx.play()
bullet = Bullet(self.rect.centerx, self.rect.top)
self.shot = True
bullet_group.add(bullet)
self.last_shot = time_now
if key[pygame.K_SPACE] == False:
self.shot = False
# update mask{mask contains the img without the rect but with boundary of img that is all parts without transparency}
self.mask = pygame.mask.from_surface(self.image)
# draw health bar
pygame.draw.rect(
screen, red, (self.rect.x, (self.rect.bottom + 10),
self.rect.width, 12)
)
if self.health_remaining > 0:
pygame.draw.rect(
screen,
green,
(
self.rect.x,
(self.rect.bottom + 10),
int(self.rect.width * (self.health_remaining / self.health_start)),
12,
),
)
elif self.health_remaining <= 0:
explosion = Explosion(self.rect.centerx, self.rect.centery, 3)
explosion_group.add(explosion)
self.kill()
game_over = -1
return game_over
# create bullets class
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/bullet.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
def update(self):
self.rect.y -= 5 # speed of bullet
if self.rect.bottom < 0:
self.kill()
if pygame.sprite.spritecollide(self, alien_group, True):
self.kill()
explosion_fx.play()
explosion = Explosion(self.rect.centerx, self.rect.centery, 2)
explosion_group.add(explosion)
# create aliens class
class Aliens(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(
"img/alien" + str(random.randint(1, 5)) + ".png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.move_direction = 1
self.move_counter = 0
def update(self):
self.rect.x += self.move_direction
self.move_counter += 1
if abs(self.move_counter) > 75:
self.move_direction *= -1
self.move_counter *= self.move_direction
# create alien bullets class
class Alien_Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/alien_bullet.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
def update(self):
self.rect.y += 2 # speed of bullet
if self.rect.top > screen_height:
self.kill()
if pygame.sprite.spritecollide(
self, spaceship_group, False, pygame.sprite.collide_mask
):
self.kill()
explosion2_fx.play()
# reduce spaceship health
spaceship.health_remaining -= 1
explosion = Explosion(self.rect.centerx, self.rect.centery, 1)
explosion_group.add(explosion)
# create explosion class
class Explosion(pygame.sprite.Sprite):
def __init__(self, x, y, size):
pygame.sprite.Sprite.__init__(self)
self.images = []
for num in range(1, 6):
img = pygame.image.load(f"img/exp{num}.png")
if size == 1:
img = pygame.transform.scale(img, (20, 20))
if size == 2:
img = pygame.transform.scale(img, (40, 40))
if size == 3:
img = pygame.transform.scale(img, (160, 160))
# add image to the list
self.images.append(img)
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.counter = 0
def update(self):
explosion_speed = 3
# update explosion animation
self.counter += 1
if self.counter >= explosion_speed and self.index < len(self.images)-1:
self.counter = 0
self.index += 1
self.image = self.images[self.index]
# if the animation is complete,delete explosion
if self.index >= len(self.images)-1 and self.counter >= explosion_speed:
self.kill()
# create sprite groups
spaceship_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
alien_group = pygame.sprite.Group()
alien_bullet_group = pygame.sprite.Group()
explosion_group = pygame.sprite.Group()
def create_aliens():
for row in range(rows):
for items in range(cols):
alien = Aliens(100 + items * 100, 100 + row * 70)
alien_group.add(alien)
create_aliens()
# create player{3 bullet shots before spaceship dies}
spaceship = Spaceship(int(screen_width / 2), screen_height - 100, 3)
spaceship_group.add(spaceship)
run = True
while run:
clock.tick(fps)
# draw background
draw_bg()
if countdown == 0:
# create random alien bullets
# record current time
time_now = pygame.time.get_ticks()
# shoot {initially last alien shot is 0}
if (
time_now - last_alien_shot > alien_cooldown
and len(alien_bullet_group) < 5
and len(alien_group) > 0
) and game_over == 0:
attacking_alien = random.choice(alien_group.sprites())
alien_bullet = Alien_Bullet(
attacking_alien.rect.centerx, attacking_alien.rect.bottom
)
alien_bullet_group.add(alien_bullet)
last_alien_shot = time_now
# check if all the aliens have been destroyed
if len(alien_group) == 0:
game_over = 1
if game_over == 0:
# update spaceship
game_over = spaceship.update()
# update sprite groups
bullet_group.update()
alien_group.update()
alien_bullet_group.update()
else:
if game_over == -1:
draw_text('GAME OVER!', font40, white, int(
screen_width/2-100), int(screen_height/2+80))
if game_over == 1:
draw_text('YOU WON!', font40, white, int(
screen_width/2-100), int(screen_height/2+80))
if countdown > 0:
draw_text('GET READY!', font40, white, int(
screen_width/2-110), int(screen_height/2+80))
draw_text(str(countdown), font30, white, int(
screen_width/2-10), int(screen_height/2+130))
count_timer = pygame.time.get_ticks()
if count_timer-last_count > 1000: # 1sec has passed
countdown -= 1
last_count = count_timer
# update explosion group
explosion_group.update()
# draw sprite groups
spaceship_group.draw(screen)
bullet_group.draw(screen)
alien_group.draw(screen)
alien_bullet_group.draw(screen)
explosion_group.draw(screen)
# event handlers
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()