47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os, pygame
|
|
from pygame.locals import *
|
|
from support.colors import *
|
|
|
|
class 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_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_score(self, score):
|
|
score = self.font.render('Score: ' + str(score), 0, WHITE)
|
|
return score
|
|
|
|
def draw_timeleft(self, time):
|
|
# Not sure this is clever, to recreate the surface each time ;)
|
|
timesurface = pygame.Surface((200, 20))
|
|
hourglass = pygame.image.load(os.path.join('img', 'hourglass.png')).convert_alpha()
|
|
hourglass = pygame.transform.scale(hourglass, (15, 18))
|
|
self.color_surface(hourglass, WHITE)
|
|
time = self.font.render(str(time), 1, WHITE)
|
|
timesurface.blit(hourglass, (0, 0))
|
|
timesurface.blit(time, (40, 0))
|
|
return timesurface
|
|
|
|
def draw_fps(self, fps):
|
|
return self.font.render('fps: ' + str(fps), 10, RED)
|
|
|
|
def color_surface(self,surface, color):
|
|
red, green, blue = color
|
|
arr = pygame.surfarray.pixels3d(surface)
|
|
arr[:,:,0] = red
|
|
arr[:,:,1] = green
|
|
arr[:,:,2] = blue
|