use a class for the hud
parent
2ab3d85c71
commit
a8addce0f4
15
game.py
15
game.py
|
@ -19,31 +19,32 @@ def main():
|
||||||
screen_width = 800
|
screen_width = 800
|
||||||
screen_height = 600
|
screen_height = 600
|
||||||
screen = pygame.display.set_mode((screen_width,screen_height))
|
screen = pygame.display.set_mode((screen_width,screen_height))
|
||||||
bg = (0,0,0)
|
bg = BLACK
|
||||||
screen.fill(bg)
|
|
||||||
desired_fps = 60
|
desired_fps = 60
|
||||||
|
|
||||||
font = pygame.font.Font(None, 40)
|
font = pygame.font.Font(None, 40)
|
||||||
game_over = font.render('GAME OVER', 0, RED)
|
game_over = font.render('GAME OVER', 0, RED)
|
||||||
|
my_hud = hud.Hud((screen_width, screen_height))
|
||||||
|
|
||||||
score = 0
|
score = 0
|
||||||
time = 10
|
time = 15
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
timeleft = time
|
timeleft = time
|
||||||
while running:
|
while running:
|
||||||
# Limit to XY fps
|
|
||||||
time_passed = clock.tick(desired_fps)
|
time_passed = clock.tick(desired_fps)
|
||||||
#time_since = clock.get_time() #Same as above
|
|
||||||
fps = clock.get_fps()
|
fps = clock.get_fps()
|
||||||
|
|
||||||
|
screen.fill(bg)
|
||||||
|
|
||||||
timeleft -= time_passed / 1000
|
timeleft -= time_passed / 1000
|
||||||
timeleft = round(timeleft,2)
|
timeleft = round(timeleft,2)
|
||||||
|
|
||||||
if timeleft <= 0:
|
if timeleft <= 0:
|
||||||
screen.blit(game_over, (screen_width/3, screen_height/2))
|
screen.blit(game_over, (screen_width/3, screen_height/2))
|
||||||
|
|
||||||
my_hud = hud.draw_hud(score, timeleft, fps)
|
chud = my_hud.draw_hud(score, timeleft, round(fps,2))
|
||||||
screen.blit(my_hud, (10,10))
|
screen.blit(chud, (10,10))
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
|
|
@ -4,24 +4,28 @@ 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), (200, 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)
|
||||||
return score
|
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):
|
def draw_score(self, score):
|
||||||
# Add a clock icon here (maybe egg-clock)
|
score = self.font.render('Score: ' + str(score), 0, WHITE)
|
||||||
time = font.render(str(time), 1, WHITE)
|
return score
|
||||||
return time
|
|
||||||
|
|
||||||
def draw_fps(font, fps):
|
def draw_timeleft(self, time):
|
||||||
return font.render('fps: ' + str(fps), 10, RED)
|
# 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)
|
||||||
|
|
Loading…
Reference in New Issue