编程实现连连看机器人外挂

来源:互联网 发布:大学智慧树网络课程 编辑:程序博客网 时间:2024/04/29 20:08

在《黑客防线》发表过的一篇,没什么含量,  
     连连看是一款非常不错的休闲游戏,相信大家都玩过。这里的连连看是单机版V3.0,网上曾经有过此类外挂,不过只能通过修改内存游戏数值提高生命值和提示数量。显然在自动化日益发达的今天,它不能满足我们完全自动化的要求。于是乎,机器人玩游戏提上日程。编程工具:Delphi7.0 ;连连看单机版V3.0
   

unit Unit1;

interface

…………

{省略部分,详细见源码}    

implementation

var

  hWinMain,pHandle:THandle;

  PID:DWORD;

{$R *.dfm}

                                                                               

{过程名:PressF5 功能:模拟快捷键F5}

procedure TForm1.PressF5;

begin

  {F5快捷键}

   PostMessage(hWinMain,WM_KEYDOWN,VK_F5,0);// hWinMain游戏主窗体句柄

   PostMessage(hWinMain,WM_KEYUP,VK_F5,0);

end;

 

{过程名:cheate  功能:修改游戏内存数值}

procedure TForm1.cheate;

var

  pINT,pLife:Integer;

  s_cb:DWord;

begin

  pINT:=100;

  Plife:=600;

  GetWindowThreadProcessId(hWinMain,@PID);//获取主窗体线程ID

  pHandle:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,PID);//打开进程

  IF pHandle <> 0 then

  begin

    if chk1.Checked then WriteProcessMemory(pHandle,Pointer($12F4D4),@plife,1,s_cb);//写内存

    if chk2.Checked then WriteProcessMemory(pHandle,Pointer($12FEBC),@pINT,1,s_cb);

  end

  else

  begin

    Application.MessageBox('打开进程失败','错误',0);

    tmr1.Enabled :=False;

  end;

end;

 

{函数名:GetTopXY  功能:获取主窗体坐标}

function TForm1.GetTopXY:TRect;

var

  cpRect:TRect;

begin

  if hWinMain <> 0 then

  begin

    GetWindowRect(hWinMain,cpRect);

    Result := cpRect;

  end;

end;

 

{函数名:getColor  功能:获取坐标点颜色值}

function TForm1.getColor(x,y:Integer):TColor;

var

  hDC: THandle;

  YourColor: TColor;

begin

  hDC := GetDC(0);

  with   TCanvas.Create   do

  try

    Handle   :=   hDC;

    YourColor:= Pixels[x,y];

    Result:=YourColor;

  finally

    Free;

    ReleaseDC(0,hDC);

  end;

end;

 

{过程名:AutoRunGame,功能:扫描游戏区域 }

procedure TForm1.AutoRunGame;

var

  X,Y,m,n:Integer;

  TopX,TopY:Integer;

  rec:TRect;

begin

  rec := GetTopXY;

  TopX := rec.Left;

  TopY := rec.Top;

 

  PressF5;// 按下快捷键F5

 

  for n:=1 to 7 do

  begin

    y:= TopY+163+45+50*(n-1);

    for m:= 1 to 12 do

    begin

      x := TopX+154+30+40*(m-1);

      if getColor(x,y) = 8421376 then //截取坐标点颜色值

      begin

        setcursorpos(x,y);

        mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//模拟鼠标按键

      end;

    end;

  end;

end;

 

{启动按钮单击事件过程}

procedure TForm1.btn1Click(Sender: TObject);

begin

   hWinMain:= FindWindow('#32770','连连看');//查找主窗体句柄

   if (chk1.Checked or chk2.Checked) then

   begin

     if (hWinMain<>0 )then

       tmr1.Enabled := True

     else

       Application.MessageBox('游戏未启动','提示',0);

   end

   else

     Application.MessageBox('请至少选择一个功能','提示',0);

end;

 

procedure TForm1.tmr1Timer(Sender: TObject);

begin

  cheate;//修改游戏内存数值过程

end;

 

procedure TForm1.btn2Click(Sender: TObject);

begin

  tmr1.Enabled :=False;

end;

 

procedure TForm1.btn3Click(Sender: TObject);

begin

  btn1Click(self);

  SetForegroundWindow(hWinMain);

  if FindWindow('#32770','连连看')=0 then Exit;//未发现游戏窗体则退出

  tmr2.Enabled:=True;

end;

{ tmr2Timer ,自动玩游戏过程}

procedure TForm1.tmr2Timer(Sender: TObject);

begin

  AutoRunGame;

  if FindWindow('#32770','成功')<>0 then tmr2.Enabled:=False;//过关停止

end;

 

end.

 

原创粉丝点击