【问题标题】:How to set a pop up time limit in python? [duplicate]如何在python中设置弹出时间限制? [复制]
【发布时间】:2014-03-24 10:40:33
【问题描述】:

我想有一个时间限制,以便在以下代码中输入输入。换句话说,应该有一个计时器来跟踪时间,如果超过限制,代码应该自动打印出一条消息,比如“游戏结束”,而不用按任何键。这是一种弹出窗口。

def human(player, panel):
    print print_panel(panel)
    print 'Your Turn! , Hint: "23" means go to row No.2 column No.3/nYou got 1 min to  move.'
    start_time = time.time()
    end_time = start_time + 60
    while True :
        move = raw_input('> ')
        if move and check(int(move), player, panel):
            return int(move)
        else:
            if (time.time() < end_time):
                print 'Wrong move >> please try again.'
            else:
                print "Game over"
                return panel, score(BLACK, panel)
                break

另一个question 几乎相同,但答案不是我想要的。我希望代码在时间结束时返回一条消息而不按“ENTER”。

【问题讨论】:

  • 我检查了这个问题及其答案。我使用了建议的答案,但仍然需要我按 Enter 键才能检查时间,如果时间已过,则返回“游戏结束”。这已经应用于代码。我希望代码跟踪时间,在按 Enter 之前,会弹出消息。
  • 我想我需要设置一个事件来引发超时,然后处理程序打印正确的消息。信号类虽然只在 Unix 中工作。任何建议!!!!

标签: python input time time-limiting


【解决方案1】:

最简单的方法是使用 curses 模块。您需要设置 nodelay(1),并轮询输入。 http://docs.python.org/2/howto/curses.html#user-input

【讨论】:

  • 当我阅读文档时,它说 getstr() 函数是有限的,并且 getch() 返回一个与按下的键的 ASCII 码相对应的整数。我输入的是一个11、24左右的数字,表示游戏面板上的一个正方形。
  • 你需要遍历getch(),转换成ascii,例如:chr(getch())
  • 不是Linux吗?!!!
猜你喜欢
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2020-10-07
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
相关资源
最近更新 更多