0

I just want to do a game of who writes the word faster, store it in an array, and then show in a memo.

The timer should start in 0, then start the timer and when the person writes the word it will be stored in an array, and then show it in a memo, finally the timer stops (all of this with repeat).

I want to save the timer expressed in minutes, and seconds. i also can´t find the component tstopwatch

    procedure TForm1.Button1Click(Sender: TObject);
    type
    people=array[1..2] of string;
    peopletime=array[1..2] of integer;
    var
    string1,text:string;
    time:integer;
    k,seconds:integer;
    p:people;
    p1:peopletime;
    t,minutes:cardinal;
    begin
    memo1.Lines.Clear;
    k:=1;
    p[1]:='James';
    p[2]:='Dan';
    repeat
     t:=gettickcount;
     timer1.Enabled:=true;
     text:=inputbox('enter the word','',''); 
     if text='day' then
       begin
       t:=gettickcount-t;
       seconds:=t div 1000;
       minutes:=t div 60000;
       form1.Memo1.Lines.add(p[k]+' '+inttostr(minutes)+' '+'minutes'+' 
       '+inttostr(seconds));
       k:=k+1;
       timer1.Enabled:=false;
       end;

    until k=3;
    end;
Mark W.
  • 11
  • 5
  • I think you're trying to calculated elapsed time. TTimer won't help you here.. check this question https://stackoverflow.com/questions/16984360/how-to-calculate-elapsed-time-of-a-function – John Easley Apr 11 '21 at 23:38
  • 2
    How is this any different from your [previous question](https://stackoverflow.com/q/67041637/65863)? – Remy Lebeau Apr 12 '21 at 00:49
  • just edited(my mistake i copied the same code at first),i fixed most of the code but now it has only one error , when the minutes>0 the seconds are still running(the seconds doesn´t start at 0 again), so now the only thing i need is when t is 60000(60 seconds) it should start at 0 again(the seconds) – Mark W. Apr 12 '21 at 00:56
  • @MarkW. First off, your use of `TTimer` is useless since the UI message loop is being blocked from handling timer events. And using `GetTickCount()` the way you are, I would suggest calculating the `minutes` and `seconds` like this instead: `seconds := t div 1000; minutes := seconds div 60; seconds := seconds mod 60;` You should probably also have a `Sleep(1000)` in your loop, otherwise your code will reach `k=3` without the `seconds` actually changing value. – Remy Lebeau Apr 12 '21 at 02:04
  • @MarkW. However, this kind of loop will not work well in a UI app, so you really should rewrite this code to get rid of the loop completely and actually use `TTimer` and other UI actions in an event-driven manner. – Remy Lebeau Apr 12 '21 at 02:07
  • That's the thing i'm new with timers, and i don't find any info like this. – Mark W. Apr 12 '21 at 02:22
  • What's your Delphi version? Seems very outdated, if you can't find ``TStopwatch`` and it's unit ``System.Diagnostic``! – Delphi Coder Apr 12 '21 at 04:51
  • Delphi 7 is the version – Mark W. Apr 12 '21 at 04:58
  • Is there another way to do it? – Mark W. Apr 12 '21 at 06:03
  • Remy said you have to write the code using events. You already use one event: Button1Click is the even handler for OnClick event triggered by Button1. Similarly, use the OnTimer event triggered by the Timer1. For your user to enter text, you should use a TEdit. It has a lot of events but probably none are needed. You have to know when the user finished entering the word. Is it when the word, known in advance by the code is written (Use Edit OnChange event to check that)? Or is it when the user clicks on a "finish" button (Use that button OnClick event)? – fpiette Apr 12 '21 at 06:41
  • Your description of the game is not complete. Please add a full description. A "user guide" for your game would be a nice description". Describe what is on screen, what button the user has to click and when, where does the user enter his word, how a user now it is his turn to play, where is the result shown, and more... – fpiette Apr 12 '21 at 06:44
  • it´s when the word, known in advance by the code is written – Mark W. Apr 12 '21 at 06:49
  • my god when i pasted the code it didn´t pasted this text:=inputbox('enter the word','',''); (it goes above if text='day' then) – Mark W. Apr 12 '21 at 07:10
  • @fpiette Mark wants to measure elapsed time. There is absolutely no need for a TTimer in this case! – Delphi Coder Apr 12 '21 at 07:14
  • Just Edited the code. – Mark W. Apr 12 '21 at 07:20
  • [Solved] i just change this seconds:=t div 1000; minutes:=seconds div 60; seconds:=seconds-(minutes*60); – Mark W. Apr 12 '21 at 07:44
  • how do i change the post to solved? or you guys have to change it? – Mark W. Apr 12 '21 at 07:53
  • 2
    @MarkW. Use the [help] to learn how this site works. More efficient that way. – David Heffernan Apr 12 '21 at 08:10

0 Answers0