-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameContainer.py
More file actions
177 lines (147 loc) · 6.59 KB
/
GameContainer.py
File metadata and controls
177 lines (147 loc) · 6.59 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
import pygame, random
from Player import Spaceship
from Obstacle import Barrier
from Obstacle import grid
from Aliens import Alien
from Lasers import Laser
from Aliens import MysteryShip
#defines all variables
class Game:
def __init__(self, screenWidth, screenHeight, offset):
self.screenWidth = screenWidth
self.screenHeight = screenHeight
self.Offset = offset
self.spaceshipGroup = pygame.sprite.GroupSingle()
self.spaceshipGroup.add(Spaceship(self.screenWidth, self.screenHeight, self.Offset))
self.obstacles = self.createObstacles()
self.alienGroup = pygame.sprite.Group()
self.createAliens()
self.alienDirection = 1
self.alienLaserGroup = pygame.sprite.Group()
self.mysteryShipGroup = pygame.sprite.GroupSingle()
self.lives = 3
self.run = True
self.score = 0
self.highScore = 0
self.loadHighScore()
pygame.mixer.music.load("Arcade Game Music Type Beat (RDCworld1 Outro) [TubeRipper.com].ogg")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
self.explosion_sound = pygame.mixer.Sound("Sounds_explosion.ogg")
self.explosion_sound.set_volume(0.2)
self.numOfAliens = 0
#function to creates the obstacles with the same gap between all 4
def createObstacles(self):
obstacle_width = len(grid[0]) * 3
gap = (self.screenWidth + self.Offset - (4 * obstacle_width)) / 5
obstacles = []
for i in range(4):
xOffset = (i + 1) * gap + i * obstacle_width
obstacle = Barrier(xOffset, self.screenHeight - 100)
obstacles.append(obstacle)
return obstacles
#function to spawns in the aliens in organised rows
def createAliens(self):
for row in range(5):
for column in range(11):
x = 75 + column * 55
y = 110 + row * 55
if row == 0:
alien_type = 3
elif row in (1,2):
alien_type = 2
else:
alien_type = 1
alien = Alien(alien_type, x + self.Offset / 2, y)
self.alienGroup.add(alien)
#function to move the aliens
def alienMovement(self):
self.alienGroup.update(self.alienDirection)
alien_sprites = self.alienGroup.sprites()
for alien in alien_sprites:
if alien.rect.right >= self.screenWidth + self.Offset / 2:#when they hit a wall they move down
self.alienDirection = -1
self.alienMoveDown(2)
elif alien.rect.left <= self.Offset / 2:
self.alienDirection = 1
self.alienMoveDown(2)
#function to move the aliens down
def alienMoveDown(self, distance):
if self.alienGroup:
for alien in self.alienGroup.sprites():
alien.rect.y += distance
#function to make the aliens shoot randomly
def shootAlienLaser(self):
if self.alienGroup.sprites():
random_alien = random.choice(self.alienGroup.sprites())
laser_sprite = Laser(random_alien.rect.center, -6, self.screenHeight)
self.alienLaserGroup.add(laser_sprite)
#function to spawn in the mystery ship
def createMysteryShip(self):
self.mysteryShipGroup.add(MysteryShip(self.screenWidth, self.Offset))
#function to constantly check for a collision and act upon it
def checkForCollisions(self):
if self.spaceshipGroup.sprite.laserGroup:
for laser_sprite in self.spaceshipGroup.sprite.laserGroup:
aliens_hit = pygame.sprite.spritecollide(laser_sprite, self.alienGroup, True)
if aliens_hit:
self.explosion_sound.play()
for alien in aliens_hit:
self.score = self.score + alien.type * 100#adds score to the score each kill
self.checkForHighScore()
laser_sprite.kill()
self.numOfAliens = self.numOfAliens - 1
aliens_hit = pygame.sprite.spritecollide(laser_sprite, self.mysteryShipGroup, True)
if aliens_hit:
for alien in aliens_hit:
self.score = self.score + 600
self.explosion_sound.play()
self.checkForHighScore()
laser_sprite.kill()
self.numOfAliens = self.numOfAliens - 1
for obstacle in self.obstacles:
if pygame.sprite.spritecollide(laser_sprite, obstacle.blocks_group, True):
laser_sprite.kill()#kills each section of the obstacle hit
if self.alienLaserGroup:#checks if lives are 0
for laser_sprite in self.alienLaserGroup:
if pygame.sprite.spritecollide(laser_sprite, self.spaceshipGroup, False):
laser_sprite.kill()
self.lives -= 1
if self.lives == 0:
self.gameOver()
for obstacle in self.obstacles:
if pygame.sprite.spritecollide(laser_sprite, obstacle.blocks_group, True):
laser_sprite.kill()
if self.alienGroup:
for alien in self.alienGroup:
for obstacle in self.obstacles:
pygame.sprite.spritecollide(alien, obstacle.blocks_group, True)
if pygame.sprite.spritecollide(alien, self.spaceshipGroup, False):
self.gameOver()
#function to end the game
def gameOver(self):
self.run = False
#function to reset all attributes
def reset(self):
self.run = True
self.lives = 3
self.spaceshipGroup.sprite.reset()
self.alienGroup.empty()
self.alienLaserGroup.empty()
self.createAliens()
self.mysteryShipGroup.empty()
self.obstacles = self.createObstacles()
self.score = 0
#function to update and store the highscore
def checkForHighScore(self):
if self.score > self.highScore:
self.highScore = self.score
with open("highscore.txt", "w") as file:
file.write(str(self.highScore))
#function to load the highscore to be displayed
def loadHighScore(self):
try:
with open("highscore.txt", "r") as file:
self.highScore = int(file.read())
except FileNotFoundError:
self.highScore = 0