Jož*_*žer 4 python pygame pygame-clock
我想在 pygame 中计算事件发生时的时间。我读过文档中的一些内容,但我不太明白如何做到这一点。
在文档中,您可以获得以毫秒为单位的时间,但它在调用 pygame.init() 时开始计数。我想当布尔值为 true 时从 0 开始计数。
import pygame
pygame.init()
loop = True
boolean = False
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.RETURN:
boolean = True
screen.fill((255, 255, 255))
if boolean:
# start counting seconds
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间。
要确定自某个事件以来已经过去的时间,您只需测量该事件发生的时间并从当前时间中减去它即可。
这是一个工作示例:
import pygame
pygame.init()
FONT = pygame.font.SysFont("Sans", 20)
TEXT_COLOR = (0, 0, 0)
BG_COLOR = (255, 255, 255)
loop = True
start_time = None
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
start_time = pygame.time.get_ticks()
screen.fill(BG_COLOR)
if start_time:
time_since_enter = pygame.time.get_ticks() - start_time
message = 'Milliseconds since enter: ' + str(time_since_enter)
screen.blit(FONT.render(message, True, TEXT_COLOR), (20, 20))
pygame.display.flip()
clock.tick(60)
pygame.quit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12468 次 |
| 最近记录: |