From 7c657961f8fa2e139f46917ae3857975218a04e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sat, 30 Aug 2014 18:43:19 +0200 Subject: [PATCH] make the avocados move --- avocado.py | 6 +++++- game.py | 24 +++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/avocado.py b/avocado.py index 0d2b7bf..f0e5710 100644 --- a/avocado.py +++ b/avocado.py @@ -16,11 +16,15 @@ class Avocado: self.pycard = self.image.get_rect() self.pycard.x = random.randint(0, self.screen_width) - self.pycard.y = random.randint(0, 50) + self.pycard.y = random.randint(20, 70) self.step_x = 10 self.step_y = 10 self.is_falling = True + def reset(self): + self.pycard.x = random.randint(0, self.screen_width) + self.pycard.y = random.randint(0, 50) + def collides(self, click): """ Checks whether this object collides with the given position diff --git a/game.py b/game.py index 28c1df1..4bbd0c6 100755 --- a/game.py +++ b/game.py @@ -10,6 +10,7 @@ from pygame.locals import * from support.colors import * from interface import hud + def main(): pygame.init() pygame.display.set_caption('Pin the Avocados!') @@ -18,7 +19,7 @@ def main(): # Move this outside the main code? screen_width = 800 screen_height = 600 - screen = pygame.display.set_mode((screen_width,screen_height)) + screen = pygame.display.set_mode((screen_width, screen_height)) bg = BLACK desired_fps = 5 @@ -28,10 +29,11 @@ def main(): score = 0 time = 15 - level = 1 + level = 5 running = True timeleft = time + avocados = [] while running: time_passed = clock.tick(desired_fps) fps = clock.get_fps() @@ -47,19 +49,19 @@ def main(): displaytime = timeleft # Redraw the HUD - chud = my_hud.draw_hud(score, displaytime, round(fps,2)) - screen.blit(chud, (10,10)) + chud = my_hud.draw_hud(score, displaytime, round(fps, 2)) + screen.blit(chud, (10, 10)) # Initialize a number of avocados, depending on the level - avocados = [] - for i in range(0, level): - a = avocado.Avocado((screen_width, screen_height)) - avocados.append(a) + if len(avocados) != level: + avocados = [] + for i in range(0, level): + a = avocado.Avocado((screen_width, screen_height)) + avocados.append(a) - has_moved = False for a in avocados: - if a.move(): - has_moved = True + if not a.move(): + a.reset() screen.blit(a.image, a.pycard) # Catch events