refactored pinned avocados

master
David Raison 2014-08-31 01:07:12 +02:00
parent ceb3316755
commit 0705a5fac3
2 changed files with 38 additions and 28 deletions

View File

@ -12,7 +12,7 @@ class Avocado:
# I'd rather just not return an instance,
# but I don't know how to do that :(
if random.randint(0,40) != 1:
self.is_falling = False
self.is_still_falling = False
self.has_been_pinned = False
return None
@ -34,9 +34,13 @@ class Avocado:
self.vx = 2
self.vy = 4
self.is_falling = True
# Avocado state
self.is_still_falling = True
self.has_been_pinned = False
# Avocado sounds
self.click = self.loadClick()
def setTargetColor(self, targetColor):
self.target = targetColor
@ -62,6 +66,8 @@ class Avocado:
if self.rect.left < mousex and self.rect.right > mousex and \
self.rect.top < mousey and self.rect.bottom > mousey:
self.click.play()
if self.color == self.target:
self.has_been_pinned = True
return True
@ -70,7 +76,12 @@ class Avocado:
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):
@ -88,10 +99,16 @@ class Avocado:
def hasLanded(self):
if self.rect.bottom > self.screen_height or self.rect.top < 0:
self.is_falling = False
self.is_still_falling = False
print('DEBUG :: splatsh!')
return True
def loadClick(self, sound=True):
if not sound:
return
return pygame.mixer.Sound("audio/click.wav")
def destroy(self):
del(self)

41
game.py
View File

@ -27,8 +27,6 @@ class TheGame:
# fonts
self.bigFont = pygame.font.Font(None, 90)
self.pinned = []
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
@ -59,17 +57,14 @@ class TheGame:
pygame.mixer.music.play()
def fade(self, sound=True):
def fadeSound(self, sound=True):
if not sound:
return
pygame.mixer.music.fadeout(3000)
def loadClick(self, sound=True):
if not sound:
return
self.click = pygame.mixer.Sound("audio/click.wav")
return self.click
def chooseRandomColor(self):
selected = random.randint(0, 3)
return self.colors[selected]
def gameOver(self):
@ -79,7 +74,7 @@ class TheGame:
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
self.screen.blit(pygame.transform.scale(gameOverImage, self.size), (0, 0))
pygame.display.flip()
self.fade() # Fade the music
self.fadeSound()
pygame.time.wait(3000)
pygame.quit()
sys.exit()
@ -103,7 +98,8 @@ class TheGame:
time = timeleft = 30
level = 1
levelChange = 0
avoClick = self.loadClick()
score = 0
targetScore = 400
# initialize the HUD class and the lawyer
the_hud = hud.Hud(self.screen)
@ -134,7 +130,7 @@ class TheGame:
pauseFor = 35
timeleft = time
avocados = []
self.pinned = []
# self.pinned = []
print('DEBUG :: Level ' + str(level))
self.playLevel(level)
@ -153,8 +149,9 @@ class TheGame:
# Redraw the HUD
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:
# Initialize a number of avocados, depending on the level
avocados_in_game = len(avocados)
avocadosWanted = level * multiplier
if avocados_in_game < avocadosWanted:
@ -166,22 +163,23 @@ class TheGame:
# Remove avocados from the list if they no longer exist
# 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() ]
for a in avocados:
a.setTargetColor(color)
a.move()
a.blitme()
for a in self.pinned:
if not a.isPinned():
a.move()
a.blitme()
# Catch events
for event in pygame.event.get():
# Collision detection
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:
hit = avo.isHit(pygame.mouse.get_pos())
hit = avo.isHit(mousepos)
if hit:
score += 100
color = self.chooseRandomColor()
@ -198,11 +196,6 @@ class TheGame:
pygame.display.flip()
def chooseRandomColor(self):
selected = random.randint(0, 3)
return self.colors[selected]
if __name__ == '__main__':
game = TheGame()
game.playLevel()