avocados/avocado.py

59 lines
1.9 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 20:55:40 +02:00
def __init__(self, screen, color, size, filename='img/AvoCado_0.png'):
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 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 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 18:20:49 +02:00
self.pycard = self.image.get_rect()
2014-08-30 19:01:48 +02:00
self.init_pos()
2014-08-30 18:20:49 +02:00
self.step_x = 10
self.step_y = 10
2014-08-30 21:11:10 +02:00
self.is_destroyed = False
2014-08-30 17:59:20 +02:00
2014-08-30 19:01:48 +02:00
def init_pos(self):
2014-08-30 18:43:19 +02:00
self.pycard.x = random.randint(0, self.screen_width)
2014-08-30 19:01:48 +02:00
self.pycard.y = random.randint(20, 70)
2014-08-30 21:11:10 +02:00
self.is_destroyed = False
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
if self.pycard.left < mousex and self.pycard.right > mousex and \
self.pycard.top < mousey and self.pycard.bottom > mousey:
self.destroy()
return True
return False
2014-08-30 17:59:20 +02:00
#if collision then self.destroy()
def destroy(self):
"""destroys this object"""
2014-08-30 21:11:10 +02:00
self.is_destroyed = True
2014-08-30 17:59:20 +02:00
2014-08-30 18:20:49 +02:00
def move(self):
if self.pycard.right > self.screen_width or self.pycard.left < 0:
self.step_x = -self.step_x
2014-08-30 21:11:10 +02:00
if self.is_destroyed or self.pycard.bottom > self.screen_height \
or self.pycard.top < 0:
2014-08-30 18:20:49 +02:00
print('platch')
return False
2014-08-30 21:11:10 +02:00
print(self.pycard.x, self.pycard.y)
2014-08-30 18:20:49 +02:00
self.pycard.x += self.step_x
self.pycard.y += self.step_y
return True