⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 这是一个基于java编写的叫做华容道的游戏
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls;

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;
    Label12: TLabel;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
     SpendTime:Integer;
     Position:array[0..9,0..1] of integer; //用于保存或设置每个Images组件的Left和Top属性
     WidthHeight:array[0..9,0..1] of integer; //用于保存每个Images组件的宽度和高度
     Images:array[0..9] of TImage;//TImage数组
     PrepareMove:Boolean;  //用于显示是否准备移动
     Moveward:TDragWord;  //移动方向
     BeginPosition:TPoint; //用户拖动Images组件时的起始位置
     Procedure ImagesMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
     Procedure ImagesMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
     Function InvalidMove(X:integer;Y:Integer;ButtonNumber:integer):Boolean;//判断是否允许用户的拖动

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
  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
    Images[i]:=TImage.Create(Panel1); //动态创建数组
    Images[i].Parent:=Panel1;
    Images[i].Left:=Position[i][0];
    Images[i].Top:=Position[i][1];
    Images[i].Width:=WidthHeight[i][0];
    Images[i].Height:=WidthHeight[i][1];
    Images[i].Tag:=i;
    Images[i].Stretch:=True;
    Images[i].Cursor:=crHandPoint;
    Images[i].OnMouseDown:=ImagesMouseDown;//设置事件相应句柄
    Images[i].OnMouseUp:=ImagesMouseUp;
  end;

 Images[0].Picture.LoadFromFile('general1.bmp');
 Images[1].Picture.LoadFromFile('caocao.bmp');
 Images[2].Picture.LoadFromFile('general2.bmp');
 Images[3].Picture.LoadFromFile('general3.bmp');
 Images[4].Picture.LoadFromFile('guanyu.bmp');
 Images[5].Picture.LoadFromFile('general4.bmp');
 Images[6].Picture.LoadFromFile('soldier1.bmp');
 Images[7].Picture.LoadFromFile('soldier2.bmp');
 Images[8].Picture.LoadFromFile('soldier3.bmp');
 Images[9].Picture.LoadFromFile('soldier4.bmp');


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[i][1]) and (tempY<=Position[i][1]+ WidthHeight[i][1])
          and
          (tempX>=Position[i][0]) and (tempX<=Position[i][0]+ WidthHeight[i][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[i][1]) and (tempY<=Position[i][1]+ WidthHeight[i][1])
          and
          (tempX>=Position[i][0]) and (tempX<=Position[i][0]+ WidthHeight[i][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[i][1]) and (tempY<=Position[i][1]+ WidthHeight[i][1])
          and
          (tempX>=Position[i][0]) and (tempX<=Position[i][0]+ WidthHeight[i][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[i][1]) and (tempY<=Position[i][1]+ WidthHeight[i][1])
          and
          (tempX>=Position[i][0]) and (tempX<=Position[i][0]+ WidthHeight[i][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.Button1Click(Sender: TObject);
var
i:integer;
begin
 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
    Images[i].Left:=Position[i][0]; //复原各个按钮的位置
    Images[i].Top:=Position[i][1];
 end;

 Timer1.Enabled:=true;
 SpendTime:=0;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   Close;
end;

Procedure TForm1.ImagesMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
begin
   PrepareMove:=TRUE;
   BeginPosition.x:=X;
   BeginPosition.y:=Y;
end;

Procedure TForm1.ImagesMouseUp(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(TImage(Sender).Left + TImage(Sender).Width div 2,
                   TImage(Sender).Top + TImage(Sender).Height div 2,
                   TImage(Sender).Tag)) then
    begin
       if(Moveward=Drag_Upward) then //如果向上移动
       begin
          Images[TImage(Sender).Tag].Top:=Images[TImage(Sender).Tag].Top-71;
          Position[TImage(Sender).Tag][1]:=Position[TImage(Sender).Tag][1]-71;
       end
       else if(Moveward=Drag_Downward) then//如果向下移动
       begin
          Images[TImage(Sender).Tag].Top:=Images[TImage(Sender).Tag].Top+71;
          Position[TImage(Sender).Tag][1]:=Position[TImage(Sender).Tag][1]+71;
       end
       else if(Moveward=Drag_Leftward) then //如果向左移动
       begin
          Images[TImage(Sender).Tag].Left:=Images[TImage(Sender).Tag].Left-74;
          Position[TImage(Sender).Tag][0]:=Position[TImage(Sender).Tag][0]-74;
       end
       else  //如果向右移动
       begin
          Images[TImage(Sender).Tag].Left:=Images[TImage(Sender).Tag].Left+74;
          Position[TImage(Sender).Tag][0]:=Position[TImage(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.Timer1Timer(Sender: TObject);
begin
   SpendTime:=SpendTime+1;
   Label12.Caption:='所花时间: '+IntToStr(SpendTime)+'秒';
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -