Merge branch 'master' of github.com:lvl2/avocados

Conflicts:
	game.py
master
Raphaël Vinot 2014-08-30 22:14:42 +02:00
commit 0a7e6853df
4 changed files with 51 additions and 18 deletions

View File

@ -5,10 +5,13 @@ from support import operations
class Avocado:
def __init__(self, screen, color, size, select, 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
# I'd rather just not return an instance,
# but I don't know how to do that :(
if random.randint(0,1) == 0:
self.is_falling = False
self.has_been_pinned = False
return None
print('New avocado is ' + ','.join(str(color)))
@ -31,6 +34,7 @@ class Avocado:
self.vx = 10
self.vy = 10
self.is_falling = True
self.has_been_pinned = False
def blitme(self):
@ -42,24 +46,24 @@ class Avocado:
self.rect.y = random.randint(20, 70)
def collides(self, click):
def isHit(self, click):
"""
Checks whether this object collides with the given position
in click
of a mouse-click
"""
mousex, mousey = click
if self.rect.left < mousex and self.rect.right > mousex and \
self.rect.top < mousey and self.rect.bottom > mousey and \
self.color == self.select:
self.destroy()
return True
self.rect.top < mousey and self.rect.bottom > mousey:
if self.color == self.select:
self.has_been_pinned = True
return True
else:
return False
def destroy(self):
"""destroys this object"""
del(self)
def exists(self):
return self.is_falling
return not self.has_been_pinned and self.is_falling
def move(self):
@ -80,13 +84,18 @@ class Avocado:
print('platch')
return True
def mute(self,mute=False):
if not sound:
return
if mute:
pygame.mixer.music.set_volume(0.0)
else:
pygame.mixer.music.set_volume(0.5)
def playLevel(self,lvl=1):
if not sound:
return
if lvl == 1:
pygame.mixer.music.load("""audio/level1.wav""")
elif lvl == 2:
@ -96,8 +105,15 @@ class Avocado:
pygame.mixer.music.play()
def fade(self):
if not sound:
return
pygame.mixer.music.fadeout(3000)
def loadClick(self):
if not sound:
return
self.click = pygame.mixer.Sound("audio/click.wav")
return self.click
def destroy(self):
del(self)

30
game.py
View File

@ -10,6 +10,7 @@ from pygame.locals import *
from support.colors import *
from interface import hud
def initialize_screen():
displayInfo = pygame.display.Info()
zoom = 1.3
@ -21,15 +22,21 @@ def initialize_screen():
def main():
pygame.init()
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
noSound = False
except:
print("Setting no sound :(")
noSound = True
pygame.display.set_caption('Pin Avo, the Cado!')
clock = pygame.time.Clock()
# initialize_screen() won't work for dualscreen
#size = initialize_screen()
size = (800, 600)
bg = BLACK
bg = pygame.image.load("img/background.png")
desired_fps = 10
font = pygame.font.Font(None, 40)
@ -59,7 +66,11 @@ def main():
while running:
time_passed = clock.tick(desired_fps)
fps = clock.get_fps()
screen.fill(bg)
if type(bg) is tuple:
screen.fill(bg)
else:
screen.blit(pygame.transform.scale(bg,(800,600)),(0,0))
# Let's add the lawyer and have him announce a color
fullegast.setColor(color)
@ -92,9 +103,11 @@ def main():
for i in range(avocados_in_game, level):
avocolor = colors[random.randint(0, 3)]
avosize = (50, 50) # should we randomize this?
a = avocado.Avocado(screen, avocolor, avosize, color)
a = avocado.Avocado(screen, avocolor, avosize, color, noSound)
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() ]
for a in avocados:
a.move()
@ -105,9 +118,12 @@ def main():
# Collision detection
if event.type == MOUSEBUTTONDOWN:
for avo in avocados:
if avo.collides(pygame.mouse.get_pos()):
hit = avo.isHit(pygame.mouse.get_pos())
if hit:
score += 100
avo.init_pos()
elif hit == False:
score -= 50
# Had enough of this?
if event.type == pygame.QUIT:
running = False

BIN
img/background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 505 KiB

View File

@ -0,0 +1 @@
hg+https://bitbucket.org/pygame/pygame