refactored pinned avocados
parent
ceb3316755
commit
0705a5fac3
25
avocado.py
25
avocado.py
|
@ -12,7 +12,7 @@ class Avocado:
|
||||||
# I'd rather just not return an instance,
|
# I'd rather just not return an instance,
|
||||||
# but I don't know how to do that :(
|
# but I don't know how to do that :(
|
||||||
if random.randint(0,40) != 1:
|
if random.randint(0,40) != 1:
|
||||||
self.is_falling = False
|
self.is_still_falling = False
|
||||||
self.has_been_pinned = False
|
self.has_been_pinned = False
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -34,9 +34,13 @@ class Avocado:
|
||||||
self.vx = 2
|
self.vx = 2
|
||||||
self.vy = 4
|
self.vy = 4
|
||||||
|
|
||||||
self.is_falling = True
|
# Avocado state
|
||||||
|
self.is_still_falling = True
|
||||||
self.has_been_pinned = False
|
self.has_been_pinned = False
|
||||||
|
|
||||||
|
# Avocado sounds
|
||||||
|
self.click = self.loadClick()
|
||||||
|
|
||||||
|
|
||||||
def setTargetColor(self, targetColor):
|
def setTargetColor(self, targetColor):
|
||||||
self.target = targetColor
|
self.target = targetColor
|
||||||
|
@ -62,6 +66,8 @@ class Avocado:
|
||||||
if self.rect.left < mousex and self.rect.right > mousex and \
|
if self.rect.left < mousex and self.rect.right > mousex and \
|
||||||
self.rect.top < mousey and self.rect.bottom > mousey:
|
self.rect.top < mousey and self.rect.bottom > mousey:
|
||||||
|
|
||||||
|
self.click.play()
|
||||||
|
|
||||||
if self.color == self.target:
|
if self.color == self.target:
|
||||||
self.has_been_pinned = True
|
self.has_been_pinned = True
|
||||||
return True
|
return True
|
||||||
|
@ -70,7 +76,12 @@ class Avocado:
|
||||||
|
|
||||||
|
|
||||||
def exists(self):
|
def exists(self):
|
||||||
return not self.has_been_pinned and self.is_falling
|
# return not self.has_been_pinned and self.is_still_falling
|
||||||
|
return self.is_still_falling
|
||||||
|
|
||||||
|
|
||||||
|
def isPinned(self):
|
||||||
|
return self.has_been_pinned
|
||||||
|
|
||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
|
@ -88,10 +99,16 @@ class Avocado:
|
||||||
|
|
||||||
def hasLanded(self):
|
def hasLanded(self):
|
||||||
if self.rect.bottom > self.screen_height or self.rect.top < 0:
|
if self.rect.bottom > self.screen_height or self.rect.top < 0:
|
||||||
self.is_falling = False
|
self.is_still_falling = False
|
||||||
print('DEBUG :: splatsh!')
|
print('DEBUG :: splatsh!')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def loadClick(self, sound=True):
|
||||||
|
if not sound:
|
||||||
|
return
|
||||||
|
return pygame.mixer.Sound("audio/click.wav")
|
||||||
|
|
||||||
|
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
del(self)
|
del(self)
|
||||||
|
|
39
game.py
39
game.py
|
@ -27,8 +27,6 @@ class TheGame:
|
||||||
# fonts
|
# fonts
|
||||||
self.bigFont = pygame.font.Font(None, 90)
|
self.bigFont = pygame.font.Font(None, 90)
|
||||||
|
|
||||||
self.pinned = []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pygame.mixer.init()
|
pygame.mixer.init()
|
||||||
pygame.mixer.music.set_volume(0.5)
|
pygame.mixer.music.set_volume(0.5)
|
||||||
|
@ -59,17 +57,14 @@ class TheGame:
|
||||||
pygame.mixer.music.play()
|
pygame.mixer.music.play()
|
||||||
|
|
||||||
|
|
||||||
def fade(self, sound=True):
|
def fadeSound(self, sound=True):
|
||||||
if not sound:
|
if not sound:
|
||||||
return
|
return
|
||||||
pygame.mixer.music.fadeout(3000)
|
pygame.mixer.music.fadeout(3000)
|
||||||
|
|
||||||
|
def chooseRandomColor(self):
|
||||||
def loadClick(self, sound=True):
|
selected = random.randint(0, 3)
|
||||||
if not sound:
|
return self.colors[selected]
|
||||||
return
|
|
||||||
self.click = pygame.mixer.Sound("audio/click.wav")
|
|
||||||
return self.click
|
|
||||||
|
|
||||||
|
|
||||||
def gameOver(self):
|
def gameOver(self):
|
||||||
|
@ -79,7 +74,7 @@ class TheGame:
|
||||||
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
|
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
|
||||||
self.screen.blit(pygame.transform.scale(gameOverImage, self.size), (0, 0))
|
self.screen.blit(pygame.transform.scale(gameOverImage, self.size), (0, 0))
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
self.fade() # Fade the music
|
self.fadeSound()
|
||||||
pygame.time.wait(3000)
|
pygame.time.wait(3000)
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
@ -103,7 +98,8 @@ class TheGame:
|
||||||
time = timeleft = 30
|
time = timeleft = 30
|
||||||
level = 1
|
level = 1
|
||||||
levelChange = 0
|
levelChange = 0
|
||||||
avoClick = self.loadClick()
|
score = 0
|
||||||
|
targetScore = 400
|
||||||
|
|
||||||
# initialize the HUD class and the lawyer
|
# initialize the HUD class and the lawyer
|
||||||
the_hud = hud.Hud(self.screen)
|
the_hud = hud.Hud(self.screen)
|
||||||
|
@ -134,7 +130,7 @@ class TheGame:
|
||||||
pauseFor = 35
|
pauseFor = 35
|
||||||
timeleft = time
|
timeleft = time
|
||||||
avocados = []
|
avocados = []
|
||||||
self.pinned = []
|
# self.pinned = []
|
||||||
print('DEBUG :: Level ' + str(level))
|
print('DEBUG :: Level ' + str(level))
|
||||||
self.playLevel(level)
|
self.playLevel(level)
|
||||||
|
|
||||||
|
@ -153,8 +149,9 @@ class TheGame:
|
||||||
# Redraw the HUD
|
# Redraw the HUD
|
||||||
the_hud.draw_hud(score, displaytime, round(fps, 2))
|
the_hud.draw_hud(score, displaytime, round(fps, 2))
|
||||||
|
|
||||||
# Initialize a number of avocados, depending on the level
|
# If we're not currently in between levels…
|
||||||
if levelChange == 0:
|
if levelChange == 0:
|
||||||
|
# Initialize a number of avocados, depending on the level
|
||||||
avocados_in_game = len(avocados)
|
avocados_in_game = len(avocados)
|
||||||
avocadosWanted = level * multiplier
|
avocadosWanted = level * multiplier
|
||||||
if avocados_in_game < avocadosWanted:
|
if avocados_in_game < avocadosWanted:
|
||||||
|
@ -166,22 +163,23 @@ class TheGame:
|
||||||
|
|
||||||
# Remove avocados from the list if they no longer exist
|
# Remove avocados from the list if they no longer exist
|
||||||
# E.g. have been pinned or fallen down
|
# E.g. have been pinned or fallen down
|
||||||
self.pinned += [avo for avo in avocados if avo.has_been_pinned]
|
|
||||||
avocados[:] = [ x for x in avocados if x.exists() ]
|
avocados[:] = [ x for x in avocados if x.exists() ]
|
||||||
for a in avocados:
|
for a in avocados:
|
||||||
a.setTargetColor(color)
|
a.setTargetColor(color)
|
||||||
|
if not a.isPinned():
|
||||||
a.move()
|
a.move()
|
||||||
a.blitme()
|
a.blitme()
|
||||||
for a in self.pinned:
|
|
||||||
a.blitme()
|
|
||||||
|
|
||||||
# Catch events
|
# Catch events
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
# Collision detection
|
# Collision detection
|
||||||
if event.type == MOUSEBUTTONDOWN:
|
if event.type == MOUSEBUTTONDOWN:
|
||||||
avoClick.play()
|
mousepos = pygame.mouse.get_pos()
|
||||||
|
# Throw a pin here
|
||||||
|
# pin.throwAt(mousepos)
|
||||||
|
# Yep, above here
|
||||||
for avo in avocados:
|
for avo in avocados:
|
||||||
hit = avo.isHit(pygame.mouse.get_pos())
|
hit = avo.isHit(mousepos)
|
||||||
if hit:
|
if hit:
|
||||||
score += 100
|
score += 100
|
||||||
color = self.chooseRandomColor()
|
color = self.chooseRandomColor()
|
||||||
|
@ -198,11 +196,6 @@ class TheGame:
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
def chooseRandomColor(self):
|
|
||||||
selected = random.randint(0, 3)
|
|
||||||
return self.colors[selected]
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
game = TheGame()
|
game = TheGame()
|
||||||
game.playLevel()
|
game.playLevel()
|
||||||
|
|
Loading…
Reference in New Issue