avocados/game.py

81 lines
2.0 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 17:59:20 +02:00
import avocado
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:43:19 +02:00
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
# Move this outside the main code?
screen_width = 800
screen_height = 600
2014-08-30 18:43:19 +02:00
screen = pygame.display.set_mode((screen_width, screen_height))
2014-08-30 16:53:03 +02:00
bg = BLACK
2014-08-30 18:20:49 +02:00
desired_fps = 5
2014-08-30 16:28:56 +02:00
font = pygame.font.Font(None, 40)
game_over = font.render('GAME OVER', 0, RED)
2014-08-30 16:53:03 +02:00
my_hud = hud.Hud((screen_width, screen_height))
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 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:
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):
a = avocado.Avocado((screen_width, screen_height))
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)
# Catch events
2014-08-30 15:28:07 +02:00
for event in pygame.event.get():
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 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()