Merge branch 'master' of github.com:lvl2/avocados

master
Steve Clement 2014-08-30 17:11:35 +02:00
commit 4fddd144d4
2 changed files with 57 additions and 31 deletions

48
game.py
View File

@ -7,37 +7,57 @@ import os, random
import pygame
from pygame.locals import *
from support import colors
from support.colors import *
from interface import hud
# Move this outside
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
bg = (0,0,0)
screen.fill(bg)
def main():
pygame.init()
pygame.display.set_caption('Avocados')
pygame.display.set_caption('Pin the Avocados!')
clock = pygame.time.Clock()
# Move this outside the main code?
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
bg = BLACK
desired_fps = 60
font = pygame.font.Font(None, 40)
game_over = font.render('GAME OVER', 0, RED)
my_hud = hud.Hud((screen_width, screen_height))
score = 0
time = 33
time = 15
running = True
timeleft = time
while running:
# Limit to 50 fps
time_passed = clock.tick(30)
time_passed = clock.tick(desired_fps)
fps = clock.get_fps()
screen.fill(bg)
my_hud = hud.draw_hud(score, time, fps)
screen.blit(my_hud, (10,10))
timeleft -= time_passed / 1000
timeleft = round(timeleft, 2)
if timeleft <= 0:
screen.blit(game_over, (screen_width/3, screen_height/2))
displaytime = 'Timed out!'
else:
displaytime = timeleft
# Redraw the HUD
chud = my_hud.draw_hud(score, displaytime, round(fps,2))
screen.blit(chud, (10,10))
# Catch events
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
score += 100
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
if __name__ == '__main__':
main()

View File

@ -4,24 +4,30 @@ import pygame
from pygame.locals import *
from support.colors import *
def draw_hud(score, timeleft, fps):
hud = pygame.Surface((800, 100))
font = pygame.font.Font(None, 30)
class Hud:
# Adding items to the hud
hud.blit(draw_score(font, score), (0, 0))
hud.blit(draw_timeleft(font, timeleft), (100, 0))
hud.blit(draw_fps(font, fps), (150, 0))
return hud
def __init__(self, screensize):
self.screen_width, self.screen_height = screensize
self.font = pygame.font.Font(None, 30)
self.screen = pygame.Surface((self.screen_width, self.screen_height / 6))
def draw_score(font, score):
score = font.render('Score: ' + str(score), 0, WHITE)
return score
def draw_hud(self, score, timeleft, fps):
self.screen.fill(BLACK)
self.screen.blit(self.draw_score(score), (0, 0))
self.screen.blit(self.draw_timeleft(timeleft), (self.screen_width / 2, 0))
thefps = self.draw_fps(fps)
fps_rect = thefps.get_rect()
self.screen.blit(thefps, (self.screen_width - fps_rect.w, 0))
return self.screen
def draw_timeleft(font, time):
# Add a clock icon here (maybe egg-clock)
time = font.render(str(time), 1, WHITE)
return time
def draw_score(self, score):
score = self.font.render('Score: ' + str(score), 0, WHITE)
return score
def draw_fps(font, fps):
return font.render('fps: ' + str(fps), 10, RED)
def draw_timeleft(self, time):
# Add a clock icon here (maybe egg-clock)
time = self.font.render(str(time), 1, WHITE)
return time
def draw_fps(self, fps):
return self.font.render('fps: ' + str(fps), 10, RED)