繁体   English   中英

暂停时间流

[英]Pausing time.time flow

我使用Python和Pygame制作游戏,并且使用time.time来计时用户进行关卡的时间。 但是,我也有一个暂停菜单。 当暂停菜单打开时,如何使time.time无法继续?

我想我会做这样的事情:如果游戏没有暂停,请使用clock.tick()返回的时间增加每个帧的计时器,并且当用户暂停和取消暂停游戏时,调用它时不带参数就放弃了游戏暂停时过去的时间。

import sys
import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
timer = 0
dt = 0
paused = False
running = True

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        elif event.type == pg.KEYDOWN:
            paused = not paused
            # This is needed to discard the time that
            # passed while the game was paused.
            clock.tick()

    if not paused:
        timer += dt  # Add delta time to increase the timer.

        screen.fill((30, 30, 30))
        txt = font.render(str(round(timer, 2)), True, (90, 120, 40))
        screen.blit(txt, (20, 20))

        pg.display.flip()
        dt = clock.tick(30) / 1000  # dt = time in seconds since last tick.

pg.quit()
sys.exit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM