Advertisement
jdelano

Untitled

Feb 12th, 2025
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.20 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Jpeg;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     imgPos1: TImage;
  12.     imgPos2: TImage;
  13.     imgPos3: TImage;
  14.     imgPos4: TImage;
  15.     imgPos5: TImage;
  16.     imgPos6: TImage;
  17.     procedure FormShow(Sender: TObject);
  18.     procedure imgPos1Click(Sender: TObject);
  19.   private
  20.     { Private declarations }
  21.   public
  22.     { Public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.   lastClickedMovableImage: TImage; {hold the component that was clicked}
  28.  
  29.   imgNameArray: Tarray<string>; {array to know which image is in which component}
  30.  
  31.   const aImageFileName = 'E:\Delphi Projects\MoveImage\A.jpg';
  32.   const whiteImageFileName = 'E:\Delphi Projects\MoveImage\white.jpg';
  33. implementation
  34.  
  35. {$R *.dfm}
  36.  
  37. procedure TForm1.FormShow(Sender: TObject);
  38. var
  39.   appPath: String;
  40.   posImg: TImage;
  41.   pos: Integer;
  42.   imgPosName: String;
  43.  
  44. begin
  45.   {you'll need this to point to the EXE path after the project is compiled
  46.    to point to the image files included with the EXE}
  47.   appPath := ExtractFileDir(Application.ExeName);
  48.  
  49.   {set the initial position images}
  50.   for pos := 1 to 6 do
  51.     begin
  52.       {find each TImage component on the form}
  53.       imgPosName := Concat('imgPos', pos.ToString());
  54.       posImg := TImage(FindComponent(imgPosName));
  55.  
  56.       if (pos = 1) then
  57.         begin {default position 1 as teh starting position for the image that will move}
  58.           posImg.Picture.LoadFromFile(aImageFileName);
  59.         end
  60.       else
  61.         begin
  62.           posImg.Picture.LoadFromFile(whiteImageFileName);
  63.           posImg.OnClick := imgPos1.OnClick;  {assign all other TImage components the same event handler}
  64.         end;
  65.  
  66.     end;
  67.  
  68.     imgNameArray :=  TArray<string>.Create('A','white','white','white','white','white');
  69. end;
  70.  
  71. procedure TForm1.imgPos1Click(Sender: TObject);
  72. var
  73.   imgName: String;
  74.   clickedImg: TImage;
  75.   imgPos : Integer;
  76.  
  77. begin
  78.  
  79.   imgName := TImage(Sender).Name;   {component name which contians its position on the form}
  80.   imgPos := StrToInt(imgName.Substring(6));
  81.  
  82.   clickedImg := TImage(Sender);
  83.  
  84.   {what image is in this clicked TImage component}
  85.   if imgNameArray[imgPos - 1] = 'A' then
  86.     begin
  87.       {this image can be moved}
  88.       lastClickedMovableImage := clickedImg {remember this image}
  89.  
  90.     end
  91.   else
  92.     begin
  93.       if lastClickedMovableImage <> nil then
  94.         {a white space was clicked and there was a value in lastClickedImage
  95.          meaning it was the image that can be moved}
  96.         begin
  97.           {move the A image from it's current location to the last clicked
  98.            white space}
  99.           clickedImg.Picture.LoadFromFile(aImageFileName);
  100.  
  101.           imgNameArray[imgPos - 1] := 'A';
  102.  
  103.           {put a white image in the previous location of the moveable image}
  104.           lastClickedMovableImage.Picture.LoadFromFile(whiteImageFileName);
  105.  
  106.           imgName := lastClickedMovableImage.Name;
  107.           imgPos := StrToInt(imgName.Substring(6));
  108.           imgNameArray[imgPos - 1] := 'white';
  109.  
  110.           lastClickedMovableImage := nil;
  111.         end;
  112.     end;
  113. end;
  114.  
  115. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement