Changeset 55
- Timestamp:
- Nov 5, 2019, 12:47:08 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.pas
r50 r55 69 69 if Length(KeyBuffer) > 1 then 70 70 Move(KeyBuffer[1], KeyBuffer[0], (Length(KeyBuffer) - 1) * SizeOf(Word)); 71 SetLength(KeyBuffer, Length(KeyBuffer) - 1); 71 if Length(KeyBuffer) > 0 then 72 SetLength(KeyBuffer, Length(KeyBuffer) - 1); 72 73 end; 73 74 end else begin -
trunk/UGame.pas
r53 r55 12 12 TGame = class; 13 13 TMoveDirection = (drNone, drLeft, drUp, drRight, drDown); 14 TTileAction = (taNone, taMove, taMerge, taAppear); 14 15 15 16 { TTile } … … 20 21 NewValue: Integer; 21 22 Merged: Boolean; 22 Moving: Boolean;23 Action: TTileAction; 23 24 Shift: TPoint; 24 25 procedure Assign(Source: TTile); … … 118 119 procedure SetSkin(AValue: TTileSkin); 119 120 procedure Win; 120 function FillRandomTile (Value4Change: Double = 0.1): TTile;121 function FillRandomTile: TTile; 121 122 function GetMoveArea(Direction: TMoveDirection): TArea; 122 123 procedure MoveAllAnimate(Direction: TMoveDirection); 123 124 function CanMergeTile(Value1, Value2: Integer): Boolean; 124 125 function MergeTile(Value1, Value2: Integer): Integer; 126 procedure AnimateTiles; 125 127 public 126 128 Board: TBoard; … … 131 133 History: THistory; 132 134 BackgroundColor: TColor; 135 Value2Chance: Double; 133 136 function CanUndo: Boolean; 134 137 procedure Undo; … … 519 522 end; 520 523 521 function TGame.FillRandomTile (Value4Change: Double = 0.1): TTile;524 function TGame.FillRandomTile: TTile; 522 525 var 523 526 EmptyTiles: TTiles; … … 528 531 Board.GetEmptyTiles(EmptyTiles); 529 532 if EmptyTiles.Count > 0 then begin 530 if Random < Value 4Change then NewValue := 2 else NewValue := 1;533 if Random < Value2Chance then NewValue := 2 else NewValue := 1; 531 534 Result := EmptyTiles[Random(EmptyTiles.Count)]; 532 535 Result.Value := NewValue; 536 Result.Action := taAppear; 533 537 end; 534 538 EmptyTiles.Free; … … 579 583 for I := 0 to InitialTileCount - 1 do begin 580 584 SetLength(History.InitialTiles, Length(History.InitialTiles) + 1); 581 Tile := FillRandomTile (0);585 Tile := FillRandomTile; 582 586 History.InitialTiles[Length(History.InitialTiles) - 1].Pos := Tile.Index; 583 587 History.InitialTiles[Length(History.InitialTiles) - 1].Value := Tile.Value; … … 585 589 end else begin 586 590 for I := 0 to InitialTileCount - 1 do 587 FillRandomTile(0); 588 end; 591 FillRandomTile; 592 end; 593 AnimateTiles; 589 594 DoChange; 590 595 end; … … 599 604 TopBarHeight: Integer; 600 605 TileMargin: Integer; 606 TileCenter: TPoint; 607 S: TPoint; 601 608 begin 602 609 TopBarHeight := ScaleY(24, 96); … … 643 650 for Y := 0 to Board.Size.Y - 1 do 644 651 for X := 0 to Board.Size.X - 1 do begin 645 if Board.Tiles[Y, X].Movingthen Canvas.Brush.Color := GetTileColor(0)652 if (Board.Tiles[Y, X].Action <> taNone) then Canvas.Brush.Color := GetTileColor(0) 646 653 else Canvas.Brush.Color := GetTileColor(Board.Tiles[Y, X].Value); 647 654 Canvas.Brush.Style := bsSolid; … … 650 657 Frame.Top + Y * TileSize.Y + TileMargin, 651 658 TileSize.X - 2 * TileMargin, TileSize.Y - 2 * TileMargin); 652 RenderTile(Canvas, Board.Tiles[Y, X], TileRect, not Board.Tiles[Y, X].Moving);653 end; 654 655 // Draw moving Tiles659 RenderTile(Canvas, Board.Tiles[Y, X], TileRect, Board.Tiles[Y, X].Action = taNone); 660 end; 661 662 // Draw moving tiles 656 663 for Y := 0 to Board.Size.Y - 1 do 657 664 for X := 0 to Board.Size.X - 1 do 658 if Board.Tiles[Y, X]. Movingthen begin665 if Board.Tiles[Y, X].Action = taMove then begin 659 666 Canvas.Brush.Color := GetTileColor(Board.Tiles[Y, X].Value); 660 667 Canvas.Brush.Style := bsSolid; … … 663 670 Frame.Top + Y * TileSize.Y + Trunc(Board.Tiles[Y, X].Shift.Y / 100 * TileSize.Y + TileMargin), 664 671 TileSize.X - 2 * TileMargin, TileSize.Y - 2 * TileMargin); 672 RenderTile(Canvas, Board.Tiles[Y, X], TileRect, True); 673 end; 674 675 // Draw moving tiles 676 for Y := 0 to Board.Size.Y - 1 do 677 for X := 0 to Board.Size.X - 1 do 678 if Board.Tiles[Y, X].Action = taAppear then begin 679 Canvas.Brush.Color := GetTileColor(Board.Tiles[Y, X].Value); 680 Canvas.Brush.Style := bsSolid; 681 TileRect := Bounds( 682 Frame.Left + X * TileSize.X + TileMargin, 683 Frame.Top + Y * TileSize.Y + TileMargin, 684 TileSize.X - 2 * TileMargin, 685 TileSize.Y - 2 * TileMargin); 686 TileCenter := TileRect.CenterPoint; 687 S := Point( 688 Trunc(Board.Tiles[Y, X].Shift.X / 100 * (TileSize.X - TileMargin)), 689 Trunc(Board.Tiles[Y, X].Shift.Y / 100 * (TileSize.Y - TileMargin)) 690 ); 691 TileRect := Rect(TileCenter.X - S.X div 2, TileCenter.Y - S.Y div 2, 692 TileCenter.X + S.X div 2, TileCenter.Y + S.Y div 2); 665 693 RenderTile(Canvas, Board.Tiles[Y, X], TileRect, True); 666 694 end; … … 694 722 if (Board.Tiles[P.Y, P.X].Value <> 0) then begin 695 723 if (Board.Tiles[PNew.Y, PNew.X].Value = 0) then begin 696 Board.Tiles[P.Y, P.X].Moving := True;697 724 Board.Tiles[PNew.Y, PNew.X].Value := Board.Tiles[P.Y, P.X].Value; 698 725 Board.Tiles[PNew.Y, PNew.X].Merged := Board.Tiles[P.Y, P.X].Merged; … … 702 729 if (not Board.Tiles[P.Y, P.X].Merged) and (not Board.Tiles[PNew.Y, PNew.X].Merged) and 703 730 CanMergeTile(Board.Tiles[PNew.Y, PNew.X].Value, Board.Tiles[P.Y, P.X].Value) then begin 704 Board.Tiles[P.Y, P.X].Moving := True;705 731 Board.Tiles[PNew.Y, PNew.X].Value := MergeTile(Board.Tiles[PNew.Y, PNew.X].Value, Board.Tiles[P.Y, P.X].Value); 706 732 Board.Tiles[PNew.Y, PNew.X].Merged := True; … … 846 872 for X := 0 to Board.Size.X - 1 do begin 847 873 Board.Tiles[Y, X].NewValue := Board.Tiles[Y, X].Value; 848 Board.Tiles[Y, X]. Moving := False;874 Board.Tiles[Y, X].Action := taNone; 849 875 end; 850 876 … … 858 884 if (Board.Tiles[P.Y, P.X].NewValue <> 0) then begin 859 885 if (Board.Tiles[PNew.Y, PNew.X].NewValue = 0) then begin 860 Board.Tiles[P.Y, P.X]. Moving := True;886 Board.Tiles[P.Y, P.X].Action := taMove; 861 887 Board.Tiles[PNew.Y, PNew.X].NewValue := Board.Tiles[P.Y, P.X].NewValue; 862 888 Board.Tiles[PNew.Y, PNew.X].Merged := Board.Tiles[P.Y, P.X].Merged; … … 867 893 if (not Board.Tiles[P.Y, P.X].Merged) and (not Board.Tiles[PNew.Y, PNew.X].Merged) and 868 894 CanMergeTile(Board.Tiles[PNew.Y, PNew.X].NewValue, Board.Tiles[P.Y, P.X].NewValue) then begin 869 Board.Tiles[P.Y, P.X]. Moving := True;895 Board.Tiles[P.Y, P.X].Action := taMove; 870 896 Board.Tiles[PNew.Y, PNew.X].NewValue := MergeTile(Board.Tiles[PNew.Y, PNew.X].NewValue, Board.Tiles[P.Y, P.X].NewValue); 871 897 Board.Tiles[PNew.Y, PNew.X].Merged := True; … … 893 919 for Y := 0 to Board.Size.Y - 1 do 894 920 for X := 0 to Board.Size.X - 1 do begin 895 if Board.Tiles[Y, X]. Movingthen921 if Board.Tiles[Y, X].Action = taMove then 896 922 Board.Tiles[Y, X].Shift := Point(Trunc(Part * DirectionDiff[Direction].X * 100), 897 923 Trunc(Part * DirectionDiff[Direction].Y * 100)); … … 913 939 for X := 0 to Board.Size.X - 1 do begin 914 940 Board.Tiles[Y, X].Shift := Point(0, 0); 915 Board.Tiles[Y, X]. Moving := False;941 Board.Tiles[Y, X].Action := taNone; 916 942 Board.Tiles[Y, X].Value := Board.Tiles[Y, X].NewValue; 917 943 end; … … 929 955 if Value1 = Value2 then Result := Value1 + 1 930 956 else Result := -1; 957 end; 958 959 procedure TGame.AnimateTiles; 960 var 961 I: Integer; 962 StartTime: TDateTime; 963 EndTime: TDateTime; 964 Time: TDateTime; 965 Part: Double; 966 X, Y: Integer; 967 begin 968 FMoving := True; 969 970 // Animate tiles move 971 StartTime := Now; 972 EndTime := StartTime + AnimationDuration / 300 * OneSecond / Max(Board.Size.X, Board.Size.Y); 973 if AnimationDuration > 0 then 974 repeat 975 Time := Now; 976 Part := (Time - StartTime) / (EndTime - StartTime); 977 if Part > 1 then Part := 1; 978 for Y := 0 to Board.Size.Y - 1 do 979 for X := 0 to Board.Size.X - 1 do begin 980 if Board.Tiles[Y, X].Action = taAppear then 981 Board.Tiles[Y, X].Shift := Point(Trunc(Part * 100), Trunc(Part * 100)); 982 end; 983 DoChange; 984 Application.ProcessMessages; 985 Sleep(1); 986 until Time > EndTime; 987 988 for Y := 0 to Board.Size.Y - 1 do 989 for X := 0 to Board.Size.X - 1 do 990 if Board.Tiles[Y, X].Action = taAppear then begin 991 Board.Tiles[Y, X].Action := taNone; 992 Board.Tiles[Y, X].Shift := Point(0, 0); 993 end; 994 DoChange; 995 FMoving := False; 931 996 end; 932 997 … … 962 1027 963 1028 NewTile := FillRandomTile; 1029 if Animation then AnimateTiles; 1030 964 1031 if RecordHistory then begin 965 1032 HistoryMove := THistoryMove.Create; … … 1049 1116 History := THistory.Create; 1050 1117 History.Game := Self; 1118 Value2Chance := 0.1; 1051 1119 end; 1052 1120
Note:
See TracChangeset
for help on using the changeset viewer.