-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceBattleGame.py
executable file
·276 lines (233 loc) · 9.84 KB
/
spaceBattleGame.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
"""
spaceBattleGame
Destroy as many enemy spaceships as you can
Do not collide with enemy spaceships, or you will blow up!
Collect crates to get boost you spaceship's health, shield, or damage
(c) 2019 nigel5
"""
# IMPORT AND INITIALIZE
import pygame
import random
from SpriteLib.bullet import Bullet
from SpriteLib.enemyShip import EnemyShip
from SpriteLib.highscore import Highscore
from SpriteLib.instructions import Instructions
from SpriteLib.lifekeeper import Lifekeeper
from SpriteLib.playerShip import PlayerShip
from SpriteLib.powerup import Powerup
from SpriteLib.scorekeeper import Scorekeeper
import sys
import json
config = json.load(open("./settings.json"))
pygame.mixer.init()
pygame.init()
def main():
"""The main function handles the Game <--> Instruction screen loop"""
FPS = config.get("framerate")
RESOLUTION = (config.get("resolutionx"), config.get("resolutiony"))
screen = pygame.display.set_mode(RESOLUTION, pygame.FULLSCREEN if config.get("fullscreen") else 0)
pygame.display.set_caption("spaceBattleGame 2019")
new_score = 0
# Alternates between the game, and the starting screen until the player stops playing
while True:
if render_instructions(screen, new_score, FPS): # If the instructions are accepted then start a new game
new_score = render_game(screen, FPS)
else:
break
pygame.mixer.quit()
pygame.quit()
def render_instructions(screen, new_score, fps):
"""This function displays the starting screen, and handles if the player wants to play or quit.
Returns True if the player wants to play. Returns False if the player wants to quit."""
# DISPLAY
# screen
# ENTITIES
background = pygame.image.load("./Sprite/space.png").convert()
screen.blit(background, (0, 0))
instructions_label = Instructions(screen)
highscore_label = Highscore()
# Update the score file if new highscore
if new_score > highscore_label.get_latest_score():
highscore_label.new_highscore(new_score)
pygame.mixer.music.load("./Sound/imarch.wav")
pygame.mixer.music.play(-1)
# Instantiate 6 ships
allShips = []
for ship in range(6):
allShips.append(EnemyShip(screen))
allShips = pygame.sprite.Group(allShips)
allSprites = pygame.sprite.OrderedUpdates(allShips, highscore_label, instructions_label)
# ACTION
# ASSIGN
keepGoing = True
clock = pygame.time.Clock()
# LOOP
while keepGoing:
# TIME
if sys.platform == "darwin":
clock.tick_busy_loop(60)
else:
clock.tick(60)
# EVENTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pygame.mixer.music.fadeout(1)
return True
elif event.key == pygame.K_ESCAPE:
pygame.mixer.music.fadeout(1)
pygame.quit()
return False
# REFRESH
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
def render_game(screen, fps):
"""This is the main function that handles the main logic of the game. It will return the score that was acheived."""
# DISPLAY
# screen = screen
# ENTITIES
background = pygame.image.load("./Sprite/space.png").convert()
pygame.mixer.music.load("./Sound/laser2.wav")
pygame.mixer.music.set_volume(0.5)
gunStop = pygame.mixer.Sound("./Sound/laserstop1.wav")
gunStop.set_volume(1.0)
coolMusic = pygame.mixer.Sound("./Sound/music.wav")
coolMusic.set_volume(0.25)
coolMusic.play(-1)
enemyShoot = pygame.mixer.Sound("./Sound/laser1.wav")
enemyShoot.set_volume(1.0)
scorekeeper = Scorekeeper(screen)
lifekeeper = Lifekeeper(screen)
player = PlayerShip(screen)
enemyGroup = pygame.sprite.Group(EnemyShip(screen))
bulletGroup = pygame.sprite.Group()
powerupGroup = pygame.sprite.Group()
allSprites = pygame.sprite.OrderedUpdates(bulletGroup, powerupGroup, player, enemyGroup, scorekeeper, lifekeeper)
# ACTION
# ASSIGN
clock = pygame.time.Clock()
keepGoing = True
destroyedShips = 0
alreadyIncreased = False
deathCounter = 0
# LOOP
while keepGoing:
# TIME
if sys.platform == "darwin":
clock.tick_busy_loop(60)
else:
clock.tick(60)
# EVENTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pygame.mixer.music.play(-1)
elif event.key == pygame.K_ESCAPE:
keepGoing = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
pygame.mixer.music.stop()
gunStop.play()
# Player ship controls (keyboard)
if not (player.get_explode() or deathCounter):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.rotate(5)
if keys[pygame.K_RIGHT]:
player.rotate(-5)
if keys[pygame.K_SPACE]:
bulletGroup.add(Bullet(screen, player, "PLAYER"))
allSprites = pygame.sprite.OrderedUpdates(bulletGroup, powerupGroup, player, enemyGroup,
scorekeeper, lifekeeper)
if keys[pygame.K_a]:
player.move((-1, 0))
if keys[pygame.K_d]:
player.move((1, 0))
if keys[pygame.K_w]:
player.move((0, -1))
if keys[pygame.K_s]:
player.move((0, 1))
# if the player is exploding, or is about to be destroyed, then none of the below code will run
if not (player.get_explode() or deathCounter):
# If the player is hit by the enemy
hits = pygame.sprite.spritecollide(player, bulletGroup, False)
for bullet in hits:
if bullet.who_shot() == "ENEMY":
lifekeeper.take_damage(bullet.get_bullet_damage())
bullet.kill()
# If the Player collides with an enemy they take immense damage and be destroyed!
for enemy in pygame.sprite.spritecollide(player, enemyGroup, False):
if not enemy.get_explode():
player.explode()
deathCounter = 1
pygame.mixer.music.stop()
# Player dies
if lifekeeper.get_health_points() <= 0:
player.explode()
deathCounter = 1
pygame.mixer.music.stop()
# if the player collides with a powerup
hits = pygame.sprite.spritecollide(player, powerupGroup, False)
for crate in hits:
if crate.get_type() == 1:
lifekeeper.increase_health_points(100)
elif crate.get_type() == 2:
lifekeeper.increase_shield_points(500)
elif crate.get_type() == 3:
player.set_bullet_damage(player.get_bullet_damage() + 1)
crate.kill()
# If the enemy is hit by the player
for enemy in enemyGroup:
if not enemy.get_explode():
hits = pygame.sprite.spritecollide(enemy, bulletGroup, False)
for bullet in hits:
if bullet.who_shot() == "PLAYER":
enemy.decrease_health_points(bullet.get_bullet_damage())
bullet.kill()
# Enemy ships shoot
if random.randint(1, 35) == 1:
for enemy in enemyGroup:
enemyShoot.play()
bulletGroup.add(Bullet(screen, enemy, "ENEMY", player))
allSprites = pygame.sprite.OrderedUpdates(bulletGroup, powerupGroup, player, enemyGroup,
scorekeeper, lifekeeper)
# Check the health of the Enemies
for enemy in enemyGroup:
if enemy.get_health_points() < 0 and not enemy.get_explode():
scorekeeper.increase_score(enemy.get_value())
enemy.explode()
destroyedShips += 1
alreadyIncreased = False
# Increase maximum ships every 10 destroyed
if destroyedShips % 10 == 0 and not alreadyIncreased:
enemyGroup.add(pygame.sprite.Group(EnemyShip(screen)))
allSprites = pygame.sprite.OrderedUpdates(bulletGroup, powerupGroup, player, enemyGroup,
scorekeeper, lifekeeper)
alreadyIncreased = True
# Powerup generation
if random.randint(1, 500) == 250:
powerupGroup.add(Powerup(screen, random.randint(1, 3)))
allSprites = pygame.sprite.OrderedUpdates(bulletGroup, powerupGroup, player, enemyGroup,
scorekeeper, lifekeeper)
# Finish player explosion animations before returning
if deathCounter:
deathCounter += 1
if deathCounter == 33:
keepGoing = False
# REFRESH
screen.blit(background, (0, 0))
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
# Return the score
coolMusic.stop()
return scorekeeper.get_score()
# Call the main function
main()