0

I want to have a time limit in order to enter the input in the following code. In other words, there should be a timer tracking the time and if it exceeds the limit, the code should print out a message like "Game over" automatically without hitting any key. it is a sort of pop-up.

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

the other question is almost the same but the answer is not what I am looking for. I want the code to return a message when the time is over without hitting "ENTER".

Community
  • 1
  • 1
msc87
  • 823
  • 3
  • 16
  • 32
  • I checked this question and its answers. I used the suggested answer, but it still needs me to press enter in order to check the time and if it is out of time, then returns "Game over". this is already applied to the code. I want the code to keep track of time and before hitting Enter, the message pops up. – msc87 Feb 22 '14 at 19:55
  • I think I need to set an event to provoke the timeout and then a handler print the proper message. the signal class is only working in Unix though. Any suggestion!!!! – msc87 Feb 22 '14 at 21:22

1 Answers1

1

The simplest way is to use the curses module. You'll want to set nodelay(1), and poll for input. http://docs.python.org/2/howto/curses.html#user-input

user590028
  • 10,124
  • 3
  • 30
  • 51
  • As I read the doc, it says the getstr() function is limited and the getch() is returning an integer corresponding to the ASCII code of keys pressed. My input is a number like 11, 24 or so, which denoting a square on the game panel. – msc87 Feb 22 '14 at 20:00
  • You'll need to loop over getch(), converting to ascii, eg: chr(getch()) – user590028 Feb 22 '14 at 20:14
  • Isn't it for Linux?!!! – msc87 Feb 24 '14 at 14:57