forked from MasterCash/donkey-kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
210 lines (164 loc) · 6.16 KB
/
game.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
"""
Game Manager
"""
import os
import uuid
from enum import Enum
from utils import AbstractMethod, DefaultMethod, Singleton
from inputManager import InputManager
from collisionDetector import CollisionDetector, CollisionTypes, CollectionTypes
from framework import SpriteGroup, Window, Clock, Music, GameLevelManager, Events, Sound, GameCollectible
class GameState(Enum):
MainMenu = 0
Playing = 1
Paused = 2
DeathScreen = 3
@Singleton
class GameManager:
""" Class to manage the state of the game """
def __init__(self, window):
self._window = window
self._objects = SpriteGroup()
self._players = SpriteGroup()
self._enemies = SpriteGroup()
self._collectibles = SpriteGroup()
self._levelManager = None
Music.newBackground('06_Stage_1_BGM')
self.state = GameState.Playing
self._playing = True
self._victory = False
def play(self):
""" Main Game Loop """
if self._levelManager is None:
raise Exception("No Level Manager")
self._spawnPlayers()
while self._playing:
Clock.forceFPS(60)
# Game Routine
self._checkForDeath()
self._checkForVictory()
self._checkForLoss()
self._handleEvents()
self._update()
self._collisionCheck()
self._draw()
# Clear everything
self._players.empty()
self._collectibles.empty()
self._enemies.empty()
self._objects.empty()
os._exit(-1)
return self._victory
def addPlayer(self, player):
""" Adds a player to the game """
self._players.add(player)
return self
def addObject(self, obj):
""" Adds an object to the game """
self._objects.add(obj)
return self
def addEnemy(self, enemy):
""" Adds an enemy to the game """
self._enemies.add(enemy)
return self
def addCollectible(self, collectible):
""" Adds a collectible item to the game """
if not isinstance(collectible, GameCollectible):
return self
self._collectibles.add(collectible)
return self
def addLevelManager(self, obj):
""" Sets the thing used for generating levels """
if not isinstance(obj, GameLevelManager):
return self
self._levelManager = obj
self._levelManager.setWindowInformation(self._window.width, self._window.height)
return self
def _handleEvents(self):
""" Handles events from PyGame """
if self.state == GameState.DeathScreen:
return
Events.handleEvents()
InputManager.handleInput()
def _update(self):
""" Updates everything """
if self.state == GameState.DeathScreen:
return
self._objects.update()
self._players.update()
self._enemies.update()
self._collectibles.update()
self._levelManager.update()
def _collisionCheck(self):
""" Checks for collisions """
if self.state == GameState.DeathScreen:
return
CollisionDetector.check(self._players, self._levelManager.ladders, CollisionTypes.Ladder)
CollisionDetector.check(self._players, self._levelManager.platforms, CollisionTypes.Platform)
CollisionDetector.check(self._players, self._levelManager.immovables, CollisionTypes.Immovable)
CollisionDetector.check(self._players, self._levelManager.walls, CollisionTypes.Wall )
CollisionDetector.check(self._players, self._enemies, CollisionTypes.Enemy)
CollisionDetector.checkCollection(self._collectibles, self._players, CollectionTypes.Player)
CollisionDetector.checkCollection(self._collectibles, self._enemies, CollectionTypes.Enemy)
CollisionDetector.check(self._enemies, self._levelManager.ladders, CollisionTypes.Ladder)
CollisionDetector.check(self._enemies, self._levelManager.platforms, CollisionTypes.Platform)
CollisionDetector.check(self._enemies, self._levelManager.immovables, CollisionTypes.Immovable)
CollisionDetector.check(self._enemies, self._levelManager.walls, CollisionTypes.Wall )
def _draw(self):
""" Draws everything on the window """
self._levelManager.draw(self._window)
self._objects.draw(self._window)
self._collectibles.draw(self._window)
self._players.draw(self._window)
if self.state != GameState.DeathScreen:
self._enemies.draw(self._window)
self._window.flip()
def _checkForDeath(self):
death = False
# Check death of a player
for player in self._players:
if player.isDying:
death = True
break
# Check death of an enemy
"""if death == False:
for enemy in self._enemies:
if enemy.isDying:
death = True
break
"""
if death:
self.state = GameState.DeathScreen
if len(self._players) == 1:
self._enemies.empty()
# Remove clearable collectibles
for collectible in self._collectibles:
if collectible.canBeCleared:
collectible.kill()
else:
self.state = GameState.Playing
def _checkForVictory(self):
self._victory = False
for player in self._players:
self._victory = self._levelManager.isLevelCompleted(player)
if self._victory:
break
self._playing = not self._victory
return self._victory
def _checkForLoss(self):
if len(self._players) == 0:
self._victory = False
self._playing = False
def _quit(self, data):
""" Closes the window """
self._window.close()
os._exit(0)
def _spawnPlayers(self):
# Set the players spawn locations
locations = self._levelManager.getSpawnLocations()
i = 0
for player in self._players:
player.spawn(locations[i][0], locations[i][1])
i = i + 1
if i >= len(locations):
i = 0