views:

124

answers:

5

i was wondering if anyone could by any chance help me. i have a school project due in 10 days and to be honest i have no idea what im doing =/ what is expected of me is that i program a memory game. where i am currently stuck is 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 finnishes. any help will be highly appreciated!

+2  A: 

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.

brent
+2  A: 

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;  
PA
The timer is redundant in this example and adds an overhead. An event is raised every time the timer ticks, etc.
dwarFish
A: 

hi,

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)

http://i.imgur.com/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

Grove
A: 

PA thank you that seems to be working great :) just one question. when the timer starts it has 12 in the hour section so it looks like this 12:00:00 and then the seconds add on to that. how do i take that 12 away when i display the time at the end of the game for the players 'score' ?

celest
Do not reply to an answer as a new answer, but rather as a follow-up comment to the answer you're replying to. Otherwise the answering user won't see your response.
Larry Lustig
ok but how do i do that? sorry im new to this =/
celest
just click on "add comment" label that you may find below last commento to the answer
PA
Anyway to quick answer your question, you may change the `TimeToStr(now-startTime)` to `FormatDateTime('nn:ss.zzz', now-startTime)`
PA
A: 
  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.
Larry Lustig