The code demonstrates running program limitted time by following going over endtime expressed in seconds since midnight, so might fail for playing just midnight (add check that endtime is less than 24*60*60 if you want to be sure).

Exciting action :) of the program is that it shows remaining time in position moving left and right as you push left and right arrow keys.

Edited 5 Years Ago by pyTony: n/a

program MoveSec;

Uses Dos, Crt;


const
    Width = 80;
    waittime = 30;
    gametime = 10.0; { time to play in seconds }
var
   c: char;
   i, x: byte;
   start, endtime: real;

function timer:real;
var hour,minute,second,sec100:word;
begin
     gettime(hour,minute,second,sec100);
     timer:=(hour*3600.0+minute*60.0+second+sec100/100.0)
end;

begin
    start := timer;
    endtime := start + gametime;
    x :=  trunc(Width / 2);

    repeat
        delay(waittime);
        while keypressed do
        begin
          c := readkey;
          if c = #0 then
          begin
            c := readkey;
            write(#13);
            case c of
              #75:  if x > 2  then  Dec(x);
              #77: if (x <= Width - 10) then Inc(x);
            end;
          end;
          for i := 1 to x do write(' ');
          write(trunc(endtime-timer),'  ');
        end;
    until (c = 'q') or (timer >= endtime);
    if c <> 'q' then writeln(#10,'Game time finished');
end.

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.