From 26147e118242b4063227843685ecc1d53c085e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sat, 30 Aug 2014 18:20:49 +0200 Subject: [PATCH] add move function --- avocado.py | 32 +++++++++++++++++++++++++++----- game.py | 8 +++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/avocado.py b/avocado.py index cbc5551..0d2b7bf 100644 --- a/avocado.py +++ b/avocado.py @@ -1,15 +1,25 @@ #!/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(0, 50) + self.step_x = 10 + self.step_y = 10 + self.is_falling = True def collides(self, click): """ @@ -22,3 +32,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 a848c2a..28c1df1 100755 --- a/game.py +++ b/game.py @@ -20,7 +20,7 @@ def main(): screen_height = 600 screen = pygame.display.set_mode((screen_width,screen_height)) bg = BLACK - desired_fps = 60 + desired_fps = 5 font = pygame.font.Font(None, 40) game_over = font.render('GAME OVER', 0, RED) @@ -56,6 +56,12 @@ def main(): a = avocado.Avocado((screen_width, screen_height)) avocados.append(a) + has_moved = False + for a in avocados: + if a.move(): + has_moved = True + screen.blit(a.image, a.pycard) + # Catch events for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN: