2014-08-30 15:28:07 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Avocados and stuff
|
|
|
|
"""
|
|
|
|
|
2014-08-30 22:42:20 +02:00
|
|
|
import os, random, sys
|
2014-08-30 15:28:07 +02:00
|
|
|
import pygame
|
2014-08-30 18:46:55 +02:00
|
|
|
import avocado, lawyer
|
2014-08-30 15:28:07 +02:00
|
|
|
from pygame.locals import *
|
2014-08-30 16:28:56 +02:00
|
|
|
from support.colors import *
|
2014-08-30 15:52:27 +02:00
|
|
|
from interface import hud
|
2014-08-30 15:28:07 +02:00
|
|
|
|
2014-08-30 22:02:17 +02:00
|
|
|
|
2014-08-30 22:16:41 +02:00
|
|
|
class TheGame:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
""" foo """
|
|
|
|
self.colors = [BLUE, GREEN, RED, YELLOW]
|
2014-08-30 23:03:14 +02:00
|
|
|
pygame.init()
|
|
|
|
try:
|
|
|
|
pygame.mixer.init()
|
|
|
|
pygame.mixer.music.set_volume(0.5)
|
|
|
|
noSound = False
|
|
|
|
except:
|
|
|
|
print("Y U NO sound? :(")
|
|
|
|
noSound = True
|
|
|
|
|
|
|
|
pygame.display.set_caption('Pin Avo, the Cado!')
|
2014-08-30 22:16:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def initialize_screen(self):
|
|
|
|
displayInfo = pygame.display.Info()
|
|
|
|
zoom = 1.3
|
|
|
|
WIDTH = int(displayInfo.current_w / zoom)
|
|
|
|
HEIGHT = int(displayInfo.current_h / zoom)
|
|
|
|
return (WIDTH, HEIGHT)
|
|
|
|
|
|
|
|
|
2014-08-30 22:59:38 +02:00
|
|
|
def mute(self,mute=False,sound=True):
|
|
|
|
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,sound=True):
|
|
|
|
if not sound:
|
|
|
|
return
|
|
|
|
if lvl == 1:
|
|
|
|
pygame.mixer.music.load("""audio/level1.wav""")
|
|
|
|
elif lvl == 2:
|
|
|
|
pygame.mixer.music.load("""audio/level2.wav""")
|
|
|
|
elif lvl == 3:
|
|
|
|
pygame.mixer.music.load("""audio/level3.wav""")
|
|
|
|
pygame.mixer.music.play()
|
|
|
|
|
|
|
|
|
|
|
|
def fade(self, sound=True):
|
|
|
|
if not sound:
|
|
|
|
return
|
|
|
|
pygame.mixer.music.fadeout(3000)
|
|
|
|
|
|
|
|
|
|
|
|
def loadClick(self, sound=True):
|
|
|
|
if not sound:
|
|
|
|
return
|
|
|
|
self.click = pygame.mixer.Sound("audio/click.wav")
|
|
|
|
return self.click
|
|
|
|
|
|
|
|
|
2014-08-30 22:16:41 +02:00
|
|
|
def main(self):
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
# initialize_screen() won't work for dualscreen
|
|
|
|
#size = initialize_screen()
|
|
|
|
size = (800, 600)
|
|
|
|
bg = pygame.image.load("img/background.png")
|
2014-08-30 22:48:49 +02:00
|
|
|
desired_fps = 15
|
|
|
|
multiplier = 6
|
|
|
|
score = 0
|
|
|
|
time = timeleft = 30
|
|
|
|
level = 1
|
2014-08-30 22:16:41 +02:00
|
|
|
font = pygame.font.Font(None, 40)
|
2014-08-30 23:05:20 +02:00
|
|
|
avoClick = game.loadClick()
|
2014-08-30 22:16:41 +02:00
|
|
|
|
|
|
|
# I don't know, should we move this text out of the way?
|
|
|
|
game_over = font.render('GAME OVER', 0, RED)
|
|
|
|
|
|
|
|
# initialize the game canvas
|
|
|
|
screen = pygame.display.set_mode(size)
|
|
|
|
|
|
|
|
# initialize the HUD class and the lawyer
|
|
|
|
the_hud = hud.Hud(screen)
|
|
|
|
fullegast = lawyer.Lawyer(screen)
|
|
|
|
|
|
|
|
# Initial color
|
|
|
|
color = self.chooseRandomColor()
|
2014-08-30 20:47:21 +02:00
|
|
|
fullegast.setColor(color)
|
2014-08-30 22:16:41 +02:00
|
|
|
|
|
|
|
avocados = []
|
2014-08-30 22:33:10 +02:00
|
|
|
running = True
|
2014-08-30 22:16:41 +02:00
|
|
|
while running:
|
2014-08-30 22:33:10 +02:00
|
|
|
|
2014-08-30 22:16:41 +02:00
|
|
|
time_passed = clock.tick(desired_fps)
|
|
|
|
fps = clock.get_fps()
|
2014-08-30 22:48:49 +02:00
|
|
|
|
2014-08-30 22:16:41 +02:00
|
|
|
if type(bg) is tuple:
|
|
|
|
screen.fill(bg)
|
|
|
|
else:
|
2014-08-30 22:48:49 +02:00
|
|
|
screen.blit(pygame.transform.scale(bg, (800, 600)), (0, 0))
|
|
|
|
|
|
|
|
# Next level?
|
|
|
|
if score >= 500:
|
|
|
|
score = 0
|
|
|
|
level += 1
|
|
|
|
print('DEBUG :: Level ' + string(level))
|
2014-08-30 22:16:41 +02:00
|
|
|
|
2014-08-30 22:33:10 +02:00
|
|
|
# Let's add the lawyer
|
2014-08-30 22:16:41 +02:00
|
|
|
fullegast.blitme()
|
|
|
|
|
|
|
|
timeleft -= time_passed / 1000
|
|
|
|
timeleft = round(timeleft, 2)
|
|
|
|
if timeleft <= 0:
|
|
|
|
screen_width, screen_height = size
|
|
|
|
screen.blit(game_over, (screen_width/3, screen_height/2))
|
|
|
|
displaytime = 'Timed out!'
|
|
|
|
pygame.display.flip()
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
displaytime = timeleft
|
|
|
|
|
|
|
|
# Redraw the HUD
|
|
|
|
the_hud.draw_hud(score, displaytime, round(fps, 2))
|
|
|
|
|
|
|
|
# Initialize a number of avocados, depending on the level
|
|
|
|
avocados_in_game = len(avocados)
|
2014-08-30 22:48:49 +02:00
|
|
|
avocadosWanted = level * multiplier
|
|
|
|
if avocados_in_game < avocadosWanted:
|
|
|
|
for i in range(avocados_in_game, avocadosWanted):
|
2014-08-30 22:16:41 +02:00
|
|
|
avocolor = self.chooseRandomColor()
|
|
|
|
avosize = (50, 50) # should we randomize this?
|
2014-08-30 22:59:38 +02:00
|
|
|
a = avocado.Avocado(screen, avocolor, avosize, color)
|
2014-08-30 22:16:41 +02:00
|
|
|
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:
|
2014-08-30 22:33:10 +02:00
|
|
|
a.updateTargetColor(color)
|
2014-08-30 22:16:41 +02:00
|
|
|
a.move()
|
|
|
|
a.blitme()
|
|
|
|
|
|
|
|
# Catch events
|
|
|
|
for event in pygame.event.get():
|
|
|
|
# Collision detection
|
|
|
|
if event.type == MOUSEBUTTONDOWN:
|
2014-08-30 23:05:20 +02:00
|
|
|
avoClick.play()
|
2014-08-30 22:16:41 +02:00
|
|
|
for avo in avocados:
|
|
|
|
hit = avo.isHit(pygame.mouse.get_pos())
|
|
|
|
if hit:
|
|
|
|
score += 100
|
2014-08-30 22:33:10 +02:00
|
|
|
color = self.chooseRandomColor()
|
|
|
|
fullegast.setColor(color)
|
2014-08-30 22:16:41 +02:00
|
|
|
elif hit == False:
|
|
|
|
score -= 50
|
|
|
|
|
|
|
|
# Had enough of this?
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
running = False
|
2014-08-30 22:42:20 +02:00
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2014-08-30 22:16:41 +02:00
|
|
|
|
2014-08-30 21:11:10 +02:00
|
|
|
pygame.display.flip()
|
2014-08-30 22:16:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def chooseRandomColor(self):
|
|
|
|
selected = random.randint(0, 3)
|
|
|
|
return self.colors[selected]
|
2014-08-30 15:28:07 +02:00
|
|
|
|
2014-08-30 16:28:56 +02:00
|
|
|
|
2014-08-30 15:28:07 +02:00
|
|
|
if __name__ == '__main__':
|
2014-08-30 22:16:41 +02:00
|
|
|
game = TheGame()
|
2014-08-30 23:03:14 +02:00
|
|
|
game.playLevel()
|
2014-08-30 22:16:41 +02:00
|
|
|
game.main()
|