unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, Grids, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
DrawGrid1: TDrawGrid;
ImageList1: TImageList;
btView_source: TButton;
procedure FormCreate(Sender: TObject);
procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure btView_sourceClick(Sender: TObject);
private
pict:array[1..5,1..5]of byte;
Switch:TPoint;
SwitchB:boolean;
{ Private declarations }
public
Source_BMP:TPicture;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses source_bmp;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
TempB,BMP:TBitmap;
x1,y1,x2,y2,i,j:byte;
DestRect,SourceRect:TRect;
begin
Randomize;
for i:=1 to 5 do //установка правильного порядка частей рисунка
for j:=1 to 5 do
pict[i,j]:=(i-1)*5+j;
BMP:=TBitmap.Create;
BMP.Width:=400; BMP.Height:=400;
TempB:=TBitmap.Create;
TempB.Width:=80;TempB.Height:=80;
Bmp.LoadFromFile('BMP.bmp');
//
Source_BMP:=TPicture.Create;
//Source_BMP.Width:=400; Source_BMP.Height:=400;
Source_BMP.Bitmap:=bmp;
//
for i:=1 to 5 do
for j:=1 to 5 do
begin //а это — копирование частей рисунка
SourceRect:=Rect((j-1)*80,(i-1)*80,j*80,i*80);
DestRect:=Rect(0,0,80,80);
TempB.Canvas.CopyRect(DestRect,BMP.Canvas, SourceRect);
ImageList1.Add(TempB,nil);
end;
BMP.Destroy; TempB.Destroy;
for i:=1 to 21 do
begin //в этом цикле фрагменты картинки меняются местами
repeat
x1:=random(5)+1;y1:=random(5)+1;
x2:=random(5)+1;y2:=random(5)+1;
until (x1<>x2)and(y2<>y1);
j:=pict[x2,y2];
pict[x2,y2]:=pict[x1,y1];
pict[x1,y1]:=j;
end;
end;
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
Tag := Pict[ARow+1,ACol+1]-1;
ImageList1.Draw(DrawGrid1.Canvas,Rect.Left,Rect.Top,Tag);
end;
procedure TForm1.DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i,j,t:byte;
win:boolean;
begin
if SwitchB then //надо менять фрагменты местами
begin
t:=pict[y div 81+1,x div 81+1];
pict[y div 81+1,x div 81+1]:=pict[switch.y,Switch.x];
pict[switch.y,Switch.x]:=t;
SwitchB:=false;
end
else //первое нажатие в паре «выбор-обмен»
begin
Switch.x:=x div 81 +1;
Switch.y:=y div 81 +1;
SwitchB:=true;
end;
win:=true;
for i:=1 to 5 do
for j:=1 to 5 do
win:=win and (pict[i,j]=(i-1)*5+j);
if win then // если все собрано.,
application.MessageBox('Молодец ты все собрал!','Победа!', MB_ICONINFORMATION);
end;
procedure TForm1.btView_sourceClick(Sender: TObject);
begin
formSource_image.Image1.Canvas.Draw(0,0,Source_BMP.Graphic);
formSource_image.Show
end;
end.