【问题标题】:How do I add a 1 second delay?如何添加 1 秒延迟?
【发布时间】:2019-07-29 09:39:31
【问题描述】:

嘿,我正在制作这款游戏​​,我需要延迟 1 秒或更长时间? 有什么想法吗?

这里我需要在 tx3 = 1000 和 cheesyx = 1000 之间延迟。

            if x < 300 and y < 300 and not duringfight:
                win.blit(cheesyt3, (tx3, ty3))
            if x < 250 and y < 250 and not duringfight:
                tx3 = 1000
                cheesyx = 1000
            if cheesyx == 1000:
                deathx -= 5
                if deathx == 600:
                    deathx += 5
                    deathmove = False
                    wmx = 1000
                    win.blit(deathtext, (dtext, 400))
                    if x > 400:
                        dtext = 1000
                        win.blit(deathhanpup, (deathx, deathy))
                        deathy = 1000

【问题讨论】:

    标签: time pygame delay wait


    【解决方案1】:

    由于 time.sleep 不能很好地与 pygame 配合使用,您可以使用 time 模块将当前时间与上次执行时间进行比较,并在经过一秒多之后才在(事件)循环中执行您的代码。

    import time
    
    last_execution = time.time()
    if time.time() - last_execution > 1:
        last_execution = time.time
        execute_code_you_want()
    

    【讨论】:

      【解决方案2】:

      Pygame 时间

      在 pygame 中,time.sleep(1) 不能正常工作,所以你可以使用 pygame.time.delay(1),它工作正常。

      链接:https://www.pygame.org/docs/ref/time.html#pygame.time.delay

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-22
        • 2017-03-20
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多