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-31 02:36:21 +02:00
|
|
|
def __init__(self, screen, color, size, target, level, filename='img/AvoCado_0.png'):
|
2014-08-30 21:20:42 +02:00
|
|
|
|
2014-08-30 22:33:10 +02:00
|
|
|
# Set up our instance variables
|
2014-08-30 20:53:47 +02:00
|
|
|
self.screen = screen
|
2014-08-30 21:20:42 +02:00
|
|
|
self.color = color
|
2014-08-30 22:33:10 +02:00
|
|
|
self.target = target
|
2014-08-30 20:47:21 +02:00
|
|
|
self.screen_width, self.screen_height = screen.get_size()
|
2014-08-30 22:33:10 +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 23:34:52 +02:00
|
|
|
self.vx = 2
|
2014-08-31 02:36:21 +02:00
|
|
|
self.vy = 4 * (level * 0.5)
|
2014-08-30 23:34:52 +02:00
|
|
|
|
2014-08-31 01:07:12 +02:00
|
|
|
# Avocado state
|
|
|
|
self.is_still_falling = True
|
2014-08-30 21:50:26 +02:00
|
|
|
self.has_been_pinned = False
|
2014-08-30 17:59:20 +02:00
|
|
|
|
2014-08-31 01:07:12 +02:00
|
|
|
# Avocado sounds
|
|
|
|
self.click = self.loadClick()
|
2014-08-31 10:04:20 +02:00
|
|
|
self.clickFail = self.loadFailClick()
|
2014-08-31 01:07:12 +02:00
|
|
|
|
2014-08-30 22:48:49 +02:00
|
|
|
|
2014-08-30 23:03:51 +02:00
|
|
|
def setTargetColor(self, targetColor):
|
2014-08-30 22:33:10 +02:00
|
|
|
self.target = targetColor
|
|
|
|
|
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 21:50:26 +02:00
|
|
|
def isHit(self, click):
|
2014-08-30 17:59:20 +02:00
|
|
|
"""
|
|
|
|
Checks whether this object collides with the given position
|
2014-08-30 22:33:10 +02:00
|
|
|
of a mouse-click. Return true is the correct color was hit and
|
|
|
|
false if it was the wrong one.
|
|
|
|
Just returns void if no avocado was hit
|
2014-08-30 17:59:20 +02:00
|
|
|
"""
|
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 \
|
2014-08-30 21:50:26 +02:00
|
|
|
self.rect.top < mousey and self.rect.bottom > mousey:
|
|
|
|
|
2014-08-30 22:33:10 +02:00
|
|
|
if self.color == self.target:
|
2014-08-30 21:50:26 +02:00
|
|
|
self.has_been_pinned = True
|
2014-08-31 02:24:02 +02:00
|
|
|
self.is_still_falling = False
|
2014-08-31 10:04:20 +02:00
|
|
|
self.click.play()
|
2014-08-31 10:15:28 +02:00
|
|
|
return True, self.rect.center
|
2014-08-30 21:50:26 +02:00
|
|
|
else:
|
2014-08-31 10:04:20 +02:00
|
|
|
self.clickFail.play()
|
2014-08-31 10:15:28 +02:00
|
|
|
return False, (0, 0)
|
2014-08-31 10:04:20 +02:00
|
|
|
# BUG - isHit is called 4 times upon click which makes it return the
|
|
|
|
# first time but fail the consecutive times
|
|
|
|
#else:
|
|
|
|
# self.clickFail.play()
|
2014-08-30 21:20:42 +02:00
|
|
|
|
|
|
|
|
2014-08-31 02:24:02 +02:00
|
|
|
def isFalling(self):
|
|
|
|
return self.is_still_falling
|
2014-08-31 01:07:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def isPinned(self):
|
|
|
|
return self.has_been_pinned
|
2014-08-30 21:20:42 +02:00
|
|
|
|
2014-08-30 17:59:20 +02:00
|
|
|
|
2014-08-31 10:20:12 +02:00
|
|
|
def checkBoundaries(self):
|
|
|
|
if self.rect.right > self.screen_width or self.rect.left < 0:
|
|
|
|
self.vx = -self.vx
|
|
|
|
|
|
|
|
|
2014-08-30 18:20:49 +02:00
|
|
|
def move(self):
|
2014-08-30 23:34:15 +02:00
|
|
|
if not self.has_been_pinned:
|
2014-08-31 10:20:12 +02:00
|
|
|
self.checkBoundaries()
|
2014-08-30 21:20:42 +02:00
|
|
|
|
2014-08-30 23:34:15 +02:00
|
|
|
if self.hasLanded():
|
|
|
|
self.destroy()
|
2014-08-30 21:20:42 +02:00
|
|
|
|
2014-08-30 23:34:15 +02:00
|
|
|
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:
|
2014-08-31 01:07:12 +02:00
|
|
|
self.is_still_falling = False
|
2014-08-31 01:38:26 +02:00
|
|
|
print('DEBUG :: splash!')
|
2014-08-30 21:20:42 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2014-08-31 01:07:12 +02:00
|
|
|
def loadClick(self, sound=True):
|
|
|
|
if not sound:
|
|
|
|
return
|
|
|
|
return pygame.mixer.Sound("audio/click.wav")
|
|
|
|
|
|
|
|
|
2014-08-31 10:04:20 +02:00
|
|
|
def loadFailClick(self, sound=True):
|
|
|
|
if not sound:
|
|
|
|
return
|
|
|
|
return pygame.mixer.Sound("audio/poop.wav")
|
|
|
|
|
|
|
|
|
2014-08-30 21:50:26 +02:00
|
|
|
def destroy(self):
|
|
|
|
del(self)
|