commit
e5a7107903
28
avocado.py
28
avocado.py
|
@ -7,8 +7,11 @@ class Avocado:
|
||||||
|
|
||||||
def __init__(self, screen, color, size, select, sound=True, filename='img/AvoCado_0.png'):
|
def __init__(self, screen, color, size, select, sound=True, filename='img/AvoCado_0.png'):
|
||||||
# We randomly decide whether we should instanciate or not
|
# We randomly decide whether we should instanciate or not
|
||||||
|
# I'd rather just not return an instance,
|
||||||
|
# but I don't know how to do that :(
|
||||||
if random.randint(0,1) == 0:
|
if random.randint(0,1) == 0:
|
||||||
self.is_falling = False
|
self.is_falling = False
|
||||||
|
self.has_been_pinned = False
|
||||||
return None
|
return None
|
||||||
|
|
||||||
print('New avocado is ' + ','.join(str(color)))
|
print('New avocado is ' + ','.join(str(color)))
|
||||||
|
@ -31,6 +34,7 @@ class Avocado:
|
||||||
self.vx = 10
|
self.vx = 10
|
||||||
self.vy = 10
|
self.vy = 10
|
||||||
self.is_falling = True
|
self.is_falling = True
|
||||||
|
self.has_been_pinned = False
|
||||||
|
|
||||||
|
|
||||||
def blitme(self):
|
def blitme(self):
|
||||||
|
@ -42,24 +46,24 @@ class Avocado:
|
||||||
self.rect.y = random.randint(20, 70)
|
self.rect.y = random.randint(20, 70)
|
||||||
|
|
||||||
|
|
||||||
def collides(self, click):
|
def isHit(self, click):
|
||||||
"""
|
"""
|
||||||
Checks whether this object collides with the given position
|
Checks whether this object collides with the given position
|
||||||
in click
|
of a mouse-click
|
||||||
"""
|
"""
|
||||||
mousex, mousey = click
|
mousex, mousey = click
|
||||||
if self.rect.left < mousex and self.rect.right > mousex and \
|
if self.rect.left < mousex and self.rect.right > mousex and \
|
||||||
self.rect.top < mousey and self.rect.bottom > mousey and \
|
self.rect.top < mousey and self.rect.bottom > mousey:
|
||||||
self.color == self.select:
|
|
||||||
self.destroy()
|
if self.color == self.select:
|
||||||
return True
|
self.has_been_pinned = True
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def destroy(self):
|
|
||||||
"""destroys this object"""
|
|
||||||
del(self)
|
|
||||||
|
|
||||||
def exists(self):
|
def exists(self):
|
||||||
return self.is_falling
|
return not self.has_been_pinned and self.is_falling
|
||||||
|
|
||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
|
@ -80,6 +84,7 @@ class Avocado:
|
||||||
print('platch')
|
print('platch')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def mute(self,mute=False):
|
def mute(self,mute=False):
|
||||||
if not sound:
|
if not sound:
|
||||||
return
|
return
|
||||||
|
@ -109,3 +114,6 @@ class Avocado:
|
||||||
return
|
return
|
||||||
self.click = pygame.mixer.Sound("audio/click.wav")
|
self.click = pygame.mixer.Sound("audio/click.wav")
|
||||||
return self.click
|
return self.click
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
del(self)
|
||||||
|
|
12
game.py
12
game.py
|
@ -10,6 +10,7 @@ from pygame.locals import *
|
||||||
from support.colors import *
|
from support.colors import *
|
||||||
from interface import hud
|
from interface import hud
|
||||||
|
|
||||||
|
|
||||||
def initialize_screen():
|
def initialize_screen():
|
||||||
displayInfo = pygame.display.Info()
|
displayInfo = pygame.display.Info()
|
||||||
zoom = 1.3
|
zoom = 1.3
|
||||||
|
@ -77,8 +78,6 @@ 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))
|
||||||
|
@ -101,6 +100,8 @@ def main():
|
||||||
a = avocado.Avocado(screen, avocolor, avosize, color, noSound)
|
a = avocado.Avocado(screen, avocolor, avosize, color, noSound)
|
||||||
avocados.append(a)
|
avocados.append(a)
|
||||||
|
|
||||||
|
# Remove avocados from the list if they no longer exist
|
||||||
|
# E.g. have been pinned or fallen down
|
||||||
avocados[:] = [ x for x in avocados if x.exists() ]
|
avocados[:] = [ x for x in avocados if x.exists() ]
|
||||||
for a in avocados:
|
for a in avocados:
|
||||||
a.move()
|
a.move()
|
||||||
|
@ -111,9 +112,12 @@ def main():
|
||||||
# Collision detection
|
# Collision detection
|
||||||
if event.type == MOUSEBUTTONDOWN:
|
if event.type == MOUSEBUTTONDOWN:
|
||||||
for avo in avocados:
|
for avo in avocados:
|
||||||
if avo.collides(pygame.mouse.get_pos()):
|
hit = avo.isHit(pygame.mouse.get_pos())
|
||||||
|
if hit:
|
||||||
score += 100
|
score += 100
|
||||||
avo.init_pos()
|
elif hit == False:
|
||||||
|
score -= 50
|
||||||
|
|
||||||
# Had enough of this?
|
# Had enough of this?
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
hg+https://bitbucket.org/pygame/pygame
|
Loading…
Reference in New Issue