Do collision detection on the glassball

master
David Raison 2014-08-31 11:48:20 +02:00
parent c6e35bf61e
commit b702494c65
3 changed files with 38 additions and 11 deletions

View File

@ -5,14 +5,16 @@ from support import operations
class Avocado:
def __init__(self, screen, color, size, target, level, filename='img/AvoCado_0.png'):
def __init__(self, screen, boundaries, properties, target, level, filename='img/AvoCado_0.png'):
# Set up our instance variables
self.screen = screen
self.color = color
self.target = target
self.screen_width, self.screen_height = screen.get_size()
self.w, self.y = size
color = properties['color']
self.w, self.y = properties['size']
self.target = target
self.boundaries = boundaries
self.checkObstacle = True
# Initialize the image
self.i = pygame.image.load(filename).convert_alpha()
@ -81,9 +83,26 @@ class Avocado:
def checkBoundaries(self):
# Checking screen boundaries
if self.rect.right > self.screen_width or self.rect.left < 0:
self.checkObstacle = True
self.vx = -self.vx
# Checking for obstacle collisions
for obstacle in self.boundaries:
left, top, width, height = obstacle
right = left + width
bottom = top + height
if self.checkObstacle \
and (self.rect.right < right and self.rect.left > left):
self.checkObstacle = False
if self.checkObstacle \
and ((self.rect.right > right and self.rect.left < right) \
or (self.rect.left < left and self.rect.right > left)):
self.vx = -self.vx
def move(self):
if not self.has_been_pinned:

View File

@ -12,11 +12,18 @@ class Crystal:
def __init__(self, screen):
self.screen = screen
screen_width, screen_height = screen.get_size()
self.imageCenterX = self.screen.get_rect().centerx
self.imageCenterY = self.screen.get_rect().centery
self.rect = self.screen.get_rect()
self.imageCenterX = self.rect.centerx
self.imageCenterY = self.rect.centery
self.pos = (self.imageCenterX-100,self.imageCenterY-5,200,183)
def getBoundaries(self):
return self.pos
def blitme(self):
pygame.draw.ellipse(self.screen, self.color, (self.imageCenterX-100,self.imageCenterY-5,200,183), 0)
pygame.draw.ellipse(self.screen, self.color, self.pos, 0)
def setColor(self, color):
self.color = color

View File

@ -140,6 +140,7 @@ class TheGame:
def main(self):
score = 0
boundaries = []
# We could use this list for redrawing only this part
# of the screen instead of all of it
@ -148,6 +149,7 @@ class TheGame:
# initialize the HUD class and the lawyer
the_hud = hud.Hud(self.screen)
crystalBall = crystal.Crystal(self.screen)
boundaries.append(crystalBall.getBoundaries())
# Initial color indication
color = self.chooseRandomColor()
@ -203,13 +205,12 @@ class TheGame:
if avocadosInGame < avocadosWanted:
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
if random.randint(0, probability) < 3:
avocolor = self.chooseRandomColor()
avosize = (50, 50) # should we randomize this?
properties = {'color': self.chooseRandomColor(), 'size': (50,50)}
# Spawn a new avocado
a = avocado.Avocado(
self.screen,
avocolor,
avosize,
boundaries,
properties,
color,
self.level
)