开发者

How can I detect elapsed time in Pascal?

I'm trying to create a simple game in Pascal. It uses the console. The goal in the game is to collect as many 'apples' as you can in 60 seconds. The game structure is a simple infinite loop. Each iteration, you can make one move. And here's the problem — before you make the move (readKey), time can pass as much as it wants. For example, the user can press a key after 10 seconds! Is there any way to count time? I need the program to know when user plays (before and after a key is pressed), so I don't know how to prevent user from "cheating".

Here's simple structure of my game:

begin
    repeat
        {* ... *}
        case ReadKey of
            {* ... *}
        end;
        {* ... *}
    until false;
end.

Full code: http://non.dagrevis.lv/junk/pascal/Parad0x/Parad0x.pas.

As far I kno开发者_如何学编程w that there are two possible solutions:

  1. getTime (from DOS),
  2. delay (from CRT).

...but I don't know how to use them with my loop.


Check this link. There might be some useful information for you. And here is the same you're asking for. And here's what you're looking for (same as the code below).

var hours: word;
    minutes: word;
    seconds: word;
    milliseconds: word;

procedure StartClock;
begin
  GetTime(hours, minutes, seconds, milliseconds);
end;

procedure StopClock;
var seconds_count : longint;
    c_hours: word;
    c_minutes: word;
    c_seconds: word;
    c_milliseconds: word;

begin
  GetTime(c_hours, c_minutes, c_seconds, c_milliseconds);
  seconds_count := c_seconds - seconds + (c_minutes - minutes) * 60 + (c_hours - hours) * 3600;
  writeln(inttostr(seconds_count) + ' seconds');
end;

begin
  StartClock;

  // code you want to measure

  StopClock;
end.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜