How to determine the time elapsed in Pascal?

I am trying to create a simple game in Pascal. He uses the console. The goal of the game is to collect as many "apples" in 60 seconds. The structure of the game is a simple endless loop. At each iteration, you can make one move. And here's the problem - before you make a move ( readKey ), time can pass as much as you want. For example, a user can press a key in 10 seconds! Is there a way to count the time? I need a program to know when the user is playing (before and after pressing the key), so I donโ€™t know how to prevent the user from โ€œcheatingโ€.

Here is a 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 as I know, there are two possible solutions:

  • getTime (from DOS),
  • delay (from CRT).

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

+4
source share
1 answer

Check this link . There may be useful information for you. And here that you are asking. And here is what you are looking for (just like 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. 
+6
source

Source: https://habr.com/ru/post/1347597/


All Articles