From e2075f33a5f3628ef672f317a970b6c37646dbe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sat, 30 Aug 2014 21:11:10 +0200 Subject: [PATCH] Make the avocados clickable --- avocado.py | 17 ++++++++++++----- game.py | 7 +++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/avocado.py b/avocado.py index 471fdc1..4efe54d 100644 --- a/avocado.py +++ b/avocado.py @@ -21,31 +21,38 @@ class Avocado: self.init_pos() self.step_x = 10 self.step_y = 10 - self.is_falling = True + self.is_destroyed = False def init_pos(self): self.pycard.x = random.randint(0, self.screen_width) self.pycard.y = random.randint(20, 70) + self.is_destroyed = False def collides(self, click): """ Checks whether this object collides with the given position in click """ - return True + mousex, mousey = click + if self.pycard.left < mousex and self.pycard.right > mousex and \ + self.pycard.top < mousey and self.pycard.bottom > mousey: + self.destroy() + return True + return False #if collision then self.destroy() def destroy(self): """destroys this object""" + self.is_destroyed = True def move(self): if self.pycard.right > self.screen_width or self.pycard.left < 0: self.step_x = -self.step_x - if self.pycard.bottom > self.screen_height or self.pycard.top < 0: + if self.is_destroyed or self.pycard.bottom > self.screen_height \ + or self.pycard.top < 0: print('platch') return False - self.is_falling = False + print(self.pycard.x, self.pycard.y) self.pycard.x += self.step_x self.pycard.y += self.step_y return True - diff --git a/game.py b/game.py index 50ecf7e..78942ac 100755 --- a/game.py +++ b/game.py @@ -28,7 +28,7 @@ def main(): #size = initialize_screen() size = (800, 600) bg = BLACK - desired_fps = 60 + desired_fps = 10 font = pygame.font.Font(None, 40) # I don't know, should we move this text out of the way? @@ -66,10 +66,13 @@ 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)) displaytime = 'Timed out!' + pygame.display.flip() + continue else: displaytime = timeleft @@ -97,7 +100,7 @@ def main(): for avo in avocados: if avo.collides(pygame.mouse.get_pos()): score += 100 - + avo.init_pos() # Had enough of this? if event.type == pygame.QUIT: running = False