2014-02-22 31 views
0

我想有一个时间限制,以便在下面的代码中输入输入。换句话说,应该有一个定时器跟踪时间,如果超过了限制,代码应该自动打印出“游戏结束”这样的消息,而不必按任何键。这是一种弹出式窗口。如何在python中设置弹出时间限制?

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”的情况下返回一条消息。

+0

我检查了这个问题及其答案。我使用了建议的答案,但它仍然需要我按下输入以检查时间,如果时间不足,则返回“游戏结束”。这已经应用于代码。我希望代码能够跟踪时间,并在点击Enter之前弹出消息。 – msc87

+0

我想我需要设置一个事件来激发超时,然后处理程序打印正确的消息。信号类只能在Unix中工作。任何建议! – msc87

回答

1

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

+0

当我阅读文档时,它说getstr()函数是有限的,getch()返回一个与按下的ASCII码相对应的整数。我的输入是一个数字,如11,24左右,表示游戏面板上的方块。 – msc87

+0

您需要循环getch(),转换为ascii,例如:chr(getch()) – user590028

+0

是不是适用于Linux? – msc87

相关问题