avocados/game.py

120 lines
3.3 KiB
Python
Raw Normal View History

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 *
from interface import hud
2014-08-30 15:28:07 +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 21:41:53 +02:00
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
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 16:53:03 +02:00
bg = BLACK
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
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 16:53:03 +02:00
screen.fill(bg)
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
timeleft = round(timeleft, 2)
2014-08-30 16:53:03 +02:00
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))
displaytime = 'Timed out!'
# Redraw the HUD
the_hud.draw_hud(score, displaytime, round(fps, 2))
2014-08-30 21:11:10 +02:00
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
2014-08-30 21:11:10 +02:00
continue
else:
displaytime = timeleft
2014-08-30 16:28:56 +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?
a = avocado.Avocado(screen, avocolor, avosize, color)
2014-08-30 18:43:19 +02:00
avocados.append(a)
2014-08-30 17:59:20 +02:00
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
# 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
if event.type == MOUSEBUTTONDOWN:
2014-08-30 17:59:20 +02:00
for avo in avocados:
if avo.collides(pygame.mouse.get_pos()):
score += 100
2014-08-30 21:11:10 +02:00
avo.init_pos()
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()