我如何实时更新pygame中的时间

时间:2019-12-21 23:32:00

标签: python pygame

我正在尝试实时显示游戏运行了多长时间。为此,我使用了self.time_passed = pygame.time.get_ticks()/1000并将其称为g.write("Time: %d" %(g.time_passed), g.white ,10, 10, 50)。我的写功能如下:

def write(self, text, color, x , y, size):
            font = pygame.font.SysFont("airel", size)
            text = font.render(text, True, color)
            textRect = text.get_rect()
            self.screen.blit(text,(x , y))

我的想法是,时间不是实时更新的。即使我将g.write("Time: %d" %(g.time_passed), g.white ,10, 10, 50)放在了主要的while True:游戏循环中,它也只会在玩家每次死亡时更新秒数。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我想是因为系统字体称为Arial而不是airel。还是您使用的字体是Aerial

这可能导致此功能失败,并且玩家死亡时调用的功能可以正常工作。

答案 1 :(得分:1)

我个人将使用time.time()

import time

start = time.time()

# your code

end = time.time()
duration = end - start # answer in seconds
print(duration)

此外,您可能只想这样声明一次字体:

import pygame

pygame.init()

font18 = pygame.font.SysFont("ariel", 18)

displaysurf = pygame.display.init((1280, 720))

def write(f, t, x, y, c):
    textsurf = f.render(t, True, c)
    displaysurf.blit(textsurf, (x, y)
相关问题