如何检测 Pascal 中经过的时间?

发布于 2024-10-31 18:46:16 字数 686 浏览 6 评论 0原文

我正在尝试用 Pascal 创建一个简单的游戏。它使用控制台。游戏的目标是在 60 秒内收集尽可能多的“苹果”。游戏结构是一个简单的无限循环。每次迭代,您都可以迈出一步。问题是,在您采取行动 (readKey) 之前,时间可以随心所欲地流逝。例如,用户可以在10秒后按下一个键!有什么办法可以计算时间吗?我需要程序知道用户何时玩游戏(按下按键之前和之后),所以我不知道如何防止用户“作弊”。

这是我的游戏的简单结构:

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

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

据我所知,有两种可能的解决方案:

  1. getTime(来自DOS)、
  2. delay(来自CRT)。

...但我不知道如何在我的循环中使用它们。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2024-11-07 18:46:16

检查此链接。可能有一些对您有用的信息。 这里与您所要求的相同。 这就是您要查找的内容(与下面的代码相同)。

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文