Changeset 10


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.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r8 r10  
    3535  private
    3636  public
    37     Game: TGame;
    38     procedure GameChange(Sender: TObject);
    3937  end;
    4038
     
    5553  MovedCount: Integer;
    5654begin
    57   if Game.Running then begin
     55  if Core.Game.Running then begin
    5856    MovedCount := 0;
    5957    case Key of
    60       37: MovedCount := Game.MoveAll(drLeft);
    61       38: MovedCount := Game.MoveAll(drUp);
    62       39: MovedCount := Game.MoveAll(drRight);
    63       40: MovedCount := Game.MoveAll(drDown);
     58      37: MovedCount := Core.Game.MoveAll(drLeft);
     59      38: MovedCount := Core.Game.MoveAll(drUp);
     60      39: MovedCount := Core.Game.MoveAll(drRight);
     61      40: MovedCount := Core.Game.MoveAll(drDown);
    6462    end;
    65     if MovedCount > 0 then Game.FillRandomCell;
    66     if not Game.CanMove and (Game.GetEmptyCellsCount = 0) then
    67       Game.GameOver;
     63    if MovedCount > 0 then Core.Game.FillRandomCell;
     64    if not Core.Game.CanMove and (Core.Game.GetEmptyCellsCount = 0) then
     65      Core.Game.GameOver;
    6866  end;
    6967end;
     
    7169procedure TFormMain.FormPaint(Sender: TObject);
    7270begin
    73   Game.Render(Canvas, Point(Width, Height - MainMenu1.Height));
     71  Core.Game.Render(Canvas, Point(Width, Height - MainMenu1.Height));
    7472end;
    7573
     
    7876  FormNew := TFormNew.Create(nil);
    7977  try
    80     FormNew.Load(Game);
     78    FormNew.Load(Core.Game);
    8179    if FormNew.ShowModal = mrOk then begin
    82       FormNew.Save(Game);
    83       Game.New;
     80      FormNew.Save(Core.Game);
     81      Core.Game.New;
    8482    end;
    8583  finally
     
    111109procedure TFormMain.FormCreate(Sender: TObject);
    112110begin
    113   Randomize;
    114   Game := TGame.Create;
    115   Game.Size := Point(4, 4);
    116   Game.OnChange := GameChange;
    117111end;
    118112
    119113procedure TFormMain.FormDestroy(Sender: TObject);
    120114begin
    121   Game.Free;
    122115end;
    123116
     
    126119  Core.PersistentForm1.RegistryContext := Core.ApplicationInfo1.GetRegistryContext;
    127120  Core.PersistentForm1.Load(Self);
    128   Game.New;
    129   {
    130   Game.Cells[0, 0].Value := 1;
    131   Game.Cells[0, 1].Value := 2;
    132   Game.Cells[0, 2].Value := 3;
    133   Game.Cells[1, 0].Value := 4;
    134   Game.Cells[1, 1].Value := 5;
    135   Game.Cells[1, 2].Value := 6;
    136   Game.Cells[2, 0].Value := 7;
    137   Game.Cells[2, 1].Value := 8;
    138   Game.Cells[2, 2].Value := 9;
    139   }
    140 end;
    141 
    142 procedure TFormMain.GameChange(Sender: TObject);
    143 begin
    144   Repaint;
     121  Core.Game.New;
    145122end;
    146123
  • trunk/UCore.lfm

    r8 r10  
    11object Core: TCore
     2  OnCreate = DataModuleCreate
     3  OnDestroy = DataModuleDestroy
    24  OldCreateOrder = False
    35  Height = 534
  • trunk/UCore.pas

    r8 r10  
    66
    77uses
    8   Classes, SysUtils, UTheme, UPersistentForm, UApplicationInfo, UTranslator;
     8  Classes, SysUtils, UTheme, UPersistentForm, UApplicationInfo, UTranslator,
     9  URegistry, UGame;
    910
    1011type
     
    1718    ThemeManager1: TThemeManager;
    1819    Translator1: TTranslator;
     20    procedure DataModuleCreate(Sender: TObject);
     21    procedure DataModuleDestroy(Sender: TObject);
    1922  private
    20 
     23    procedure GameChange(Sender: TObject);
    2124  public
    22 
     25    Game: TGame;
     26    procedure LoadConfig;
     27    procedure SaveConfig;
    2328  end;
    2429
     
    3035{$R *.lfm}
    3136
     37uses
     38  UFormMain;
     39
     40{ TCore }
     41
     42procedure TCore.DataModuleCreate(Sender: TObject);
     43begin
     44  Randomize;
     45  Game := TGame.Create;
     46  Game.Size := Point(4, 4);
     47  Game.OnChange := GameChange;
     48  LoadConfig;
     49end;
     50
     51procedure TCore.DataModuleDestroy(Sender: TObject);
     52begin
     53  SaveConfig;
     54  FreeAndNil(Game);
     55end;
     56
     57procedure TCore.GameChange(Sender: TObject);
     58begin
     59  FormMain.Repaint;
     60end;
     61
     62procedure TCore.LoadConfig;
     63begin
     64  with TRegistryEx.Create do
     65  try
     66    CurrentContext := ApplicationInfo1.GetRegistryContext;
     67
     68    Game.TopScore := ReadIntegerWithDefault('TopScore', 0);
     69  finally
     70    Free;
     71  end;
     72end;
     73
     74procedure TCore.SaveConfig;
     75begin
     76  with TRegistryEx.Create do
     77  try
     78    CurrentContext := ApplicationInfo1.GetRegistryContext;
     79
     80    WriteInteger('TopScore', Game.TopScore);
     81  finally
     82    Free;
     83  end;
     84end;
     85
    3286end.
    3387
  • 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.