【问题标题】:How to set input time limit for user in game?如何在游戏中为用户设置输入时间限制?
【发布时间】:2022-12-03 12:49:06
【问题描述】:

我想知道如何在 python 中制作一个输入最多 5 秒的程序(例如,他可以在 2 秒后发送输入)我决定做一个简单的游戏,你基本上必须在 5 秒以下重写一个单词。我知道如何创建输入并让它等待 EXACTLY 5 秒,但我想要实现的是将最大输入时间设置为 5 秒,这样如果用户在让我们说 2 秒后键入答案,他将进入下一个单词。你能告诉我实现目标的方法吗?提前致谢!


for word in ["banana","earth","turtle","manchester","coctail","chicken"]:

    # User gets maximum of 5 seconds to write the word,
    # if he does it before 5 seconds pass ,he goes to next word (does not have to wait exactly 5 seconds, he   
    # can send input in e.g 2 seconds)      
    # if he does not do it in 5 seconds he loses game and it is finished


    user_input = input(f"Type word '{word}': ")

    #IF the word is correct go to next iteration
    if(user_input==word):
        continue

    #If the word is incorrect finish the game
    else:
        print("You lost")
        break

我试着用 threading.Timer() 来做,但它不起作用

import threading

class NoTime(Exception):
    pass

def count_time():
    raise NoTime

for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
    try:


        #Create timer which raises exception after 5 seconds
        timer = threading.Timer(5,count_time)
        timer.start()

        user_input = input(f"Type word '{word}': ")
        #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
        timer.cancel()

        if user_input==word:
            print("Correct")
        else:
            print("Incorrect, you LOSE!")
            break

    except NoTime:
        print("You run out of time, you lose")
        break

我得到的错误

Traceback (most recent call last):
  File "C:\Users\papit\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\Users\papit\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1394, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Users\papit\OneDrive\Pulpit\Programming\Python Bro Course\Math\second\threading_training.py", line 7, in count_time
    raise NoTime
NoTime

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    =======

    在Python中创建一个最大输入时间为5秒的程序,可以使用time和select模块实现输入操作的超时。 time 模块提供处理时间的函数,例如测量经过的时间,select 模块提供等待来自多个源的输入的函数,包括超时。

    要创建最长输入时间为 5 秒的程序,您可以使用以下步骤:

    在程序开头导入时间并选择模块: 导入时间 导入选择 使用 time.time() 函数获取输入操作开始时的当前时间: 开始时间 = 时间.时间() 使用 select.select() 函数等待用户输入,超时时间为 5 秒: timeout = 5 # 设置超时为5秒 input_ready, _, _ = select.select([sys.stdin], [], [], 超时) 如果 input_ready 变量不为空,表示在超时时间内收到了用户的输入,则使用 input() 函数读取用户的输入: 如果输入就绪: user_input = 输入() 如果 input_ready 变量为空,这表明超时已过而未收到用户输入,则通过显示错误消息或采取适合您的程序的其他操作来处理超时: 别的: # 处理超时,例如通过显示错误消息或采取其他操作 使用time.time()函数获取输入操作结束时的当前时间,用结束时间减去开始时间计算经过的时间:

    结束时间 = 时间.时间() 经过时间 = 结束时间 - 开始时间 使用经过的时间来确定用户的输入是否在最大时间限制内,并采取适当的操作,例如显示输入或继续游戏中的下一个单词:

    如果 elapsed_time <= 超时: # 用户的输入在时间限制内,所以显示它或采取其他行动 别的: # 用户的输入不在限定时间内,所以处理超时 总的来说,在Python中创建一个最大输入时间为5秒的程序,可以使用time和select模块实现输入操作超时,如果用户没有在最长时间内提供输入,则处理超时限制。这使您可以确保在指定的时间限制内接收到用户的输入,并根据经过的时间采取适当的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多