use a class for the hud

master
David Raison 2014-08-30 16:53:03 +02:00
parent 2ab3d85c71
commit a8addce0f4
2 changed files with 29 additions and 24 deletions

15
game.py
View File

@ -19,31 +19,32 @@ def main():
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
bg = (0,0,0)
screen.fill(bg)
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 = 10
time = 15
running = True
timeleft = time
while running:
# Limit to XY fps
time_passed = clock.tick(desired_fps)
#time_since = clock.get_time() #Same as above
fps = clock.get_fps()
screen.fill(bg)
timeleft -= time_passed / 1000
timeleft = round(timeleft,2)
if timeleft <= 0:
screen.blit(game_over, (screen_width/3, screen_height/2))
my_hud = hud.draw_hud(score, timeleft, fps)
screen.blit(my_hud, (10,10))
chud = my_hud.draw_hud(score, timeleft, round(fps,2))
screen.blit(chud, (10,10))
for event in pygame.event.get():
if event.type == pygame.QUIT:

View File

@ -4,24 +4,28 @@ 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), (200, 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), (100, 0))
self.screen.blit(self.draw_fps(fps), (650, 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)