add move function
parent
fd952fdf43
commit
26147e1182
32
avocado.py
32
avocado.py
|
@ -1,15 +1,25 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import pygame, random
|
import pygame
|
||||||
|
import random
|
||||||
|
|
||||||
class Avocado:
|
class Avocado:
|
||||||
|
|
||||||
def __init__(self, screensize):
|
def __init__(self, screensize, filename='avocado-01.jpg'):
|
||||||
screen_width, screen_height = screensize
|
self.screen_width, self.screen_height = screensize
|
||||||
self.x = random.randint(0,screen_width)
|
self.x = random.randint(0, self.screen_width)
|
||||||
self.y = 0 # change this to start somewhere above the screen
|
self.y = 0 # change this to start somewhere above the screen
|
||||||
self.w = 100
|
self.w = 100
|
||||||
self.y = 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):
|
def collides(self, click):
|
||||||
"""
|
"""
|
||||||
|
@ -22,3 +32,15 @@ class Avocado:
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
"""destroys this object"""
|
"""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
|
||||||
|
|
||||||
|
|
8
game.py
8
game.py
|
@ -20,7 +20,7 @@ def main():
|
||||||
screen_height = 600
|
screen_height = 600
|
||||||
screen = pygame.display.set_mode((screen_width,screen_height))
|
screen = pygame.display.set_mode((screen_width,screen_height))
|
||||||
bg = BLACK
|
bg = BLACK
|
||||||
desired_fps = 60
|
desired_fps = 5
|
||||||
|
|
||||||
font = pygame.font.Font(None, 40)
|
font = pygame.font.Font(None, 40)
|
||||||
game_over = font.render('GAME OVER', 0, RED)
|
game_over = font.render('GAME OVER', 0, RED)
|
||||||
|
@ -56,6 +56,12 @@ def main():
|
||||||
a = avocado.Avocado((screen_width, screen_height))
|
a = avocado.Avocado((screen_width, screen_height))
|
||||||
avocados.append(a)
|
avocados.append(a)
|
||||||
|
|
||||||
|
has_moved = False
|
||||||
|
for a in avocados:
|
||||||
|
if a.move():
|
||||||
|
has_moved = True
|
||||||
|
screen.blit(a.image, a.pycard)
|
||||||
|
|
||||||
# Catch events
|
# Catch events
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == MOUSEBUTTONDOWN:
|
if event.type == MOUSEBUTTONDOWN:
|
||||||
|
|
Loading…
Reference in New Issue