add move function

master
Raphaël Vinot 2014-08-30 18:20:49 +02:00
parent fd952fdf43
commit 26147e1182
2 changed files with 34 additions and 6 deletions

View File

@ -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

View File

@ -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: