/
grapes.py
executable file
·333 lines (258 loc) · 11.6 KB
/
grapes.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
326
327
328
329
330
331
332
333
#!/usr/bin/python
import pygame
import sys
from gi.repository import Gtk
from entity import Entity
from bucket import Bucket
from grape import Grape
from background import Background
import random
class grapes:
def __init__(self, debug):
# Set up a clock for managing the frame rate.
self.clock = pygame.time.Clock()
self.state = 'START'
# Setup game variables
# self.background = Background(0, 0)
# self.bucket = Bucket(-100, 100)
self.grapes = []
self.spawnCount = 0
self.changeGoalCount = 0
self.paused = False
self.debug = debug
# Begin button variables
self.startButtonX = 0
self.startButtonY = 0
self.startButtonSurface = None
self.startButtonWidth = 0
self.startButtonHeight = 0
# Setup current level variables
self.level = 0
self.score = 0
self.totalScore = 0
self.goalScore = 0
self.spawnTime = 0
self.goalResetTime = 0
self.grapeVelocity = 0
self.maxGrapesPerTick = 0
# Setup goal variables
self.currentVerts = -1
self.currentDisplayGrape = None
def set_paused(self, paused):
self.paused = paused
# Called to save the state of the game to the Journal.
def write_file(self, file_path):
pass
# Called to load the state of the game from the Journal.
def read_file(self, file_path):
pass
# Takes the player to the next level
def nextLevel(self):
# Increment total score
self.totalScore += self.score
# Increment the level and reset the level score
self.level += 1
self.score = 0
# Calculate the goal score
self.goalScore = self.level * self.level * 40
# Determine level index
index = ((self.level - 1) % Background.TOTAL_LEVELS) + 1
# Calculate spawn rate, goal change rate, and fall speed
maxCalcLevel = 15
calcLevel = self.level - 1
if calcLevel > maxCalcLevel:
calcLevel = maxCalcLevel
self.spawnTime = 45 - int((calcLevel * 3.5))
self.goalResetTime = 270 - int((calcLevel * 2))
self.grapeVelocity = 5 + int((calcLevel * 1.5))
self.maxGrapesPerTick = 1 + int((calcLevel / 1.5))
if self.spawnTime < 10:
self.spawnTime = 10
if self.goalResetTime < 30:
self.goalResetTime = 30
if self.grapeVelocity > 17:
self.grapeVelocity = 17
if self.maxGrapesPerTick > 5:
self.maxGrapesPerTick = 5
# Start the music
pygame.mixer.music.stop()
pygame.mixer.music.load("assets/levels/" + str(index) + "/music.ogg")
pygame.mixer.music.play(-1) # Loop the music
# Generate first goal
self.generateNewGoal()
# Generate a new goal for the player
def generateNewGoal(self):
self.currentVerts = random.randint(Grape.MIN_VERTS, Grape.MAX_VERTS)
self.currentDisplayGrape = Grape(40, 10 + 26 + 80, self.currentVerts, 0)
self.currentDisplayGrape.color = (25, 252, 0)
self.randMod = random.randint(1, 5)
# Spawns a grape
def spawnGrape(self, width, offsetIndex):
# Don't spawn grapes off the edge of the screen
self.grapes.append(Grape(random.randrange(Grape.DEFAULT_RADIUS, width - Grape.DEFAULT_RADIUS), -Grape.DEFAULT_RADIUS * (offsetIndex + 1), random.randint(Grape.MIN_VERTS, Grape.MAX_VERTS), self.grapeVelocity))
# The main game loop.
def run(self):
self.running = True
screen = pygame.display.get_surface()
# These needed to be moved for the activity to work
self.background = Background(0, 0)
self.bucket = Bucket(-100, 100)
# Load the font
self.font = pygame.font.SysFont("monospace", 33)
self.juiceFont = pygame.font.SysFont("monospace", 30)
self.titleFont = pygame.font.SysFont("monospace", 120)
# Mixer setup
pygame.mixer.init()
# Sound setup
self.squishEffect = pygame.mixer.Sound('assets/squish.wav')
self.incorrectEffect = pygame.mixer.Sound('assets/incorrect.wav')
# Start the first level
self.nextLevel()
while self.running:
# Pump GTK messages.
while Gtk.events_pending():
Gtk.main_iteration()
pos = pygame.mouse.get_pos()
# Pump PyGame messages.
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEMOTION and self.state == 'GAME':
x, y = pos
# Center the bucket
x -= self.bucket.sprite.get_width() / 2
self.bucket.setPos(x, screen.get_height() * 0.8)
elif event.type == pygame.KEYDOWN: # Shortcut to next level
if self.debug and event.key == pygame.K_n:
self.nextLevel()
elif event.key == pygame.K_p: # Toggle pause status
self.set_paused(not self.paused)
elif event.type == pygame.MOUSEBUTTONDOWN and self.state == 'START':
x, y = pos
width, height = self.titleFont.size("Begin")
if x > self.startButtonX and x < self.startButtonX + self.startButtonWidth and y > self.startButtonY and y < self.startButtonY + self.startButtonHeight:
self.state = 'GAME'
if self.state == 'START':
self.background.draw(1, screen, False);
titleText = "Grapes of Math"
(titleWidth, titleHeight) = self.titleFont.size(titleText)
title = self.titleFont.render(titleText, 1, (200, 200, 200))
screen.blit(title, (screen.get_width() / 2 - (titleWidth / 2), 50))
startText = "Begin"
(self.startButtonWidth, self.startButtonHeight) = self.titleFont.size(startText)
# Only generate this the first draw
if self.startButtonX == 0:
overlayColor = (0, 0, 0, 127)
overlayRect = pygame.Rect(0, 0, self.startButtonWidth, self.startButtonHeight)
overlaySurface = pygame.Surface((300, 160), pygame.SRCALPHA)
overlaySurface.fill(overlayColor, overlayRect)
self.startButtonX = (screen.get_width() / 2 - (self.startButtonWidth / 2))
self.startButtonY = 200
screen.blit(overlaySurface, (self.startButtonX, self.startButtonY))
startButton = self.titleFont.render(startText, 1, (200, 200, 200))
screen.blit(startButton, (self.startButtonX, self.startButtonY))
elif self.state == 'GAME':
if not self.paused:
# Spawn Grapes
if self.spawnCount > random.randrange(self.spawnTime - 5, self.spawnTime):
for i in range(0, random.randint(1, self.maxGrapesPerTick)):
self.spawnGrape(screen.get_width(), i)
self.spawnCount = 0
self.spawnCount += 1
# Change goal
if self.changeGoalCount > random.randrange(self.goalResetTime - 7, self.goalResetTime):
self.generateNewGoal()
self.changeGoalCount = 0
self.changeGoalCount += 1
# Clear Display
screen.fill((255, 255, 255)) # 255 for white
# Draw the background
self.background.draw(self.level, screen, True)
# Draw paused text if paused
if self.paused:
pauseText = "Paused"
(pauseWidth, pauseHeight) = self.titleFont.size(pauseText)
pauseLabel = self.titleFont.render(pauseText, 1, (255, 255, 255))
pauseX = (screen.get_width() / 2) - (pauseWidth / 2)
pauseY = (screen.get_height() / 2) - (pauseHeight / 2)
screen.blit(pauseLabel, (pauseX, pauseY))
# Draw the bucket
self.bucket.draw(screen)
clone = list(self.grapes)
for i, g in enumerate(clone):
if not self.paused:
g.falling = True
g.update()
g.draw(screen)
if self.bucket.catchGrape(g.x, g.y, g.r):
# Delete the grape
del self.grapes[i]
# Check if the grape is correct
if g.numVerts == self.currentVerts:
self.score += int(g.value * 1.5)
self.squishEffect.play()
if self.score >= self.goalScore:
self.nextLevel()
else:
self.score -= g.value / 3
if self.score < 0:
self.score = 0
self.incorrectEffect.play()
else:
g.draw(screen)
# Text drawing
textX = 16
textY = 16
# Draw the current level text
label = self.font.render("Level " + str(self.level), 1, (176, 229, 255))
screen.blit(label, (textX, textY))
textY += 26
# Draw the score
label = self.juiceFont.render("Grape Juice: " + str(self.score) + " / " + str(self.goalScore), 1, (219, 140, 213))
screen.blit(label, (textX, textY))
textY += 26;
# Draw the current goal
levelText = "Collect grapes with " + str(self.currentVerts) + " sides"
if self.level == 4:
levelText = "Collect grapes with " + str(self.currentVerts + self.randMod) + ' - ' + str(self.randMod) + " sides"
label = self.juiceFont.render(levelText, 1, (162, 252, 151))
screen.blit(label, (textX, textY))
# Only draw on level one
if self.level == 1:
# Draw the current goal
self.currentDisplayGrape.draw(screen)
# Flip Display
pygame.display.flip()
# Try to stay at 30 FPS
self.clock.tick(30)
# This function is called when the game is run directly from the command line:
# ./TestGame.py
def main():
# Initalize pygame
pygame.init()
# This is the resolution of the XO
xo_screen_width = 1200
xo_screen_height = 900
# XO Mode will make the screen a fixed size
# so the background fills up the screen
xo_mode = True
# Is debugging enabled
debug = False
# Check for low resolution mode (good for testing)
if len(sys.argv) > 1 and sys.argv[1] == "-lowres":
pygame.display.set_mode((800, 600), pygame.RESIZABLE)
debug = True
elif xo_mode:
pygame.display.set_mode((xo_screen_width, xo_screen_height), pygame.RESIZABLE)
else:
pygame.display.set_mode((0, 0), pygame.RESIZABLE)
# Set the window title
pygame.display.set_caption("Grapes of Math")
# Create an instance of the game
game = grapes(debug)
# Start the game
game.run()
if __name__ == '__main__':
main()