[Tutor] Primitive Chess Clock Program Question

Alan Gauld alan.gauld at btinternet.com
Wed Jan 4 02:22:42 CET 2012

On 03/01/12 21:28, Steven D'Aprano wrote:

> I assume you want to display something like this:
> Enter your next move: 0:30
> where the "0:30" is the time remaining, and is constantly updating. When
> it hits zero, the function returns whether the user has typed anything
> or not.

Assuming Steven has guessed right then I think you need to use one of 
the non blocking input mechanisms like kbhit() or getch() or somesuch.

Those methods are notioriously unreliable and OS specific. For example 
you may need to use curses or the Microsoft runtime module msvcrt.

The general code will look like

Display prompt
while no key hit
     sleep briefly
     update time in prompt
     (using ctrl characters to delete/overwrire previouis entry)
#when key hit
process input.

It is a non trivial problem and the details will depend on whether you 
use curses or the microsoft route.

It is one of those few cases that is actually much easier to do
in a GUI. GUIs generally make life more complex but in this case 
updating a label while awaiting user input is almost trivial
(for a GUI).

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list