program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.RES}
begin
Application.Initialize;
Form2:=TForm2.Create(Application);
Form2.ShowModal;
Form2.Free;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;
type
TDragWord=(Drag_Upward, //向上移动
Drag_Downward, //向下移动
Drag_Rightward, //向右移动
Drag_Leftward); //向左移动
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Button1: TButton;
Button2: TButton;
Timer1: TTimer;
Label12: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure BitBtn9Click(Sender: TObject);
procedure BitBtn10Click(Sender: TObject);
procedure BitBtn6Click(Sender: TObject);
procedure BitBtn8Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn5Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure MouseMove(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Function InvalidMove(X:integer;Y:Integer;ButtonNumber:integer):Boolean;
procedure Timer1Timer(Sender: TObject);
Private
SpendTime:Integer;
{ Private declarations }
//用于保存每个BitBtn组件的宽度和高度
Position:array[0..9,0..1] of integer;
//用于保存每个BitBtn组件的宽度和高度
WidthHeight:array[0..9,0..1] of integer;
//用于显示是否准备移动
PrepareMove:Boolean;
//移动方向
Moveward:TDragWord;
//用户拖动BitBtn组件时的起始位置
BeginPosition:TPoint;
//判断是否允许用户的拖动
{ Private declarations }
BitBtn:array[0..9] of TBitBtn;//TBitBtn数组
Y,X:integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
SpendTime := 0;
PrepareMove:=FALSE;
Position[0][0]:= 6; //用于保存将军1的Left属性
Position[0][1]:= 6; //用于保存将军1的Top属性
Position[1][0]:= 80; //曹操
Position[1][1]:= 6;
Position[2][0]:= 228; //将军2
Position[2][1]:= 6;
Position[3][0]:= 6; //将军3
Position[3][1]:= 148;
Position[4][0]:= 80; //关羽
Position[4][1]:= 148;
Position[5][0]:= 228; //将军4
Position[5][1]:= 148;
Position[6][0]:= 80; //小兵1
Position[6][1]:= 219;
Position[7][0]:= 154; //小兵2
Position[7][1]:= 219;
Position[8][0]:= 6; //小兵3
Position[8][1]:= 290;
Position[9][0]:= 228; //小兵4
Position[9][1]:= 290;
WidthHeight[0][0]:= 74;
WidthHeight[0][1]:= 142;
WidthHeight[1][0]:= 148;
WidthHeight[1][1]:= 142;
WidthHeight[2][0]:= 74;
WidthHeight[2][1]:= 142;
WidthHeight[3][0]:= 74;
WidthHeight[3][1]:= 142;
WidthHeight[4][0]:= 148;
WidthHeight[4][1]:= 71;
WidthHeight[5][0]:= 74;
WidthHeight[5][1]:= 142;
WidthHeight[6][0]:= 74;
WidthHeight[6][1]:= 71;
WidthHeight[7][0]:= 74;
WidthHeight[7][1]:= 71;
WidthHeight[8][0]:= 74;
WidthHeight[8][1]:= 71;
WidthHeight[9][0]:= 74;
WidthHeight[9][1]:= 71;
for i:=0 to 9 do
begin
BitBtn:=TBitBtn.Create(Panel1); //动态创建数组
BitBtn.Parent:=Panel1;
BitBtn.Left:=Position[0];
BitBtn.Top:=Position[1];
BitBtn.Width:=WidthHeight[0];
BitBtn.Height:=WidthHeight[1];
BitBtn.Tag:=i;
BitBtn.OnMouseDown:=MouseMove; //设置事件相应处理程序
BitBtn.OnMouseUp:=MouseUp;
end;
BitBtn[0].Glyph.LoadFromFile('张.bmp');
BitBtn[1].Glyph.LoadFromFile('曹.bmp');
BitBtn[2].Glyph.LoadFromFile('赵.bmp');
BitBtn[3].Glyph.LoadFromFile('马.bmp');
BitBtn[4].Glyph.LoadFromFile('关.bmp');
BitBtn[5].Glyph.LoadFromFile('黄.bmp');
BitBtn[6].Glyph.LoadFromFile('1.bmp');
BitBtn[7].Glyph.LoadFromFile('2.bmp');
BitBtn[8].Glyph.LoadFromFile('3.bmp');
BitBtn[9].Glyph.LoadFromFile('4.bmp');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
Timer1.Enabled:=true;
SpendTime:=0;
Position[0][0]:= 6; //将军1的Left属性值
Position[0][1]:= 6; //将军1的Top属性值
Position[1][0]:= 80; //曹操
Position[1][1]:= 6;
Position[2][0]:= 228; //将军2
Position[2][1]:= 6;
Position[3][0]:= 6; //将军3
Position[3][1]:= 148;
Position[4][0]:= 80; //关羽
Position[4][1]:= 148;
Position[5][0]:= 228; //将军4
Position[5][1]:= 148;
Position[6][0]:= 80; //小兵1
Position[6][1]:= 219;
Position[7][0]:= 154; //小兵2
Position[7][1]:= 219;
Position[8][0]:= 6; //小兵3
Position[8][1]:= 290;
Position[9][0]:= 228; //小兵4
Position[9][1]:= 290;
for i:=0 to 9 do
begin
BitBtn.Left:=Position[0]; //复原各个按钮的位置
BitBtn.Top:=Position[1];
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.BitBtn9Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn10Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn6Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn8Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn4Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn5Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
procedure TForm1.MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var MessageString:String;
begin
//判断移动方向
if (PrepareMove) then
begin
if(X>BeginPosition.x) then //如果向右拖
begin
if(Y>BeginPosition.y) then //如果向下拖
begin
if((X-BeginPosition.x)>(Y-BeginPosition.y))then
//如果向右拖的量比向下拖的量大
Moveward:=Drag_Rightward //确定为向右拖
else
Moveward:=Drag_Downward; //确定为向下拖
end
else //如果向上拖
begin
if((X-BeginPosition.x)>(BeginPosition.y-Y))then
//如果向右拖的量比向上拖的量大
Moveward:=Drag_Rightward //确定为向右拖
else
Moveward:=Drag_Upward; //确定为向上拖
end;
end
else //如果为向左拖
begin
if(Y>BeginPosition.y) then //如果为向下拖
begin
if((BeginPosition.x-X)>(Y-BeginPosition.y)) then
//如果向左拖的量比向下拖的量大
Moveward:=Drag_Leftward //确定为向左拖
else
Moveward:=Drag_Downward;
end
else //如果向上拖
begin
if((BeginPosition.x-X)>(BeginPosition.y-Y)) then
//如果向左拖的量比向上拖的量大
Moveward:=Drag_Leftward //确定为向左拖
else
Moveward:=Drag_Upward;
end;
end;
//判断当前拖动是否有效
if(InvalidMove(TBitBtn(Sender).Left + TBitBtn(Sender).Width div 2,
TBitBtn(Sender).Top + TBitBtn(Sender).Height div 2,
TBitBtn(Sender).Tag)) then
begin
if(Moveward=Drag_Upward) then //如果向上移动
begin
BitBtn[TBitBtn(Sender).Tag].Top:=BitBtn[TBitBtn(Sender)
.Tag].Top-71;
Position[TBitBtn(Sender).Tag][1]:=Position[TBitBtn(Sender)
.Tag] [1]-71;
end
else if(Moveward=Drag_Downward) then //如果向下移动
begin
BitBtn[TBitBtn(Sender).Tag].Top:=BitBtn[TBitBtn(Sender)
.Tag].Top+71;
Position[TBitBtn(Sender).Tag][1]:=Position[TBitBtn(Sender)
.Tag][1]+71;
end
else if(Moveward=Drag_Leftward) then //如果向左移动
begin
BitBtn[TBitBtn(Sender).Tag].Left:=BitBtn[TBitBtn(Sender)
.Tag].Left-74;
Position[TBitBtn(Sender).Tag][0]:=Position[TBitBtn(Sender)
.Tag][0]-74;
end
else //如果向右移动
begin
BitBtn[TBitBtn(Sender).Tag].Left:=BitBtn[TBitBtn(Sender)
.Tag].Left+74;
Position[TBitBtn(Sender).Tag][0]:=Position[TBitBtn(Sender)
.Tag][0]+74;
end
end;
end;
if ((Position[1][0]=80) and (Position[1][1]=219)) then
begin
Timer1.Enabled:=False;
MessageString:='你胜利了!你真行!,你所花时间为:'+
IntToStr(SpendTime)+'秒';
Application.MessageBox(PChar(MessageString),'恭喜',MB_OK);
end;
end;
procedure TForm1.MouseMove(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
PrepareMove:=TRUE;
BeginPosition.x:=X;
BeginPosition.y:=Y;
end;
Function TForm1.InvalidMove(X:integer;Y:Integer;ButtonNumber:integer):Boolean;
//判断是否允许用户的拖动
var
tempY,tempX,i:integer;
begin
if (Moveward=Drag_Upward) then
begin
tempY:=Y-71;
tempX:=X;
//判断上一个单元格中是否有方块,如果有则返回false,表示不能移动
for i:=0 to 9 do
begin
if (i=ButtonNumber) then
continue;
if((tempY>=Position[1]) and (tempY<=Position[1]+
WidthHeight[1])
and
(tempX>=Position[0]) and (tempX<=Position[0]+
WidthHeight[0]))
then
begin
InvalidMove:=false;
exit;
end;
end;
//防止从顶部出去
if(Position[ButtonNumber][1]=6)then
begin
InvalidMove:=false;
exit;
end;
InvalidMove:=true;
exit;
end
else if(Moveward=Drag_Downward) then
begin
tempY:=Y+71;
tempX:=X;
//判断下一个单元格中是否有方块,如果有则返回false,表示不能移动
for i:=0 to 9 do
begin
if (i=ButtonNumber) then
continue;
if((tempY>=Position[1]) and (tempY<=Position[1]+
WidthHeight[1])
and
(tempX>=Position[0]) and (tempX<=Position[0]+
WidthHeight[0]))
then
begin
InvalidMove:=false;
exit;
end;
end;
//防止从底部出去
if(Position[ButtonNumber][1]+WidthHeight[ButtonNumber][1]=361)
then
begin
InvalidMove:=false;
exit;
end;
InvalidMove:=true;
exit;
end
else if(Moveward=Drag_Rightward) then
begin
tempY:=Y;
tempX:=X+74;
//判断下一个单元格中是否有方块,如果有则返回false,表示不能移动
for i:=0 to 9 do
begin
if(i=ButtonNumber) then
continue;
if((tempY>=Position[1]) and (tempY<=Position[1]+
WidthHeight[1])
and
(tempX>=Position[0]) and (tempX<=Position[0]+
WidthHeight[0]))
then
begin
InvalidMove:=false;
exit;
end;
end;
//防止从右部出去
if(Position[ButtonNumber][0]+WidthHeight[ButtonNumber][0]=302)
then
begin
InvalidMove:=false;
exit;
end;
InvalidMove:=true;
exit;
end
else
begin
tempY:=Y;
tempX:=X-74;
//判断下一个单元格中是否有方块,如果有则返回false,表示不能移动
for i:=0 to 9 do
begin
if(i=ButtonNumber) then
continue;
if((tempY>=Position[1]) and (tempY<=Position[1]+
WidthHeight[1])
and
(tempX>=Position[0]) and (tempX<=Position[0]+
WidthHeight[0]))
then
begin
InvalidMove:=false;
exit;
end;
end;
//防止从左部出去
if(Position[ButtonNumber][0]=6) then
begin
InvalidMove:=false;
exit;
end;
InvalidMove:=true;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
SpendTime:=SpendTime+1;
Label12.Caption:='所花时间: '+IntToStr(SpendTime)+'秒';
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm2 = class(TForm)
PaintBox1: TPaintBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Timer1: TTimer;
Button1: TButton;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
private
BitmapRect:TRect;
PositiveMasks:array of TBrush; //正画刷
NegativeMasks:array of TBrush; //负画刷
SolidBlackBrush:TBrush; //黑画刷
FadeBitmap:TBitmap;
WorkSpaceBitmap:TBitmap;
Progress:integer;
BRUSHCOUNT:integer;
Procedure ProcessFadeFromBlackStep; //淡出
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.FormCreate(Sender: TObject);
var
i,j:integer;
BrushRect:TRect;
begin
BRUSHCOUNT := 8;
BrushRect.left := 0;
BrushRect.top := 0;
BrushRect.right := 8;
BrushRect.bottom := 8;
setLength(PositiveMasks,BRUSHCOUNT);
setLength(NegativeMasks,BRUSHCOUNT);
for j:=0 to BRUSHCOUNT-1 do
begin
PositiveMasks[j] := TBrush.Create;
NegativeMasks[j] := TBrush.Create;
PositiveMasks[j].Bitmap := TBitmap.Create;
NegativeMasks[j].Bitmap := TBitmap.Create;
PositiveMasks[j].Bitmap.Width := 8;
PositiveMasks[j].Bitmap.Height:= 8;
Randomize();
for i:=0 to 8*j-1 do
begin
PositiveMasks[j].Bitmap.Canvas.
Pixels[random(8),random(8)]:=clBlack;
NegativeMasks[j].Bitmap.Assign(PositiveMasks[j].Bitmap);
NegativeMasks[j].Bitmap.Canvas.CopyMode:=cmNotSrcCopy;
NegativeMasks[j].Bitmap.Canvas.Draw(0,0,PositiveMasks[j]
.Bitmap);
InvertRect(NegativeMasks[j].Bitmap.Canvas.Handle, BrushRect);
end;
SolidBlackBrush := TBrush.Create;
SolidBlackBrush.Style := bsSolid;
SolidBlackBrush.Color := clBlack;
FadeBitmap := TBitmap.Create;
WorkSpaceBitmap := TBitmap.Create;
Progress := 0;
FadeBitmap.LoadFromFile('将.bmp');
WorkSpaceBitmap.Width := FadeBitmap.Width;
WorkSpaceBitmap.Height := FadeBitmap.Height;
WorkSpaceBitmap.Canvas.Draw(0,0,FadeBitmap);
BitmapRect := Rect(0,0,FadeBitmap.Width,FadeBitmap.Height);
end;
end;
procedure TForm2.FormDestroy(Sender: TObject);
var
j:integer;
begin
for j:=0 to BRUSHCOUNT-1 do
begin
PositiveMasks[j].Bitmap.Free;
NegativeMasks[j].Bitmap.Free;
PositiveMasks[j].Free;
NegativeMasks[j].Free;
end;
SolidBlackBrush.Free;
FadeBitmap.Free;
WorkSpaceBitmap.Free;
end;
procedure TForm2.Timer1Timer(Sender: TObject);
begin
ProcessFadeFromBlackStep;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
Close;
end;
Procedure TForm2.ProcessFadeFromBlackStep;
begin
if (Progress = BRUSHCOUNT) then
begin
Progress := 0;
Timer1.Enabled := false;
WorkSpaceBitmap.Canvas.CopyMode:=cmSrcCopy;
WorkSpaceBitmap.Canvas.Draw(0,0,FadeBitmap);
end
else
begin
WorkSpaceBitmap.Canvas.Brush := NegativeMasks[Progress];
WorkSpaceBitmap.Canvas.CopyMode:=cmMergeCopy;
WorkSpaceBitmap.Canvas.Draw(0,0,FadeBitmap);
Progress:=Progress+1;
end;
PaintBox1.Canvas.Draw(0,0,WorkSpaceBitmap);
end;
procedure TForm2.PaintBox1Paint(Sender: TObject);
begin
PaintBox1.Canvas.Draw(0,0,WorkSpaceBitmap);
end;
end.