Jump to content
Andry

ScoreBoard StopWatch

Recommended Posts

Hello all,
I'm trying to create a scoreboard, I need to use a stopwatch that can be paused and then resumed. I've already tried with a Timer, a TStopWatch but it's the Pause/Resume function that bothers me a bit.
Could you suggest me something for this feature.
Andry

MyScoreboard.png

Share this post


Link to post

@Andry

try some like this

implementation

{$R *.dfm}

uses
  System.DateUtils;

var
  LRigthEndTime  : TDateTime;
  LLeftEndTime   : TDateTime;
  LCurrentTime   : TDateTime;      // the start value...
  LDirectionTimer: boolean = true; // true = to right, false = to left
  LGameOver      : boolean = false;

procedure MyResetTimes;
begin
  //
  // define your initial values in some place...
  // just for my test in my OnCreate on form
  //
  Form1.Timer1.Enabled := false;
  //
  LCurrentTime  := now; // StrToDateTime('xx/xx/xxxx 00:00:00');
  LRigthEndTime := LCurrentTime;
  LLeftEndTime  := LCurrentTime;
  //
  LRigthEndTime.AddSecond(10); // StrToDateTime('xx/xx/xxxx 00:00:00');
  LLeftEndTime.AddSecond(-10); // StrToDateTime('xx/xx/xxxx 23:59:59');
  //
  // here, it's dependent of your direction usage:
  LGameOver := (LRigthEndTime <= LCurrentTime) { or (LLeftEndTime >= LCurrentTime) };
  //
  Form1.Memo1.Text := 'End Right: ' + TimeToStr(LRigthEndTime);
  Form1.Memo1.Lines.Add('End Left: ' + TimeToStr(LLeftEndTime));
  Form1.Label1.Caption := TimeToStr(LCurrentTime);
  Form1.Label2.Caption := DateTimeToStr(LCurrentTime);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyResetTimes;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
  procedure MyTimeIsOver(const AValue: smallint);
  begin
    LCurrentTime.AddSecond(AValue);
    if (AValue > 0) then
      Timer1.Enabled := not(LCurrentTime = LRigthEndTime)
    else
      Timer1.Enabled := not(LCurrentTime = LLeftEndTime);
    //
    LGameOver := Timer1.Enabled = false;
  end;

begin
  if LDirectionTimer then
    MyTimeIsOver(1)
  else
    MyTimeIsOver(-1);
  //
  Label1.Caption := TimeToStr(LCurrentTime);
  Label2.Caption := DateTimeToStr(LCurrentTime);
end;

procedure TForm1.BtnTimePLUSClick(Sender: TObject);
begin
  if LGameOver then
    exit;
  //
  LDirectionTimer := true;
  Timer1.Enabled  := not Timer1.Enabled;
end;

procedure TForm1.BtnTimeMINUSClick(Sender: TObject);
begin
  if LGameOver then
    exit;
  //
  LDirectionTimer := false;
  Timer1.Enabled  := not Timer1.Enabled;
end;

procedure TForm1.BtnResetTimesClick(Sender: TObject);
begin
  MyResetTimes;
end;

end.

 

Edited by programmerdelphi2k

Share this post


Link to post

To improve fluidity I would use a thread, so as to limit the pauses due to the execution of something else

Share this post


Link to post

I've added a very basic example project in hope that I've understand correct what you try to achieve.

Here are my basics shown as code:

procedure TForm1.btnStartClick(Sender: TObject);
begin
  FSec := SpinEdit1.Value;
  lblTime.Caption := IntToStr(FSec);
  Timer1.Enabled := True;
  btnPause.Enabled := True;
  btnStop.Enabled := True;
  btnStart.Enabled := False;
end;

procedure TForm1.btnPauseClick(Sender: TObject);
begin
  Timer1.Enabled := (not Timer1.Enabled);
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  btnPause.Enabled := False;
  btnStop.Enabled := False;
  btnStart.Enabled := True;
  Timer1.Enabled := False;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (FSec > 0) then
    begin
      Dec(FSec, 1);
      lblTime.Caption := IntToStr(FSec);
    end
    else
      begin
        lblTime.Caption := IntToStr(FSec);
        Timer1.Enabled := False;
        btnPause.Enabled := False;
        btnStop.Enabled := False;
        btnStart.Enabled := True;
      end;
end;

(above code is slighty updated, attached archive does not contain latest modification from above)

timerStopwatch.zip

Edited by KodeZwerg

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

×