Changeset 5 for trunk/UGame.pas


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:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        11Game2048
        22Game2048.lps
         3Game2048.dbg
        34lib
         5heaptrclog.trc
  • 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.