如何在 Delphi 7 中测量经过的时间?

发布于 2024-09-27 10:51:13 字数 222 浏览 6 评论 0原文

我想知道是否有人可以帮助我。我对 Delphi 编程非常陌生,我有一个 10 天内到期的学校项目,说实话我不知道我在做什么。

对我的期望是,我编写了一个记忆游戏,我目前陷入困境,我必须计算该人玩游戏需要多长时间,然后在最后显示他们花了多长时间作为“分数”。

我如何计时?我应该使用什么组件以及如何对该组件进行编程以计时?它应该在单击按钮时开始,然后在游戏结束时结束。

任何帮助将不胜感激!

I was wondering if anyone could by any chance help me. I am very new to Delphi Programming and I have a school project due in 10 days and to be honest I have no idea what I'm doing.

What is expected of me is that I program a memory game where I am currently stuck in that I have to time how long it takes the person to play the game and then display how long it took them as a 'score' at the end.

How do I time? What component should I use and how do I program this component to time? It should start when a button is clicked and then end when the game finishes.

Any help will be highly appreciated!

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

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

发布评论

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

评论(4

心是晴朗的。 2024-10-04 10:51:13

为什么不在他开始游戏时将当前时间保存在变量中,并在他结束时再次保存时间?

您可以通过立即指令获取。

var time: TDateTime;
begin
 time := now;
 ShowMessage(DateTimeToStr(time));
end;

您将在系统中看到当前时间。

Why don't you save the current time in a variable when he starts the game, and again save the time when he ends?

You can take it by the Now instruction.

var time: TDateTime;
begin
 time := now;
 ShowMessage(DateTimeToStr(time));
end;

You'll see the current time in the system.

时光瘦了 2024-10-04 10:51:13

您将需要

1.- 在表单中

  • 添加一个计时器,并将其 Enabled 属性设置为 False
  • 添加一个标签来显示时间
  • 添加一个私有属性startTime来记录用户开始游戏的时间。

应该会产生类似这样的结果...

type
 TForm1 = class(TForm)
  ...
  Label1: TLabel;
  Timer1: TTimer;
  ...
 private
  startTime:TDateTime;
  ....

2.- 在开始按钮的单击事件中,初始化 startTime 属性并启动计时器的代码。

 procedure TForm1.Button1Click(Sender: TObject);
  begin
   startTime:=Now;
   Timer1.Enabled:=True;
   ....
  end;

3.- 在计时器的计时器事件中,一些代码用于显示时间计数

 procedure TForm1.Timer1Timer(Sender: TObject);
  begin
   Label1.Caption:=TimeToStr(Now-startTime);
   ....
  end;

4.- 在完成按钮的单击事件中,或者当程序认为游戏结束时,一些代码用于停止计时器。

 procedure TForm1.Button2Click(Sender: TObject);
  begin
   Timer1.Enabled:=False;
   Label1.Caption:=TimeToStr(now-startTime);
   ....
  end;  

You will need

1.- In your form,

  • add a timer, and set its Enabled property to False.
  • add a label to display the time
  • add a private attribute startTime to record the time when the user starts the game.

should result something like this...

type
 TForm1 = class(TForm)
  ...
  Label1: TLabel;
  Timer1: TTimer;
  ...
 private
  startTime:TDateTime;
  ....

2.- At the click event of the start button, the code to initialize the startTime attribute and kick-off the Timer.

 procedure TForm1.Button1Click(Sender: TObject);
  begin
   startTime:=Now;
   Timer1.Enabled:=True;
   ....
  end;

3.- At the Timer event of the Timer, some code to display the time counting

 procedure TForm1.Timer1Timer(Sender: TObject);
  begin
   Label1.Caption:=TimeToStr(Now-startTime);
   ....
  end;

4.- At the click event of the finish button, or when the program considers the end of the game, some code to stop the timer.

 procedure TForm1.Button2Click(Sender: TObject);
  begin
   Timer1.Enabled:=False;
   Label1.Caption:=TimeToStr(now-startTime);
   ....
  end;  
看海 2024-10-04 10:51:13
  1. 在表单中创建 TDateTime 类型的变量(例如 StartTime)。
  2. 当用户开始玩时,将变量设置为等于 Now()。
  3. 当用户完成后,计算 Now()-StartTime 的值。结果是一个十进制数,表示开始时间和结束时间之间经过的一天的分数。
  4. 要将其转换为秒数,请将其乘以 (60 * 60 * 24)(即一天的秒数)。从那里您可以根据需要显示号码。
  1. Create a variable (for example StartTime) of TDateTime type in your form.
  2. When the user starts playing, set the variable to equal Now().
  3. When the user finishes, calculate the value of Now()-StartTime. The result is a decimal number that represents the fraction of a day that elapsed between the starting time and ending time.
  4. To convert that to the number of seconds, multiply it by (60 * 60 * 24) (which is the number of seconds in a day). From there you can display the number however you want.
哽咽笑 2024-10-04 10:51:13

PA 的答案似乎正是您所需要的。因为如果我理解得很好并且这是您第一次使用delphi,我只需补充一点:

  • Now是SysUtils中定义的一个函数,它返回当前日期和时间

  • 您将在系统组件面板上找到 TTimer(请参见下面链接中的图片)

  • 通过选择对象检查器中的“事件”选项卡,然后双击输入框(参见下面链接中的图片)

< a href="https://i.sstatic.net/0iNsL.png" rel="nofollow">https://i.sstatic.net/0iNsL.png (抱歉,无法内联图像,因为我还没有必要的声誉)

从这里开始你的申请将会很容易完成,

祝你好运,
G

PA's answer seems to be exactly what you need. because if i understood well and this is your first time working with delphi, i'd only add that:

  • Now is a function defined in SysUtils that returns the current date&time

  • you'll find the TTimer on the System component pallette (see image in link below)

  • all the procedures where you need to write the code will be automatically generated by selecting the Events tab in the Object inspector, and then double clicking in the input box (see image in link below)

https://i.sstatic.net/0iNsL.png (sorry, can't inline images because i don't have the necessary reputation yet)

from here on it hould be very easy to finish your application

good luck,
G

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