Add fps counter, fix #10

This commit is contained in:
grisel-davy 2020-05-10 23:41:07 -04:00
parent ab6fb53b6a
commit 60cc17e5d4
2 changed files with 16 additions and 2 deletions

View file

@ -50,6 +50,8 @@ game = Game(carte,player,camera,enemies,hud)
clock = pygame.time.Clock()
done = False
fps_counter_time = pygame.time.get_ticks()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
@ -71,6 +73,12 @@ while not done:
if game.pause:
game.draw_pause(screen)
span = pygame.time.get_ticks()-fps_counter_time
hud.write_fps(screen,span/1000)
fps_counter_time = pygame.time.get_ticks()
pygame.display.flip()

View file

@ -71,16 +71,22 @@ class Hud():
def __init__(self,player):
self.player = player
self.ratio = 1
self.font = pygame.font.SysFont('GlacialIndifference-Regular', 72)
self.font_large = pygame.font.SysFont('GlacialIndifference-Regular', 72)
self.font_small = pygame.font.SysFont('GlacialIndifference-Regular', 40)
def draw(self,surface):
# draw the life bar
pygame.draw.rect(surface,(255,0,0),(10,10,round(self.player.life/100*surface.get_width()/3),30),0)
pygame.draw.rect(surface,(255,255,255),(10,10,int(surface.get_width()/3),30),3)
text = self.font.render(str(self.player.ammo), True, (255, 255, 255))
text = self.font_large.render(str(self.player.ammo), True, (255, 255, 255))
surface.blit(text, (int(surface.get_width()-1.5*text.get_width()),int(surface.get_height()-1.5*text.get_height())))
def write_fps(self,surface,time_mili):
text = self.font_small.render(str(int(1/time_mili)), True, (255, 255, 255))
surface.blit(text, (int(surface.get_width()-1.5*text.get_width()),int(0.5*text.get_height())))
class Carte():
def __init__(self,n):