How can I detect elapsed time in Pascal?

拥有回忆 提交于 2019-12-06 06:09:47

问题


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 know 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.


回答1:


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.


来源:https://stackoverflow.com/questions/5625798/how-can-i-detect-elapsed-time-in-pascal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!