其他代码运行时的Python倒计时

2024-04-24 11:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让播放器只有500-600毫秒的帧来输入1或2。我不知道该怎么做,而且我找到的唯一计时器

time.sleep(1)

这是我的密码:

        game = 1
    while game == 1:
        play = input(print("'display picture in other window' [1] for right [2] for wrong "))
        if '1' in play:
            print("You shot the Criminal! + 1 point")
            point = point + 1
            row = row + 1
            if row == 3:
                unlock2 = 1
                print ("You gained 2 bonus points!")
                point = point + 2
                row = 0
            if unlock2 == 1:
                point = point + (point/2)
                print ("You gained bonus points!")

        elif '2' in play:
            print("You shot an innocent! Game Over!")
            break

            game = 0

编辑:编辑


Tags: inyougame编辑forplayifpoints
1条回答
网友
1楼 · 发布于 2024-04-24 11:45:03

最简单的方法是测量用户的响应时间,如下所示:

import time

def respond_in_time(msg, s):
    start = time.time()
    response = input(msg)
    end = time.time()
    print(end-start)
    if end - start > s:
        return False
    else:
        return response

用法:

>>> respond_in_time("What is 2+2? ", 0.5) # User responds in time
'4'
>>> respond_in_time("What is 2+2? ", 0.5) # User is too slow
False

相关问题 更多 >