2014-08-30 15:28:07 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Avocados and stuff
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os, random
|
|
|
|
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 18:46:55 +02:00
|
|
|
def initialize_screen():
|
|
|
|
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 15:28:07 +02:00
|
|
|
def main():
|
|
|
|
pygame.init()
|
2014-08-30 22:02:08 +02:00
|
|
|
try:
|
|
|
|
pygame.mixer.init()
|
|
|
|
pygame.mixer.music.set_volume(0.5)
|
|
|
|
noSound = False
|
|
|
|
except:
|
|
|
|
print("Setting no sound :(")
|
|
|
|
noSound = True
|
|
|
|
|
2014-08-30 20:47:21 +02:00
|
|
|
pygame.display.set_caption('Pin Avo, the Cado!')
|
2014-08-30 15:28:07 +02:00
|
|
|
clock = pygame.time.Clock()
|
2014-08-30 16:28:56 +02:00
|
|
|
|
2014-08-30 18:46:55 +02:00
|
|
|
# initialize_screen() won't work for dualscreen
|
|
|
|
#size = initialize_screen()
|
|
|
|
size = (800, 600)
|
2014-08-30 21:50:50 +02:00
|
|
|
bg = pygame.image.load("img/background.png")
|
2014-08-30 21:20:42 +02:00
|
|
|
desired_fps = 10
|
2014-08-30 16:28:56 +02:00
|
|
|
font = pygame.font.Font(None, 40)
|
2014-08-30 18:46:55 +02:00
|
|
|
|
|
|
|
# I don't know, should we move this text out of the way?
|
2014-08-30 16:28:56 +02:00
|
|
|
game_over = font.render('GAME OVER', 0, RED)
|
2014-08-30 18:46:55 +02:00
|
|
|
|
|
|
|
# initialize the game canvas
|
|
|
|
screen = pygame.display.set_mode(size)
|
|
|
|
|
2014-08-30 20:47:21 +02:00
|
|
|
# initialize the HUD class and the lawyer
|
|
|
|
the_hud = hud.Hud(screen)
|
2014-08-30 18:46:55 +02:00
|
|
|
fullegast = lawyer.Lawyer(screen)
|
2014-08-30 16:28:56 +02:00
|
|
|
|
2014-08-30 20:47:21 +02:00
|
|
|
# Well, we want this to select between several colors, so we need a list
|
|
|
|
# of colors, right?
|
|
|
|
colors = [BLUE, GREEN, RED, YELLOW]
|
2014-08-30 21:20:42 +02:00
|
|
|
selected = random.randint(0, 3)
|
2014-08-30 20:47:21 +02:00
|
|
|
color = colors[selected]
|
2014-08-30 16:28:56 +02:00
|
|
|
|
2014-08-30 16:06:02 +02:00
|
|
|
score = 0
|
2014-08-30 16:53:03 +02:00
|
|
|
time = 15
|
2014-08-30 18:43:19 +02:00
|
|
|
level = 5
|
2014-08-30 15:28:07 +02:00
|
|
|
|
2014-08-30 15:38:47 +02:00
|
|
|
running = True
|
2014-08-30 16:28:56 +02:00
|
|
|
timeleft = time
|
2014-08-30 18:43:19 +02:00
|
|
|
avocados = []
|
2014-08-30 15:28:07 +02:00
|
|
|
while running:
|
2014-08-30 16:28:56 +02:00
|
|
|
time_passed = clock.tick(desired_fps)
|
2014-08-30 16:06:02 +02:00
|
|
|
fps = clock.get_fps()
|
2014-08-30 21:50:50 +02:00
|
|
|
if type(bg) is tuple:
|
|
|
|
screen.fill(bg)
|
|
|
|
else:
|
|
|
|
screen.blit(pygame.transform.scale(bg,(800,600)),(0,0))
|
|
|
|
|
2014-08-30 16:53:03 +02:00
|
|
|
|
2014-08-30 20:47:21 +02:00
|
|
|
# Let's add the lawyer and have him announce a color
|
|
|
|
fullegast.setColor(color)
|
2014-08-30 18:46:55 +02:00
|
|
|
fullegast.blitme()
|
|
|
|
|
2014-08-30 16:28:56 +02:00
|
|
|
timeleft -= time_passed / 1000
|
2014-08-30 17:05:01 +02:00
|
|
|
timeleft = round(timeleft, 2)
|
2014-08-30 16:28:56 +02:00
|
|
|
if timeleft <= 0:
|
2014-08-30 18:46:55 +02:00
|
|
|
screen_width, screen_height = size
|
2014-08-30 16:28:56 +02:00
|
|
|
screen.blit(game_over, (screen_width/3, screen_height/2))
|
2014-08-30 17:05:01 +02:00
|
|
|
displaytime = 'Timed out!'
|
2014-08-30 21:11:10 +02:00
|
|
|
pygame.display.flip()
|
|
|
|
continue
|
2014-08-30 17:05:01 +02:00
|
|
|
else:
|
|
|
|
displaytime = timeleft
|
2014-08-30 16:28:56 +02:00
|
|
|
|
2014-08-30 17:05:01 +02:00
|
|
|
# Redraw the HUD
|
2014-08-30 20:47:21 +02:00
|
|
|
the_hud.draw_hud(score, displaytime, round(fps, 2))
|
2014-08-30 15:28:07 +02:00
|
|
|
|
2014-08-30 17:59:20 +02:00
|
|
|
# Initialize a number of avocados, depending on the level
|
2014-08-30 21:20:42 +02:00
|
|
|
avocados_in_game = len(avocados)
|
|
|
|
print(avocados_in_game)
|
|
|
|
if avocados_in_game != level:
|
|
|
|
for i in range(avocados_in_game, level):
|
|
|
|
avocolor = colors[random.randint(0, 3)]
|
|
|
|
avosize = (50, 50) # should we randomize this?
|
2014-08-30 22:02:08 +02:00
|
|
|
a = avocado.Avocado(screen, avocolor, avosize, color, noSound)
|
2014-08-30 18:43:19 +02:00
|
|
|
avocados.append(a)
|
2014-08-30 17:59:20 +02:00
|
|
|
|
2014-08-30 21:50:26 +02:00
|
|
|
# Remove avocados from the list if they no longer exist
|
|
|
|
# E.g. have been pinned or fallen down
|
2014-08-30 21:20:42 +02:00
|
|
|
avocados[:] = [ x for x in avocados if x.exists() ]
|
2014-08-30 18:20:49 +02:00
|
|
|
for a in avocados:
|
2014-08-30 21:20:42 +02:00
|
|
|
a.move()
|
|
|
|
a.blitme()
|
2014-08-30 17:59:20 +02:00
|
|
|
|
2014-08-30 17:05:01 +02:00
|
|
|
# Catch events
|
2014-08-30 15:28:07 +02:00
|
|
|
for event in pygame.event.get():
|
2014-08-30 18:46:55 +02:00
|
|
|
# Collision detection
|
2014-08-30 17:05:01 +02:00
|
|
|
if event.type == MOUSEBUTTONDOWN:
|
2014-08-30 17:59:20 +02:00
|
|
|
for avo in avocados:
|
2014-08-30 21:50:26 +02:00
|
|
|
hit = avo.isHit(pygame.mouse.get_pos())
|
|
|
|
if hit:
|
2014-08-30 17:59:20 +02:00
|
|
|
score += 100
|
2014-08-30 21:50:26 +02:00
|
|
|
elif hit == False:
|
|
|
|
score -= 50
|
|
|
|
|
2014-08-30 18:46:55 +02:00
|
|
|
# Had enough of this?
|
2014-08-30 15:28:07 +02:00
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
running = False
|
|
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
|
2014-08-30 16:28:56 +02:00
|
|
|
|
2014-08-30 15:28:07 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|