Merge branch 'master' of github.com:lvl2/avocados
commit
4fddd144d4
48
game.py
48
game.py
|
@ -7,37 +7,57 @@ import os, random
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
|
|
||||||
from support import colors
|
from support.colors import *
|
||||||
from interface import hud
|
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():
|
def main():
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption('Avocados')
|
pygame.display.set_caption('Pin the Avocados!')
|
||||||
clock = pygame.time.Clock()
|
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
|
score = 0
|
||||||
time = 33
|
time = 15
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
|
timeleft = time
|
||||||
while running:
|
while running:
|
||||||
# Limit to 50 fps
|
time_passed = clock.tick(desired_fps)
|
||||||
time_passed = clock.tick(30)
|
|
||||||
fps = clock.get_fps()
|
fps = clock.get_fps()
|
||||||
|
screen.fill(bg)
|
||||||
|
|
||||||
my_hud = hud.draw_hud(score, time, fps)
|
timeleft -= time_passed / 1000
|
||||||
screen.blit(my_hud, (10,10))
|
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():
|
for event in pygame.event.get():
|
||||||
|
if event.type == MOUSEBUTTONDOWN:
|
||||||
|
score += 100
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -4,24 +4,30 @@ import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
from support.colors import *
|
from support.colors import *
|
||||||
|
|
||||||
def draw_hud(score, timeleft, fps):
|
class Hud:
|
||||||
hud = pygame.Surface((800, 100))
|
|
||||||
font = pygame.font.Font(None, 30)
|
|
||||||
|
|
||||||
# Adding items to the hud
|
def __init__(self, screensize):
|
||||||
hud.blit(draw_score(font, score), (0, 0))
|
self.screen_width, self.screen_height = screensize
|
||||||
hud.blit(draw_timeleft(font, timeleft), (100, 0))
|
self.font = pygame.font.Font(None, 30)
|
||||||
hud.blit(draw_fps(font, fps), (150, 0))
|
self.screen = pygame.Surface((self.screen_width, self.screen_height / 6))
|
||||||
return hud
|
|
||||||
|
|
||||||
def draw_score(font, score):
|
def draw_hud(self, score, timeleft, fps):
|
||||||
score = font.render('Score: ' + str(score), 0, WHITE)
|
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_score(self, score):
|
||||||
|
score = self.font.render('Score: ' + str(score), 0, WHITE)
|
||||||
return score
|
return score
|
||||||
|
|
||||||
def draw_timeleft(font, time):
|
def draw_timeleft(self, time):
|
||||||
# Add a clock icon here (maybe egg-clock)
|
# Add a clock icon here (maybe egg-clock)
|
||||||
time = font.render(str(time), 1, WHITE)
|
time = self.font.render(str(time), 1, WHITE)
|
||||||
return time
|
return time
|
||||||
|
|
||||||
def draw_fps(font, fps):
|
def draw_fps(self, fps):
|
||||||
return font.render('fps: ' + str(fps), 10, RED)
|
return self.font.render('fps: ' + str(fps), 10, RED)
|
||||||
|
|
Loading…
Reference in New Issue