如何检测Pascal中的已用时间? [英] How can I detect elapsed time in Pascal?

查看:203
本文介绍了如何检测Pascal中的已用时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Pascal中创建一个简单的游戏。它使用控制台。游戏的目标是在60秒内收集尽可能多的苹果。游戏结构是一个简单的无限循环。每次迭代,你可以做一个移动。这里的问题 - 在你做出移动( readKey )之前,时间可以通过它想要的多少。例如,用户可以在10秒后按下一个键!有什么办法计数时间吗?我需要该程序才能知道用户播放时(按键之前和之后),所以我不知道如何防止用户欺骗。

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.

完整代码: http://non.dagrevis.lv/junk/pascal/Parad0x/Parad0x.pas

我知道有两种可能的解决方案:

As far I know that there are two possible solutions:


  1. getTime(来自DOS),

  2. 延迟(来自CRT)。

...但我不知道如何使用它们与我的循环。 p>

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

这篇关于如何检测Pascal中的已用时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆