From 1c21c151e845b653d31f208fbf3a585cf157d3f2 Mon Sep 17 00:00:00 2001 From: David Raison Date: Sat, 30 Aug 2014 21:50:26 +0200 Subject: [PATCH] Substract points if hitting the wrong avocado --- avocado.py | 29 +++++++++++++++++------------ game.py | 11 +++++++---- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/avocado.py b/avocado.py index 21ef7f3..e26fce8 100644 --- a/avocado.py +++ b/avocado.py @@ -6,12 +6,15 @@ from support import operations class Avocado: def __init__(self, screen, color, size, select, filename='img/AvoCado_0.png'): + # We randomly decide whether we should instanciate or not + # I'd rather just not return an instance, + # but I don't know how to do that :( if random.randint(0,1) == 0: self.is_falling = False + self.has_been_pinned = False return None - print('New avocado is ' + ','.join(str(color))) self.screen = screen self.color = color self.select = select @@ -31,6 +34,7 @@ class Avocado: self.vx = 10 self.vy = 10 self.is_falling = True + self.has_been_pinned = False def blitme(self): @@ -42,24 +46,24 @@ class Avocado: self.rect.y = random.randint(20, 70) - def collides(self, click): + def isHit(self, click): """ Checks whether this object collides with the given position - in click + of a mouse-click """ mousex, mousey = click if self.rect.left < mousex and self.rect.right > mousex and \ - self.rect.top < mousey and self.rect.bottom > mousey and \ - self.color == self.select: - self.destroy() - return True + self.rect.top < mousey and self.rect.bottom > mousey: + + if self.color == self.select: + self.has_been_pinned = True + return True + else: + return False - def destroy(self): - """destroys this object""" - del(self) def exists(self): - return self.is_falling + return not self.has_been_pinned and self.is_falling def move(self): @@ -77,7 +81,8 @@ class Avocado: def hasLanded(self): if self.rect.bottom > self.screen_height or self.rect.top < 0: self.is_falling = False - print('platch') return True + def destroy(self): + del(self) diff --git a/game.py b/game.py index c4cd111..95b2660 100755 --- a/game.py +++ b/game.py @@ -65,8 +65,6 @@ def main(): timeleft -= time_passed / 1000 timeleft = round(timeleft, 2) - - if timeleft <= 0: screen_width, screen_height = size screen.blit(game_over, (screen_width/3, screen_height/2)) @@ -89,6 +87,8 @@ def main(): a = avocado.Avocado(screen, avocolor, avosize, color) avocados.append(a) + # Remove avocados from the list if they no longer exist + # E.g. have been pinned or fallen down avocados[:] = [ x for x in avocados if x.exists() ] for a in avocados: a.move() @@ -99,9 +99,12 @@ def main(): # Collision detection if event.type == MOUSEBUTTONDOWN: for avo in avocados: - if avo.collides(pygame.mouse.get_pos()): + hit = avo.isHit(pygame.mouse.get_pos()) + if hit: score += 100 - avo.init_pos() + elif hit == False: + score -= 50 + # Had enough of this? if event.type == pygame.QUIT: running = False