Make the avocados clickable

master
Raphaël Vinot 2014-08-30 21:11:10 +02:00
parent 85a79e07af
commit e2075f33a5
2 changed files with 17 additions and 7 deletions

View File

@ -21,31 +21,38 @@ class Avocado:
self.init_pos() self.init_pos()
self.step_x = 10 self.step_x = 10
self.step_y = 10 self.step_y = 10
self.is_falling = True self.is_destroyed = False
def init_pos(self): def init_pos(self):
self.pycard.x = random.randint(0, self.screen_width) self.pycard.x = random.randint(0, self.screen_width)
self.pycard.y = random.randint(20, 70) self.pycard.y = random.randint(20, 70)
self.is_destroyed = False
def collides(self, click): def collides(self, click):
""" """
Checks whether this object collides with the given position Checks whether this object collides with the given position
in click in click
""" """
return True 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
#if collision then self.destroy() #if collision then self.destroy()
def destroy(self): def destroy(self):
"""destroys this object""" """destroys this object"""
self.is_destroyed = True
def move(self): def move(self):
if self.pycard.right > self.screen_width or self.pycard.left < 0: if self.pycard.right > self.screen_width or self.pycard.left < 0:
self.step_x = -self.step_x self.step_x = -self.step_x
if self.pycard.bottom > self.screen_height or self.pycard.top < 0: if self.is_destroyed or self.pycard.bottom > self.screen_height \
or self.pycard.top < 0:
print('platch') print('platch')
return False return False
self.is_falling = False print(self.pycard.x, self.pycard.y)
self.pycard.x += self.step_x self.pycard.x += self.step_x
self.pycard.y += self.step_y self.pycard.y += self.step_y
return True return True

View File

@ -28,7 +28,7 @@ def main():
#size = initialize_screen() #size = initialize_screen()
size = (800, 600) size = (800, 600)
bg = BLACK bg = BLACK
desired_fps = 60 desired_fps = 10
font = pygame.font.Font(None, 40) font = pygame.font.Font(None, 40)
# I don't know, should we move this text out of the way? # I don't know, should we move this text out of the way?
@ -66,10 +66,13 @@ def main():
timeleft -= time_passed / 1000 timeleft -= time_passed / 1000
timeleft = round(timeleft, 2) timeleft = round(timeleft, 2)
if timeleft <= 0: if timeleft <= 0:
screen_width, screen_height = size screen_width, screen_height = size
screen.blit(game_over, (screen_width/3, screen_height/2)) screen.blit(game_over, (screen_width/3, screen_height/2))
displaytime = 'Timed out!' displaytime = 'Timed out!'
pygame.display.flip()
continue
else: else:
displaytime = timeleft displaytime = timeleft
@ -97,7 +100,7 @@ def main():
for avo in avocados: for avo in avocados:
if avo.collides(pygame.mouse.get_pos()): if avo.collides(pygame.mouse.get_pos()):
score += 100 score += 100
avo.init_pos()
# Had enough of this? # Had enough of this?
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False