Do collision detection on the glassball
parent
c6e35bf61e
commit
b702494c65
27
avocado.py
27
avocado.py
|
@ -5,14 +5,16 @@ from support import operations
|
||||||
|
|
||||||
class Avocado:
|
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
|
# Set up our instance variables
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.color = color
|
|
||||||
self.target = target
|
|
||||||
self.screen_width, self.screen_height = screen.get_size()
|
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
|
# Initialize the image
|
||||||
self.i = pygame.image.load(filename).convert_alpha()
|
self.i = pygame.image.load(filename).convert_alpha()
|
||||||
|
@ -81,7 +83,24 @@ class Avocado:
|
||||||
|
|
||||||
|
|
||||||
def checkBoundaries(self):
|
def checkBoundaries(self):
|
||||||
|
# Checking screen boundaries
|
||||||
if self.rect.right > self.screen_width or self.rect.left < 0:
|
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
|
self.vx = -self.vx
|
||||||
|
|
||||||
|
|
||||||
|
|
13
crystal.py
13
crystal.py
|
@ -12,11 +12,18 @@ class Crystal:
|
||||||
def __init__(self, screen):
|
def __init__(self, screen):
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
screen_width, screen_height = screen.get_size()
|
screen_width, screen_height = screen.get_size()
|
||||||
self.imageCenterX = self.screen.get_rect().centerx
|
self.rect = self.screen.get_rect()
|
||||||
self.imageCenterY = self.screen.get_rect().centery
|
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):
|
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):
|
def setColor(self, color):
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
9
game.py
9
game.py
|
@ -140,6 +140,7 @@ class TheGame:
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
score = 0
|
score = 0
|
||||||
|
boundaries = []
|
||||||
|
|
||||||
# We could use this list for redrawing only this part
|
# We could use this list for redrawing only this part
|
||||||
# of the screen instead of all of it
|
# of the screen instead of all of it
|
||||||
|
@ -148,6 +149,7 @@ class TheGame:
|
||||||
# initialize the HUD class and the lawyer
|
# initialize the HUD class and the lawyer
|
||||||
the_hud = hud.Hud(self.screen)
|
the_hud = hud.Hud(self.screen)
|
||||||
crystalBall = crystal.Crystal(self.screen)
|
crystalBall = crystal.Crystal(self.screen)
|
||||||
|
boundaries.append(crystalBall.getBoundaries())
|
||||||
|
|
||||||
# Initial color indication
|
# Initial color indication
|
||||||
color = self.chooseRandomColor()
|
color = self.chooseRandomColor()
|
||||||
|
@ -203,13 +205,12 @@ class TheGame:
|
||||||
if avocadosInGame < avocadosWanted:
|
if avocadosInGame < avocadosWanted:
|
||||||
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
|
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
|
||||||
if random.randint(0, probability) < 3:
|
if random.randint(0, probability) < 3:
|
||||||
avocolor = self.chooseRandomColor()
|
properties = {'color': self.chooseRandomColor(), 'size': (50,50)}
|
||||||
avosize = (50, 50) # should we randomize this?
|
|
||||||
# Spawn a new avocado
|
# Spawn a new avocado
|
||||||
a = avocado.Avocado(
|
a = avocado.Avocado(
|
||||||
self.screen,
|
self.screen,
|
||||||
avocolor,
|
boundaries,
|
||||||
avosize,
|
properties,
|
||||||
color,
|
color,
|
||||||
self.level
|
self.level
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue