Changeset 5


Ignore:
Timestamp:
Sep 24, 2019, 10:09:45 PM (5 years ago)
Author:
chronos
Message:
  • Added: Show game score.
  • Fixed: Make cell font smaller if text is too width.
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        11Game2048
        22Game2048.lps
         3Game2048.dbg
        34lib
         5heaptrclog.trc
  • trunk/Game2048.lpr

    r2 r5  
    77  cthreads,
    88  {$ENDIF}{$ENDIF}
    9   Interfaces, // this includes the LCL widgetset
     9  Interfaces, SysUtils,// this includes the LCL widgetset
    1010  Forms, UFormMain, UGame, UFormNew
    1111  { you can add units after this };
     
    1313{$R *.res}
    1414
     15{$if declared(UseHeapTrace)}
     16const
     17  HeapTraceLog = 'heaptrclog.trc';
     18{$ENDIF}
     19
     20
    1521begin
     22  {$if declared(UseHeapTrace)}
     23  DeleteFile(ExtractFilePath(ParamStr(0)) + HeapTraceLog);
     24  SetHeapTraceOutput(ExtractFilePath(ParamStr(0)) + HeapTraceLog);
     25  {$ENDIF}
    1626  RequireDerivedFormResource:=True;
    1727  Application.Initialize;
  • trunk/UFormMain.lfm

    r2 r5  
    11object FormMain: TFormMain
    22  Left = 684
    3   Height = 360
    4   Top = 391
    5   Width = 480
     3  Height = 521
     4  Top = 307
     5  Width = 580
    66  Caption = '2048 game'
    77  DesignTimePPI = 144
     
    1212  OnPaint = FormPaint
    1313  OnShow = FormShow
    14   LCLVersion = '1.8.4.0'
     14  LCLVersion = '2.0.2.0'
    1515  object MainMenu1: TMainMenu
    1616    left = 325
     
    2020      object MenuItem1: TMenuItem
    2121        Action = AGameNew
     22      end
     23      object MenuItem2: TMenuItem
     24        Action = AExit
    2225      end
    2326    end
     
    3033      OnExecute = AGameNewExecute
    3134    end
     35    object AExit: TAction
     36      Caption = 'Exit'
     37      OnExecute = AExitExecute
     38    end
    3239  end
    3340end
  • trunk/UFormMain.pas

    r2 r5  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
    9   ActnList, UGame;
     9  ActnList, ExtCtrls, StdCtrls, UGame;
    1010
    1111type
     
    1414
    1515  TFormMain = class(TForm)
     16    AExit: TAction;
    1617    AGameNew: TAction;
    1718    ActionList1: TActionList;
    1819    MainMenu1: TMainMenu;
    1920    MenuItem1: TMenuItem;
     21    MenuItem2: TMenuItem;
    2022    MenuItemGame: TMenuItem;
     23    procedure AExitExecute(Sender: TObject);
    2124    procedure AGameNewExecute(Sender: TObject);
    2225    procedure FormCreate(Sender: TObject);
     
    6265end;
    6366
     67procedure TFormMain.FormPaint(Sender: TObject);
     68begin
     69  Game.Render(Canvas);
     70end;
     71
    6472procedure TFormMain.AGameNewExecute(Sender: TObject);
    6573begin
     
    6977    Game.New;
    7078  end;
     79end;
     80
     81procedure TFormMain.AExitExecute(Sender: TObject);
     82begin
     83  Close;
    7184end;
    7285
     
    8295begin
    8396  Game.Free;
    84 end;
    85 
    86 procedure TFormMain.FormPaint(Sender: TObject);
    87 begin
    88   Game.Render(Canvas);
    8997end;
    9098
  • trunk/UGame.pas

    r4 r5  
    66
    77uses
    8   Classes, SysUtils, Dialogs, fgl, Graphics;
     8  Classes, SysUtils, Dialogs, fgl, Graphics, Types;
    99
    1010type
     
    3535    procedure DoChange;
    3636    procedure ClearMerged;
     37    function GetScore: Integer;
    3738  public
    3839    Cells: array of array of TCell;
     
    4950    function IsValidPos(Pos: TPoint): Boolean;
    5051    constructor Create;
     52    destructor Destroy; override;
     53    property Score: Integer read GetScore;
    5154    property Size: TPoint read FSize write SetSize;
    5255    property Running: Boolean read FRunning write FRunning;
     
    201204  ValueStr: string;
    202205  Frame: TRect;
    203 begin
    204   CellSize := Point(Canvas.Width div Size.X, Canvas.Height div Size.Y);
     206  TextSize: TSize;
     207const
     208  TopBarHeight = 32;
     209begin
     210  Canvas.Brush.Style := bsSolid;
     211  Canvas.Brush.Color := clBlack;
     212  Canvas.FillRect(0, 0, Canvas.Width, Canvas.Height);
     213
     214  Canvas.Font.Color := clWhite;
     215  Canvas.Font.Height := 20;
     216  Canvas.TextOut(16, 4, 'Score: ' + IntToStr(Score));
     217
     218  Frame := Rect(0, TopBarHeight, Canvas.Width, Canvas.Height);
     219  CellSize := Point(Frame.Width div Size.X, Frame.Height div Size.Y);
    205220  if CellSize.X < CellSize.Y then CellSize.Y := CellSize.X;
    206221  if CellSize.Y < CellSize.X then CellSize.X := CellSize.Y;
    207   Frame := Rect(Canvas.Width div 2 - (Size.X * CellSize.X) div 2,
    208     Canvas.Height div 2 - (Size.Y * CellSize.Y) div 2,
    209     Canvas.Width div 2 + (Size.X * CellSize.X) div 2,
    210     Canvas.Height div 2 + (Size.Y * CellSize.Y) div 2);
    211 
    212   Canvas.FillRect(0, 0, Canvas.Width, Canvas.Height);
     222  Frame := Rect(Frame.Width div 2 - (Size.X * CellSize.X) div 2,
     223    Frame.Top + Frame.Height div 2 - (Size.Y * CellSize.Y) div 2,
     224    Frame.Width div 2 + (Size.X * CellSize.X) div 2,
     225    Frame.Top + Frame.Height div 2 + (Size.Y * CellSize.Y) div 2);
     226
    213227  Canvas.Font.Color := clBlack;
    214   Canvas.Font.Height := Trunc(CellSize.Y * 0.7);
    215228  for Y := 0 to Size.Y - 1 do
    216229    for X := 0 to Size.X - 1 do begin
     
    221234      if Cells[Y, X].Value <> 0 then begin
    222235        ValueStr := IntToStr(Cells[Y, X].Value);
     236        Canvas.Brush.Style := bsClear;
     237        Canvas.Font.Height := Trunc(CellSize.Y * 0.7); // * (CellSize.X * 0.7) / Canvas.TextWidth(ValueStr));
     238        TextSize := Canvas.TextExtent(ValueStr);
     239        if TextSize.Width > CellSize.X then
     240          Canvas.Font.Height := Trunc(Canvas.Font.Height / TextSize.Width * CellSize.X);
     241        TextSize := Canvas.TextExtent(ValueStr);
    223242        Canvas.TextOut(Frame.Left + X * CellSize.X + CellSize.X div 2 -
    224           Canvas.TextWidth(ValueStr) div 2,
    225           Frame.Top + Y * CellSize.Y, ValueStr);
     243          TextSize.Width div 2,
     244          Frame.Top + Y * CellSize.Y + CellSize.Y div 2 - TextSize.Height div 2, ValueStr);
    226245      end;
    227246    end;
     
    323342end;
    324343
     344function TGame.GetScore: Integer;
     345var
     346  X, Y: Integer;
     347begin
     348  Result := 0;
     349  for Y := 0 to Size.Y - 1 do
     350    for X := 0 to Size.X - 1 do
     351      Result := Result + Cells[Y, X].Value;
     352end;
     353
    325354constructor TGame.Create;
    326355begin
     356end;
     357
     358destructor TGame.Destroy;
     359begin
     360  Size := Point(0, 0);
     361  inherited;
    327362end;
    328363
Note: See TracChangeset for help on using the changeset viewer.