avocados/avocado.py

84 lines
2.2 KiB
Python
Raw Normal View History

2014-08-30 17:59:20 +02:00
#!/usr/bin/env python3
2014-08-30 20:53:47 +02:00
import pygame, random
from support import operations
2014-08-30 17:59:20 +02:00
class Avocado:
2014-08-30 21:20:42 +02:00
def __init__(self, screen, color, size, select, filename='img/AvoCado_0.png'):
# We randomly decide whether we should instanciate or not
if random.randint(0,1) == 0:
self.is_falling = False
return None
2014-08-30 20:47:21 +02:00
print('New avocado is ' + ','.join(str(color)))
2014-08-30 20:53:47 +02:00
self.screen = screen
2014-08-30 21:20:42 +02:00
self.color = color
self.select = select
2014-08-30 20:47:21 +02:00
self.screen_width, self.screen_height = screen.get_size()
2014-08-30 18:20:49 +02:00
self.x = random.randint(0, self.screen_width)
self.y = 0 # change this to start somewhere above the screen
2014-08-30 20:55:40 +02:00
self.w , self.y = size
2014-08-30 20:53:47 +02:00
2014-08-30 21:20:42 +02:00
# Initialize the image
2014-08-30 18:20:49 +02:00
self.i = pygame.image.load(filename).convert_alpha()
2014-08-30 20:53:47 +02:00
operations.color_surface(self.i, color)
2014-08-30 20:55:40 +02:00
self.image = pygame.transform.scale(self.i, (self.w, self.y))
2014-08-30 21:20:42 +02:00
self.rect = self.image.get_rect()
2014-08-30 18:20:49 +02:00
2014-08-30 21:20:42 +02:00
# Set the avocado's initial position and velocity
2014-08-30 19:01:48 +02:00
self.init_pos()
2014-08-30 21:20:42 +02:00
self.vx = 10
self.vy = 10
2014-08-30 18:20:49 +02:00
self.is_falling = True
2014-08-30 17:59:20 +02:00
2014-08-30 21:20:42 +02:00
def blitme(self):
self.screen.blit(self.image, self.rect)
2014-08-30 19:01:48 +02:00
def init_pos(self):
2014-08-30 21:20:42 +02:00
self.rect.x = random.randint(0, self.screen_width)
self.rect.y = random.randint(20, 70)
2014-08-30 18:43:19 +02:00
2014-08-30 17:59:20 +02:00
def collides(self, click):
"""
Checks whether this object collides with the given position
in click
"""
2014-08-30 21:11:10 +02:00
mousex, mousey = click
2014-08-30 21:24:58 +02:00
if self.rect.left < mousex and self.rect.right > mousex and \
self.rect.top < mousey and self.rect.bottom > mousey and \
2014-08-30 21:23:42 +02:00
self.color == self.select:
2014-08-30 21:20:42 +02:00
self.destroy()
return True
2014-08-30 17:59:20 +02:00
def destroy(self):
"""destroys this object"""
2014-08-30 21:20:42 +02:00
del(self)
def exists(self):
return self.is_falling
2014-08-30 17:59:20 +02:00
2014-08-30 18:20:49 +02:00
def move(self):
2014-08-30 21:20:42 +02:00
if self.rect.right > self.screen_width or self.rect.left < 0:
self.vx = -self.vy
if self.hasLanded():
self.destroy()
self.rect.x += self.vx
self.rect.y += self.vy
2014-08-30 18:20:49 +02:00
return True
2014-08-30 21:20:42 +02:00
def hasLanded(self):
if self.rect.bottom > self.screen_height or self.rect.top < 0:
self.is_falling = False
print('platch')
return True