Jump to content
Iña

Timer game delphi 7

Recommended Posts

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

 

my problem is when the minutes>0 the seconds are still running(the seconds doesn´t start at 0 again), so  the only thing i need is when t is 60000(60 seconds) it should start at 0 again(the seconds)


 

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

 

Quote

(i don' t know if this the proper way to use timer, i don't know much about timers)

 

Share this post


Link to post

Use TStopWatch from System.Diagnostics / System.TimeSpan.

Share this post


Link to post

How i do that? I'm a newbie with timers

Share this post


Link to post
28 minutes ago, Lars Fosdal said:

Use TStopWatch from System.Diagnostics / System.TimeSpan.

I'm afraid System.Diagnostics was not part of Delphi 7.

Share this post


Link to post

A VCL:TTimer / FMX:TTimer is something that produces an event after a given interval, so not what you are looking for when you want to measure an interval.

A TStopWatch is handy for measuring an interval.

A TTimeSpan is an interval and has numerous methods for converting to various time units

var
  t: TStopWatch;
  time: TTimeSpan;

whenever you want to start measuring

  t := TStopWatch.StartNew;

When you want to measure 

time := t.Elapsed;

Alternatively

  t.Stop;
  time := t.Elapsed;

Have a look at the TTimeSpan doc for how to get the various time units.

 

Your Button1Click does not solve your problem since nothing else can happen while you are inside that code.

 

Sometimes it helps to clarify how to solve a problem by writing the solution in regular text.

 

That is as far as I will go in solving homework assignments.

 

 

Share this post


Link to post
1 minute ago, Lajos Juhász said:

I'm afraid System.Diagnostics was not part of Delphi 7.

Ah, yeah... that is true.  My explanation of TTimer events vs measuring an interval still stands.

Share this post


Link to post

I forgot to paste this

Text:=inputbox('enter the word','',''); it goes above the if

 

Share this post


Link to post
4 hours ago, Iña said:

seconds:=t div 1000;

minutes:=t div 60000;

You're here calculating the total seconds and minutes. Try:

seconds:=t div 1000;

minutes:=seconds div 60;

seconds:=seconds-(minutes*60);

  • Thanks 1

Share this post


Link to post
10 hours ago, Lajos Juhász said:

seconds:=seconds-(minutes*60);

This would be easier:

seconds:=seconds mod 60;

 

Share this post


Link to post
Posted (edited)

Please find attached unit StopWatch for Delphi 7 (StopWatch.pas)

This is from Zarko Gajic, possibly not exactly identical to that  from System.Diagnostics.

Edited by Rolf Fankhauser

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×