diff --git a/avocado.py b/avocado.py index cbc5551..f0e5710 100644 --- a/avocado.py +++ b/avocado.py @@ -1,15 +1,29 @@ #!/usr/bin/env python3 -import pygame, random +import pygame +import random class Avocado: - def __init__(self, screensize): - screen_width, screen_height = screensize - self.x = random.randint(0,screen_width) - self.y = 0 # change this to start somewhere above the screen + def __init__(self, screensize, filename='avocado-01.jpg'): + self.screen_width, self.screen_height = screensize + self.x = random.randint(0, self.screen_width) + self.y = 0 # change this to start somewhere above the screen self.w = 100 self.y = 100 + self.i = pygame.image.load(filename).convert_alpha() + self.image = pygame.transform.scale(self.i, (20, 20)) + self.pycard = self.image.get_rect() + + self.pycard.x = random.randint(0, self.screen_width) + 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): """ @@ -22,3 +36,15 @@ class Avocado: def destroy(self): """destroys this object""" + 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: + 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 b10aeee..84064f0 100755 --- a/game.py +++ b/game.py @@ -45,10 +45,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() @@ -68,14 +69,20 @@ 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(size) - avocados.append(a) + if len(avocados) != level: + avocados = [] + for i in range(0, level): + a = avocado.Avocado(size) + avocados.append(a) + + for a in avocados: + if not a.move(): + a.reset() + screen.blit(a.image, a.pycard) # Catch events for event in pygame.event.get(): diff --git a/img/AvoCado_0.png b/img/AvoCado_0.png new file mode 100644 index 0000000..e8dcadb Binary files /dev/null and b/img/AvoCado_0.png differ diff --git a/img/pin.png b/img/pin.png new file mode 100644 index 0000000..ee76e5c Binary files /dev/null and b/img/pin.png differ