forked from omiguelgomes/FEUP-IART
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
346 lines (268 loc) · 12.2 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
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
334
335
336
337
338
339
340
341
342
343
344
345
from pygame.sprite import groupcollide
from AI import *
from bubble import *
windowWidth = 100*colNr
windowHeight = 100*rowNr
center = (windowWidth/2, windowHeight/2)
class Game():
def __init__(self, levels):
self.win = pygame.display.set_mode((windowWidth, windowHeight + scoreVerticalSpace))
self.bubbles = pygame.sprite.Group()
self.score = 0
self.projectiles = pygame.sprite.Group()
self.levels = levels
pygame.init()
pygame.display.set_caption("Bubble blast")
def setLevel(self, level):
self.startGrid = self.levels[level-1][1]
self.touchesLeft = self.levels[level-1][2]
self.solution = self.levels[level-1][3]
self.createBubbles()
#create list of bubbles
def createBubbles(self):
# create bubble list
for i in range(0, len(self.startGrid[0])):
for j in range(0, len(self.startGrid)):
# dont create level 0 balls
if self.startGrid[j][i] > 0:
self.bubbles.add(Bubble((i, j), self.startGrid[j][i]))
#Recieves list of key presses to check for
def checkInputs(self, options):
# check mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONUP:
return pygame.mouse.get_pos()
if event.type == pygame.KEYDOWN:
if event.key in options:
return event.key
def checkReturnToMenu(self):
while True:
self.writeToScreen((center[0], center[1] + 40), "Y - Go back to main menu", False)
self.writeToScreen((center[0], center[1] + 80), "Q - Quit game", False)
pygame.display.flip()
input = self.checkInputs([pygame.K_y, pygame.K_q])
if input == pygame.K_y : return True
elif input == pygame.K_q:
pygame.quit()
quit()
#decrements clicked bubble's level, creates projectiles if the bubble explodes (if it's a level 1 bubble)
def makeMove(self, bubble):
self.ai.grid = self.ai.clickBubble(self.ai.grid, (bubble.pos[1], bubble.pos[0]))
self.ai.touchesLeft -= 1
# if bubble is clicked, make it get hit and decrement touchesLeft
newExplosion = bubble.hit()
# if bubble explodes (level 1), add explosions
if newExplosion != None:
self.score += bubble.score
self.bubbles.remove(bubble)
self.projectiles.add(newExplosion[0][0])
self.projectiles.add(newExplosion[0][1])
self.projectiles.add(newExplosion[0][2])
self.projectiles.add(newExplosion[0][3])
#logic for human mode
def playHuman(self):
gameOver = False
hint = False
self.ai = AI(self.startGrid, self.touchesLeft)
while (not gameOver):
self.update()
# avoid clicking before move ends
if len(self.projectiles) <= 0 and self.touchesLeft > 0:
# check mouse/kbd events
input = self.checkInputs([pygame.K_h])
if input == pygame.K_h:
self.win.fill((0, 0, 0))
pygame.display.flip()
moves = self.ai.iddfs_algorithm()
pos = 0
if type(moves) == None:
self.writeToScreen(center, "No solution exists for the current state :(", False)
pygame.display.flip()
else:
for move in moves:
self.writeToScreen((center[0], center[1] + pos), str(move), False)
pygame.display.flip()
pos += 40
pygame.time.wait(5000)
elif input != None:
for bubble in self.bubbles:
#if a bubble was clicked
if bubble.rect.collidepoint(input[0], input[1]):
self.makeMove(bubble)
self.touchesLeft -= 1
break
#check if game ended
if len(self.projectiles) <= 0:
playAgain = False
if len(self.bubbles) <= 0:
self.writeToScreen(center, "You won!", True)
gameOver = True
break
elif self.touchesLeft <= 0:
gameOver = True
hint = True
# update whole screen
pygame.display.flip()
# fill background
self.win.fill((0, 0, 0))
if not hint and self.checkReturnToMenu():
return "Main menu"
self.writeToScreen(center, "Game over :(", True)
self.writeToScreen((center[0], center[1] + 100), "H - Hint", False)
self.writeToScreen((center[0], center[1] + 60), "Would you like to play again? (y/n)", False)
while(hint):
input = self.checkInputs([pygame.K_h, pygame.K_y, pygame.K_n])
if input == pygame.K_h:
moves = self.solution
self.writeToScreen(center, "HINT", True)
self.writeToScreen((center[0], center[1] - 40), "[row, column]", True)
self.writeToScreen((center[0], center[1]), "Solution: ", True)
position = 40
for move in moves:
self.writeToScreen((center[0], center[1] + position), str(move), False)
position += 40
pygame.display.flip()
self.writeToScreen((center[0], center[1] + position), "Would you like to play again? (y/n)", False)
if input == pygame.K_y:
return "Play again"
if input == pygame.K_n:
pygame.quit()
quit()
pygame.display.flip()
#logic for computer mode
def playComputer(self):
gameOver = False
greedy = False
moves = []
waitingForSelection = True
self.ai = AI(self.startGrid, self.touchesLeft)
#Select AI mode
while waitingForSelection:
waitingForSelection = False
self.writeToScreen(center, "Press A to BFS algorithm", True)
self.writeToScreen((center[0], center[1] + 40), "Press B to DFS algorithm", False)
self.writeToScreen((center[0], center[1] + 80), "Press C to IDDFS algorithm", False)
self.writeToScreen((center[0], center[1] + 120), "Press D to Greedy algorithm", False)
pygame.display.flip()
input = self.checkInputs([pygame.K_a, pygame.K_b, pygame.K_c, pygame.K_d])
#choose algo to use
if input == pygame.K_a:
moves = self.ai.BFS()
elif input == pygame.K_b:
moves = self.ai.dfs()
elif input == pygame.K_c:
moves = self.ai.iddfs_algorithm()
elif input == pygame.K_d:
greedy = True
else:
waitingForSelection = True
print(len(moves))
print(self.touchesLeft)
#check if algo returned solution with more moves than allowed
if len(moves) > self.touchesLeft:
self.writeToScreen(center, "No solution exists for given moves :(", True)
pygame.display.flip()
gameOver = True
#choose heuristic for greedy mode
while(greedy):
self.writeToScreen(center, "Press A to Level Heuristic", True)
self.writeToScreen((center[0], center[1] + 40), "Press B to Score Heuristic", False)
pygame.display.flip()
input = self.checkInputs([pygame.K_a, pygame.K_b])
if input == pygame.K_a:
moves = self.ai.greedy_algorithm("levels")
greedy = False
if input == pygame.K_b:
moves = self.ai.greedy_algorithm("score")
greedy = False
#check if algo returned solution with more moves than allowed
if len(moves) > self.touchesLeft:
self.writeToScreen(center, "No solution exists for given moves :(", True)
pygame.display.flip()
gameOver = True
if not gameOver:
waitBetweenMoves = False
self.win.fill((0, 0, 0))
pygame.display.flip()
self.update()
pygame.time.wait(1000)
print(moves)
while (not gameOver):
self.update()
pygame.display.flip()
if waitBetweenMoves:
pygame.time.wait(1000)
waitBetweenMoves = False
#if previous move ended, and there are more moves to play, play next move
if len(self.projectiles) == 0 and len(moves) > 0:
move = moves.pop(0)
for bubble in self.bubbles:
if bubble.rect.collidepoint(100 * move[1] + 50, 100 * move[0] + scoreVerticalSpace):
#if the bubble isn't going to explode, wait 1 second to see the changes on screen
if bubble.level > 1:
waitBetweenMoves = True
self.makeMove(bubble)
self.touchesLeft -= 1
break
#check if game ended
if len(self.projectiles) <= 0:
if len(self.bubbles) <= 0:
self.writeToScreen(center, "You won!", True)
gameOver = True
break
elif self.touchesLeft <= 0 or len(moves) == 0:
self.writeToScreen(center, "Game over :(", True)
gameOver = True
# update whole screen
pygame.display.flip()
# fill background
self.win.fill((0, 0, 0))
if self.checkReturnToMenu():
return "Main menu"
# write text in the screen, if clearScreen=true, whole screen is wiped before writing
def writeToScreen(self, pos, text, clearScreen):
if clearScreen:
self.win.fill((0, 0, 0))
smallfont = pygame.font.SysFont("Arial", 25)
textSurf = smallfont.render(text, True, (255, 255, 255))
textRect = textSurf.get_rect()
textRect.center = pos
self.win.blit(textSurf, textRect)
# checks collision between sprites, removes projectiles which hit balls, returns score increment
def checkCollisions(self):
# dictionary with bubbles and projectiles which collided
ballsHit = groupcollide(self.bubbles, self.projectiles, False, True)
scoreAddition = 0
#hit bubbles which have collided with a projectile
for bubble in ballsHit.keys():
newExplosion = bubble.hit()
#if bubble exploded, generate new projectiles
if newExplosion != None:
scoreAddition += bubble.score * len(ballsHit[bubble])
self.bubbles.remove(bubble)
self.projectiles.add(newExplosion[0][0])
self.projectiles.add(newExplosion[0][1])
self.projectiles.add(newExplosion[0][2])
self.projectiles.add(newExplosion[0][3])
# remove projectiles that leave screen
for projectile in self.projectiles:
if projectile.x < 0 or projectile.y < 0 or projectile.x > windowWidth or projectile.y > windowHeight:
self.projectiles.remove(projectile)
return scoreAddition
#update entities
def update(self):
# update bubbles, projectiles
self.bubbles.update()
self.projectiles.update()
# check collisions between projectiles and bubbles. update score
self.score += self.checkCollisions()
# draw all entities
self.writeToScreen((center[0] - 100, 20), "Score: " + str(self.score), False)
self.writeToScreen((center[0], windowHeight + 50), "Touches left: " + str(self.touchesLeft), False)
self.writeToScreen((center[0] + 100, 20), "H - Hint", False)
self.bubbles.draw(self.win)
self.projectiles.draw(self.win)
pygame.display.flip()