Reputation: 1
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!
Upvotes: 0
Views: 597
Reputation: 50998
Upvotes: 1
Reputation: 15528
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
Upvotes: 0
Reputation: 29369
You will need
1.- In your form,
Enabled
property to False
.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;
Upvotes: 2
Reputation: 101
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.
Upvotes: 3