-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
executable file
·335 lines (270 loc) · 10.8 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*- #
"""
Avocados and stuff
"""
import os, random, sys
import pygame
import avocado, crystal, pingenerator, itext
from pygame.locals import *
from support.colors import *
from interface import hud
class TheGame:
def __init__(self):
""" Initialize the game """
pygame.init()
pygame.display.set_caption('Pin Avo, the Cado!')
self.clock = pygame.time.Clock()
self.initializeScreen()
# initialize the game canvas
self.timeout = 30
self.level = 1
self.psychomode = 2
self.targetScore = 400
##############################
# Never set below 4, else we have a high
# probability of losing the game due to a missing color
# Alternatively, you could edit chooseRandomColor()
# to only work on the first multiplier colors
self.desired_fps = 60
self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
self.colors = [RED, GREEN, BLUE, YELLOW, PINK]
self.multiplier = len(self.colors)
self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png'))
self.bg.set_colorkey((255,255,255))
self.bg.set_alpha(75)
self.last_colors = []
# fonts
self.bigFont = pygame.font.Font(None, 90)
# Set splashscreen
splashScreen = pygame.image.load(os.path.join('img', 'splashScreen.png'))
self.screen.blit(
pygame.transform.scale(
splashScreen, (self.WIDTH, self.HEIGHT)
),
(0, 0)
)
pygame.display.flip()
pygame.time.wait(3000)
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
sound = True
except:
print("Y U NO sound? :(")
sound = False
def initializeScreen(self):
# displayInfo = pygame.display.Info()
# self.resize = 1.3
# self.WIDTH = int(displayInfo.current_w / self.resize)
# self.HEIGHT = int(displayInfo.current_h / self.resize)
# Look at the cleverness ;)
self.WIDTH, self.HEIGHT = 800, 600
def mute(self,mute=False, sound=True):
if not sound:
return
if mute:
pygame.mixer.music.set_volume(0.0)
else:
pygame.mixer.music.set_volume(0.5)
def playLevel(self,lvl=1,sound=True):
if not sound:
return
if lvl == 1:
pygame.mixer.music.load("""audio/level1.wav""")
elif lvl == 2:
pygame.mixer.music.load("""audio/level2.wav""")
elif lvl >= 3:
pygame.mixer.music.load("""audio/level3.wav""")
pygame.mixer.music.play()
def toggle_fullscreen(self):
global fullS
fullS = True
self.screen = pygame.display.get_surface()
self.tmp = self.screen.convert()
self.caption = pygame.display.get_caption()
self.cursor = pygame.mouse.get_cursor() # Duoas 16-04-2007
self.w,self.h = self.screen.get_width(),self.screen.get_height()
self.flags = self.screen.get_flags()
self.bits = self.screen.get_bitsize()
pygame.display.quit()
pygame.display.init()
self.screen = pygame.display.set_mode((self.w,self.h),self.flags^FULLSCREEN,self.bits)
self.screen.blit(self.tmp,(0,0))
pygame.display.set_caption(*self.caption)
pygame.key.set_mods(0) #HACK: work-a-round for a SDL bug??
pygame.mouse.set_cursor( *self.cursor ) # Duoas 16-04-2007
return self.screen
def fadeSound(self, sound=True):
if not sound:
return
pygame.mixer.music.fadeout(3000)
def chooseRandomColor(self):
selected = random.randint(0, len(self.colors) - 1)
if len(self.last_colors) > 5:
self.last_colors.pop(0)
for i in range(0, 5):
if selected in self.last_colors:
selected = random.randint(0, 3)
else:
break
self.last_colors.append(selected)
return self.colors[selected]
def gameOver(self):
screen_width, screen_height = self.screen.get_size()
gameOverImage = pygame.image.load(os.path.join('img', 'gameOver.png'))
gameOverText = self.bigFont.render('GAME OVER', False, YELLOW)
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
self.screen.blit(pygame.transform.scale(gameOverImage,
self.screen.get_size()), (0, 0))
pygame.display.flip()
self.fadeSound()
pygame.time.wait(3000)
pygame.quit()
sys.exit()
def keepPinned(self, avocado):
self.pinned.append(avocado)
def drawBackground(self):
if type(self.bg) is tuple:
self.screen.fill(self.bg)
else:
self.screen.blit(
pygame.transform.scale(
self.bg, self.screen.get_size()
),
(0, 0)
)
def resetLevel(self):
self.pinnedAvocados = []
self.movingAvocados = []
self.thrownPins = []
self.timeleft = self.timeout
def main(self):
score = 0
boundaries = []
# We could use this list for redrawing only this part
# of the screen instead of all of it
self.resetLevel()
# initialize the HUD class and the lawyer
the_hud = hud.Hud(self.screen)
crystalBall = crystal.Crystal(self.screen, self.level, self.bigFont, self.psychomode)
boundaries.append(crystalBall.getBoundaries())
# Initial color indication
color = self.chooseRandomColor()
crystalBall.setColor(color)
texts = []
container = {'font': self.bigFont, 'screen': self.screen, 'clock': self.clock}
running = True
while running:
time_passed = self.clock.tick(self.desired_fps)
fps = self.clock.get_fps()
screen_width, screen_height = self.screen.get_size()
# Next level?
if score >= (self.targetScore * self.level):
self.level += 1
levelText = itext.Text(
container,
'Level ' + str(self.level),
(screen_width / 3, screen_height /2),
2000
)
texts.append(levelText)
self.playLevel(self.level)
self.resetLevel()
self.timeleft -= time_passed / 1000
self.timeleft = round(self.timeleft, 2)
if self.timeleft <= 0:
self.gameOver()
else:
displaytime = self.timeleft
# Redraw the background and put our lawyer back on top
self.drawBackground()
crystalBall.blitme()
# Check if there's any text that wants to get displayed
for text in texts:
text.blitme()
texts[:] = [text for text in texts if not text.hasExpired() ]
# Redraw the HUD
the_hud.draw_hud(score, displaytime, round(fps, 2))
# Initialize a number of avocados, depending on the level
avocadosInGame = len(self.movingAvocados)
avocadosWanted = self.level * self.multiplier
if avocadosInGame < avocadosWanted:
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
if random.randint(0, probability) < 3:
properties = {'color': self.chooseRandomColor(), 'size': (50,50)}
# Spawn a new avocado
a = avocado.Avocado(
self.screen,
boundaries,
properties,
color,
self.level
)
self.movingAvocados.append(a)
# Remove avocados from the list of moving avocados if they no longer move
# Or add them to the list of pinned avocados if they're been hit
self.pinnedAvocados += [avo for avo in self.movingAvocados if avo.isPinned() ]
self.movingAvocados[:] = [ avo for avo in self.movingAvocados if avo.isFalling() ]
##############################
#
# Late-Night-Comments:
#
# Can we maybe handle the pinned avocados the same way I handle "stuck"
# pins? It seems to be easier.. well, the pins don't fall out of the screen
# though…
#
##############################
# Now redraw our avocados
for a in self.movingAvocados:
a.setTargetColor(color)
a.move()
a.blitme()
for a in self.pinnedAvocados:
a.blitme()
# And finally check if we need to redraw any pins
for activePin in self.thrownPins:
activePin.blitme()
if not activePin.isStuck():
activePin.moveTowardsTarget()
# Catch events
for event in pygame.event.get():
# Collision detection
if event.type == MOUSEBUTTONDOWN:
mousepos = pygame.mouse.get_pos()
# Throwing a new pin
newPin = pingenerator.Generate(self.screen)
newPin.throwAt(mousepos)
self.thrownPins.append(newPin)
# Check if any avocados have been hit
for avo in self.movingAvocados:
hit, center = avo.isHit(mousepos)
if hit is None:
continue
if hit:
score += 100
newPin.throwAt(center)
color = self.chooseRandomColor()
crystalBall.setColor(color)
else:
score -= 50
# Had enough of this?
if event.type == pygame.QUIT:
running = False
game.gameOver()
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if pygame.key.get_pressed()[pygame.K_f] != 0:
print("Toggling full screen, in the Future")
#game.toggle_fullscreen()
elif (pygame.key.get_pressed()[pygame.K_q] != 0) or (pygame.key.get_pressed()[pygame.K_ESCAPE] != 0):
running = False
game.gameOver()
pygame.quit()
sys.exit()
pygame.display.flip()
if __name__ == '__main__':
game = TheGame()
game.playLevel()
game.main()