Proceses keys and keep track of time in Turbo Pascal

TrustyTony 0 Tallied Votes 444 Views Share

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.

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.