avocados/game.py

104 lines
2.5 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()
pygame.display.set_caption('Pin the Avocados!')
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 16:28:56 +02:00
desired_fps = 60
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)
# initialize the HUD class
my_hud = hud.Hud(size)
# initialize our lawyer
fullegast = lawyer.Lawyer(screen)
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 18:46:55 +02:00
# Let's add the lawyer
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!'
else:
displaytime = timeleft
2014-08-30 16:28:56 +02:00
# Redraw the HUD
2014-08-30 18:43:19 +02:00
chud = my_hud.draw_hud(score, displaytime, round(fps, 2))
screen.blit(chud, (10, 10))
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 18:43:19 +02:00
if len(avocados) != level:
avocados = []
for i in range(0, level):
2014-08-30 18:49:41 +02:00
a = avocado.Avocado(size)
2014-08-30 18:43:19 +02:00
avocados.append(a)
2014-08-30 17:59:20 +02:00
2014-08-30 18:20:49 +02:00
for a in avocados:
2014-08-30 18:43:19 +02:00
if not a.move():
a.reset()
2014-08-30 18:20:49 +02:00
screen.blit(a.image, a.pycard)
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 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()