Changeset 5
- Timestamp:
- Sep 24, 2019, 10:09:45 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 1 1 Game2048 2 2 Game2048.lps 3 Game2048.dbg 3 4 lib 5 heaptrclog.trc
-
- Property svn:ignore
-
trunk/Game2048.lpr
r2 r5 7 7 cthreads, 8 8 {$ENDIF}{$ENDIF} 9 Interfaces, // this includes the LCL widgetset9 Interfaces, SysUtils,// this includes the LCL widgetset 10 10 Forms, UFormMain, UGame, UFormNew 11 11 { you can add units after this }; … … 13 13 {$R *.res} 14 14 15 {$if declared(UseHeapTrace)} 16 const 17 HeapTraceLog = 'heaptrclog.trc'; 18 {$ENDIF} 19 20 15 21 begin 22 {$if declared(UseHeapTrace)} 23 DeleteFile(ExtractFilePath(ParamStr(0)) + HeapTraceLog); 24 SetHeapTraceOutput(ExtractFilePath(ParamStr(0)) + HeapTraceLog); 25 {$ENDIF} 16 26 RequireDerivedFormResource:=True; 17 27 Application.Initialize; -
trunk/UFormMain.lfm
r2 r5 1 1 object FormMain: TFormMain 2 2 Left = 684 3 Height = 3604 Top = 3 915 Width = 4803 Height = 521 4 Top = 307 5 Width = 580 6 6 Caption = '2048 game' 7 7 DesignTimePPI = 144 … … 12 12 OnPaint = FormPaint 13 13 OnShow = FormShow 14 LCLVersion = ' 1.8.4.0'14 LCLVersion = '2.0.2.0' 15 15 object MainMenu1: TMainMenu 16 16 left = 325 … … 20 20 object MenuItem1: TMenuItem 21 21 Action = AGameNew 22 end 23 object MenuItem2: TMenuItem 24 Action = AExit 22 25 end 23 26 end … … 30 33 OnExecute = AGameNewExecute 31 34 end 35 object AExit: TAction 36 Caption = 'Exit' 37 OnExecute = AExitExecute 38 end 32 39 end 33 40 end -
trunk/UFormMain.pas
r2 r5 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, 9 ActnList, UGame;9 ActnList, ExtCtrls, StdCtrls, UGame; 10 10 11 11 type … … 14 14 15 15 TFormMain = class(TForm) 16 AExit: TAction; 16 17 AGameNew: TAction; 17 18 ActionList1: TActionList; 18 19 MainMenu1: TMainMenu; 19 20 MenuItem1: TMenuItem; 21 MenuItem2: TMenuItem; 20 22 MenuItemGame: TMenuItem; 23 procedure AExitExecute(Sender: TObject); 21 24 procedure AGameNewExecute(Sender: TObject); 22 25 procedure FormCreate(Sender: TObject); … … 62 65 end; 63 66 67 procedure TFormMain.FormPaint(Sender: TObject); 68 begin 69 Game.Render(Canvas); 70 end; 71 64 72 procedure TFormMain.AGameNewExecute(Sender: TObject); 65 73 begin … … 69 77 Game.New; 70 78 end; 79 end; 80 81 procedure TFormMain.AExitExecute(Sender: TObject); 82 begin 83 Close; 71 84 end; 72 85 … … 82 95 begin 83 96 Game.Free; 84 end;85 86 procedure TFormMain.FormPaint(Sender: TObject);87 begin88 Game.Render(Canvas);89 97 end; 90 98 -
trunk/UGame.pas
r4 r5 6 6 7 7 uses 8 Classes, SysUtils, Dialogs, fgl, Graphics ;8 Classes, SysUtils, Dialogs, fgl, Graphics, Types; 9 9 10 10 type … … 35 35 procedure DoChange; 36 36 procedure ClearMerged; 37 function GetScore: Integer; 37 38 public 38 39 Cells: array of array of TCell; … … 49 50 function IsValidPos(Pos: TPoint): Boolean; 50 51 constructor Create; 52 destructor Destroy; override; 53 property Score: Integer read GetScore; 51 54 property Size: TPoint read FSize write SetSize; 52 55 property Running: Boolean read FRunning write FRunning; … … 201 204 ValueStr: string; 202 205 Frame: TRect; 203 begin 204 CellSize := Point(Canvas.Width div Size.X, Canvas.Height div Size.Y); 206 TextSize: TSize; 207 const 208 TopBarHeight = 32; 209 begin 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); 205 220 if CellSize.X < CellSize.Y then CellSize.Y := CellSize.X; 206 221 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 213 227 Canvas.Font.Color := clBlack; 214 Canvas.Font.Height := Trunc(CellSize.Y * 0.7);215 228 for Y := 0 to Size.Y - 1 do 216 229 for X := 0 to Size.X - 1 do begin … … 221 234 if Cells[Y, X].Value <> 0 then begin 222 235 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); 223 242 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); 226 245 end; 227 246 end; … … 323 342 end; 324 343 344 function TGame.GetScore: Integer; 345 var 346 X, Y: Integer; 347 begin 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; 352 end; 353 325 354 constructor TGame.Create; 326 355 begin 356 end; 357 358 destructor TGame.Destroy; 359 begin 360 Size := Point(0, 0); 361 inherited; 327 362 end; 328 363
Note:
See TracChangeset
for help on using the changeset viewer.