-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.py
203 lines (158 loc) · 7.27 KB
/
GameManager.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
add_library('sound')
from Fruit import *
from Player import *
'''The GameManager class controls and stores important aspects of the game. It controls how the player and fruits move
as well as keeping track of time and score'''
class GameManager:
#Here we set up the GameManager
def __init__(self, bgimage, framerate, playerimage, timer, fruitimage, itemCatchMusicPlayer, backgroundMusicPlayer):
#The game background image
self.backgroundimage = bgimage
#The player image
self.playerimage = playerimage
#The game framerate in frames/second
self.framerate = framerate
#The game timer that will help control how long the game lasts
self.timer = timer
#This boolean will help us check if the game is done (the timer ran out)
self.done = False
#We can load custom fonts to display the fruit points and total score
self.scorefont = loadFont("Sansation-Bold-35.vlw");
self.pointsfont = loadFont("Sansation-Bold-35.vlw");
#The total score so far
self.score = 0
#How "fast" the fruits will fall
self.fruitspeed = 7
#The time that will be used to spawn fruits
self.time = 0
#This is a list that will store the fruits
self.items = []
#How many points the fruits are worth
self.pointsValue = 0
#Where the fruit points will appear once a fruit is caught
self.pointsx = 0
self.pointsy = 0
#Checks whether the fruit points should be displayed
self.pointsOn = False
#How many frames the fruit points should be displayed for
self.pointsFrameCount = 0
#How long the game will last in seconds
self.gameDurationSeconds = 30
#The fruit image
self.fruitimage = fruitimage
#The background and caught fruit sounds
self.itemCatchMusicPlayer = itemCatchMusicPlayer
self.backgroundMusicPlayer = backgroundMusicPlayer
#Set up the game's player
#Adjust where the player will be located on the drawing canvas here
self.player = Player(self.playerimage, width/2, height - 100)
#This calls all the methods that will be needed to make the game run
def playGame(self):
#Increase the difficulty (speed of fruit) over time
self.difficultyChange()
#Call a class function here to make the fruit "spawn"
#PUT SOME CODE HERE
#Call a class function here to make the fruit fall or move
#PUT SOME CODE HERE
#Draw the player, fruits, and points
self.drawWorld()
#Check if the game has ended
self.checkEnd()
#Check if the player has caught a fruit
self.checkCollision()
#Show the score and current fruit speed
self.displayScore()
self.displaySpeed()
#Every time this is used add 1 second to the game time
self.time = self.time + 1
#Here we draw all the fruits and player on our game screen
def drawWorld(self):
#draw all the fruits that have been spawned by looping through the items list
for i in range(len(self.items)):
aFruit = self.items[i]
aFruit.draw()
#draw the player on the screen
self.player.draw()
#Here we create fruits and add them to the items list
def spawn(self):
#Control how often the fruits will spawn
if(self.time == self.framerate / 2):
#reset the fruit spawn time
self.time = 0
#Create a fruit with a random x location
aFruit = Fruit(self.fruitimage, int(random(30,470)), self.fruitspeed, 3)
#CHALLENGE1: Add a new type of fruit
#CHALLENGE2: Add a powerup
#Add the fruit to the list
self.items.append(aFruit)
#Here we move the fruits by using their move() method
def moveFruit(self):
#Finish this for loop to make the fruits fall
'''for i in :
#inside the loop
aFruit = self.items[i]
aFruit.move()'''
#Here we check if the game has ended by checking the time
#How could we change this to end the game by reaching a certain score?
def checkEnd(self):
#If the elapsed time has reached the game duration time
if(self.timer.currentTime() == self.gameDurationSeconds):
self.done = True
self.timer.pause()
#Here we increase the speed of the fruits falling every 5 seconds that pass
#What other ways could difficulty increase?
def difficultyChange(self):
#Increase the difficulty every 5 seconds
if(self.timer.currentTime() % 5 == 0 and self.timer.currentTime() != 0 and frameCount % self.framerate == 0 and frameCount != 0):
self.fruitspeed = self.fruitspeed + 1
#Here we check if the player has collide with or caught a fruit
def checkCollision(self):
#store items you want removed in this list, never a good idea to modify a list you are iterating over in python
itemsToRemove = []
#loop through the fruits
#We need to check every fruit to see if it is touching the player
for i in range(len(self.items)):
aFruit = self.items[i]
#If the player caught a fruit
#Display the fruit points
'''if():
self.score = self.score + aFruit.value
self.pointsValue = aFruit.value
self.pointsx = aFruit.xlocation
self.pointsy = aFruit.ylocation
self.pointsOn = True
#remove the fruit from the items list
itemsToRemove.append(aFruit)
#print("CAUGHT!")
self.itemCatchMusicPlayer.play()
self.displayPoints(self.pointsValue, self.pointsx, self.pointsy)'''
#remove caught fruits from the list
for i in range(len(itemsToRemove)):
itemToRemove = itemsToRemove[i]
self.items.remove(itemToRemove)
#Here we show the fruit points
def displayPoints(self, value, x, y):
if(self.pointsOn and self.pointsFrameCount < 90):
textFont(self.pointsfont, 30)
fill(255)
text("+" + str(value), x, y)
self.pointsFrameCount = self.pointsFrameCount + 1
else:
self.pointsOn = False
self.pointsFrameCount = 0
#Here we show the fruit speed
def displaySpeed(self):
fill(255)
textFont(self.scorefont, 20)
text("Speed: " + str(self.fruitspeed), 10,31)
#Here we show the total score
def displayScore(self):
fill(255)
textFont(self.scorefont, 20)
text("Score: " + str(self.score), 10,15)
#Here we show the game over message
def displayGameOverMessage(self):
textFont(self.scorefont, 40)
text("Good Job!",10,40)
textFont(self.scorefont,20)
text("SCORE: " + str(self.score),10,70)