Changeset 10 for trunk/UGame.pas


Ignore:
Timestamp:
Oct 4, 2019, 11:05:35 PM (5 years ago)
Author:
chronos
Message:
  • Added: Show Top score and remember it after application restart.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r9 r10  
    3232    FOnChange: TNotifyEvent;
    3333    FRunning: Boolean;
     34    FScore: Integer;
    3435    FSize: TPoint;
    3536    function GetCellColor(Value: Integer): TColor;
     37    procedure SetScore(AValue: Integer);
    3638    procedure SetSize(AValue: TPoint);
    3739    procedure GetEmptyCells(EmptyCells: TCells);
    3840    procedure DoChange;
    3941    procedure ClearMerged;
    40     function GetScore: Integer;
    4142  public
    4243    Cells: array of array of TCell;
     44    TopScore: Integer;
    4345    procedure GameOver;
    4446    function FillRandomCell: Integer;
     
    5456    constructor Create;
    5557    destructor Destroy; override;
    56     property Score: Integer read GetScore;
     58    property Score: Integer read FScore write SetScore;
    5759    property Size: TPoint read FSize write SetSize;
    5860    property Running: Boolean read FRunning write FRunning;
     
    187189begin
    188190  Clear;
     191  Score := 0;
    189192  Running := True;
    190193  for I := 0 to 1 do FillRandomCell;
     
    222225  Canvas.Font.Height := Trunc(TopBarHeight * 0.7);
    223226  Canvas.TextOut(ScaleY(16, 96), (TopBarHeight - Canvas.TextHeight(ValueStr)) div 2, ValueStr);
     227
     228  ValueStr := 'Top score: ' + IntToStr(TopScore);
     229  Canvas.Font.Color := clWhite;
     230  Canvas.Font.Height := Trunc(TopBarHeight * 0.7);
     231  Canvas.TextOut(ScaleY(106, 96), (TopBarHeight - Canvas.TextHeight(ValueStr)) div 2, ValueStr);
    224232
    225233  // Form.Canvas.Width and Form.Canvas.Height is not working correctly under Windows.
     
    377385              Cells[P.Y, P.X].Merged := False;
    378386              Inc(MovedCount);
     387              Score := Score + Cells[PNew.Y, PNew.X].NewValue;
    379388            end;
    380389          end;
     
    421430  Result := (Pos.X >= 0) and (Pos.X < FSize.X) and
    422431    (Pos.Y >= 0) and (Pos.Y < FSize.Y);
    423 end;
    424 
    425 function TGame.GetScore: Integer;
    426 var
    427   X, Y: Integer;
    428 begin
    429   Result := 0;
    430   for Y := 0 to Size.Y - 1 do
    431     for X := 0 to Size.X - 1 do
    432       Result := Result + Cells[Y, X].Value;
    433432end;
    434433
     
    462461end;
    463462
     463procedure TGame.SetScore(AValue: Integer);
     464begin
     465  if FScore = AValue then Exit;
     466  FScore := AValue;
     467  if FScore > TopScore then TopScore := FScore;
     468end;
     469
    464470end.
    465471
Note: See TracChangeset for help on using the changeset viewer.