这些日子,QQ游戏外挂是风光了一阵.俄罗斯方块,连连看,对对碰这些游戏的外挂层出不穷。其实这一类外挂的原理大体都是一样的。下面我就以QQ游戏对对碰外挂作为例子阐述一下QQ外挂的制作原理。
观察QQ游戏对对碰的游戏界面及游戏规则,发现玩家是在固定的一个游戏区内寻找复合要求的方块然后点击两次鼠标消去方块从而达到得分的目的。因此,我们可以通过模拟人的观察,和鼠标点击来实现外挂自动消除方块,完成全局。
取得可消方块(模拟人观察):要判断哪个方块可以消,我们可以通过获取方块某点的颜色来判断方块的类型,然后建立方块矩阵,然后在矩阵中选择出适合消去的方块。关于取颜色,我们用到几个API函数 GetPixel、GetDC、ReleaseDC。(详细的介绍请参考MSDN或各种API相关资料)
模拟鼠标击:在此我们使用 mouse_event 来模拟鼠标的移动,点击,弹起。个人认为用postmessage是更好的,在此使用mouse_event只是为了阐述简单。
下面是VB实现代码(带详细注释):
'模块中
'*************模块:modMain******************
'作者:Cyril
'Email:[email protected]
'Web: http://www.sguca.com/other
'书写日期:2004.10.23
'编辑日期:2002.10.23
'转载请保留此信息
'版权所有(a)Cyril 405 工作室
'********************************************
Option Explicit
'API声明
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
'API类型定义
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'方块类型定义
Public Enum BOX_TYPE
Ox = 0
Dog = 1
Panda = 2
Chicken = 3
Cat = 4
Frog = 5
Monkey = 6
End Enum
'自定义方块数据类型x,y位方块坐标,type为方块类型.
'在Easy对对碰1.5版中还加如了一些其他属性,例如是否带道具属性.
Public Type BOX
x As Integer
y As Integer
type As BOX_TYPE
End Type
'Api常量
'鼠标事件常量
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Public Const HWND_TOPMOST = -1
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOMOVE = &H2
'自定义常量
'游戏区左上角坐标
Const GAME_LEFT As Integer = 176
Const GAME_TOP As Integer = 102
'每个方块的长宽
Const BOX_WIDTH As Integer = 48
Const BOX_HEIGHT As Integer = 48
'游戏窗口句柄
Public g_WindowHwnd As Long
'方块矩阵 (8*8)
Public boxs(7, 7) As BOX
'**********过程名:getBoxs******************
'作者:Cyril
'书写日期:2004.10.23
'编辑日期:2002.10.23
'目的:获取当前场景 , 建立方块矩阵
'方法:getBoxs
'应用于:MainMod模块
'********************************************
Public Function getBoxs()
Dim i As Integer '矩阵行
Dim j As Integer '矩阵列
Dim color1 As Long '颜色 (22,22)处
Dim color2 As Long '颜色 (22,17)处
For i = 0 To 7
For j = 0 To 7
With boxs(i, j)
.x = GAME_LEFT + 22 + BOX_WIDTH * j
.y = GAME_TOP + 22 + BOX_HEIGHT * i
'取每个方块坐标(22,22)和(22,17)位置的颜色
color1 = getColor(.x, .y)
color2 = getColor(.x, .y - 5)
'用两点颜色确定一个方块类型.
If color1 = 16777215 And color2 = 16777215 Then .type = Panda
If color1 = 2097151 And color2 = 1353909 Then .type = Chicken
If color1 = 4473924 And color2 = 14209230 Then .type = Dog
If color1 = 13828048 And color2 = 3862322 Then .type = Frog
If color1 = 8623264 And color2 = 5805536 Then .type = Monkey
If color1 = 10921638 And color2 = 9408399 Then .type = Cat
If color1 = 15398649 And color2 = 1655140 Then .type = Ox
End With
Next j
Next i
End Function
'为了方便理解,这里用了一种比较简单的算法 -- 穷举法.(这也是Easy对对碰最初版本的算法).
'其主要思想是列举16种消除方块的可能。一旦有匹配的情况出现,则马上执行鼠标点击动作.
'如果你要使你的外挂更强大,就必须采更优秀的算法.
'**********过程名:killBox******************
'作者:Cyril
'书写日期:2004.10.23
'编辑日期:2002.10.23
'目的:消去一个方块
'方法:killBox
'说明:无
'返回值:无
'应用于:MainMod模块
'********************************************
Public Function killBox()
Dim i As Integer
Dim j As Integer
getBoxs
' :
' | 情况
For i = 0 To 4
For j = 0 To 7
If boxs(i, j).type = boxs(i + 2, j).type And boxs(i, j).type = boxs(i + 3, j).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i + 1, j).x, boxs(i + 1, j).y
Exit Function
End If
Next j
Next i
'_
' | 情况
For i = 0 To 5
For j = 0 To 6
If boxs(i, j).type = boxs(i + 2, j + 1).type And boxs(i, j).type = boxs(i + 1, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j + 1).x, boxs(i, j + 1).y
Exit Function
End If
Next j
Next i
' _
'| 情况
For i = 0 To 5
For j = 1 To 7
If boxs(i, j).type = boxs(i + 2, j - 1).type And boxs(i, j).type = boxs(i + 1, j - 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j - 1).x, boxs(i, j - 1).y
Exit Function
End If
Next j
Next i
'|
': 情况
For i = 3 To 7
For j = 0 To 7
If boxs(i, j).type = boxs(i - 2, j).type And boxs(i, j).type = boxs(i - 3, j).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i - 1, j).x, boxs(i - 1, j).y
Exit Function
End If
Next j
Next i
'_ | 情况
For i = 2 To 7
For j = 0 To 6
If boxs(i, j).type = boxs(i - 1, j + 1).type And boxs(i, j).type = boxs(i - 2, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j + 1).x, boxs(i, j + 1).y
Exit Function
End If
Next j
Next i
'| _ 情况
For i = 2 To 7
For j = 1 To 7
If boxs(i, j).type = boxs(i - 1, j - 1).type And boxs(i, j).type = boxs(i - 2, j - 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j - 1).x, boxs(i, j - 1).y
Exit Function
End If
Next j
Next i
'_-- 情况
For i = 1 To 7
For j = 0 To 5
If boxs(i, j).type = boxs(i - 1, j + 2).type And boxs(i, j).type = boxs(i - 1, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i - 1, j).x, boxs(i - 1, j).y
Exit Function
End If
Next j
Next i
'-_ _ 情况
For i = 0 To 6
For j = 0 To 5
If boxs(i, j).type = boxs(i + 1, j + 2).type And boxs(i, j).type = boxs(i + 1, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i + 1, j).x, boxs(i + 1, j).y
Exit Function
End If
Next j
Next i
' -_- 情况
For i = 1 To 7
For j = 1 To 6
If boxs(i, j).type = boxs(i - 1, j - 1).type And boxs(i, j).type = boxs(i - 1, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i - 1, j).x, boxs(i - 1, j).y
Exit Function
End If
Next j
Next i
' _-_ 情况
For i = 0 To 6
For j = 1 To 6
If boxs(i, j).type = boxs(i + 1, j - 1).type And boxs(i, j).type = boxs(i + 1, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i + 1, j).x, boxs(i + 1, j).y
Exit Function
End If
Next j
Next i
'|< 情况
For i = 1 To 6
For j = 1 To 7
If boxs(i, j).type = boxs(i + 1, j - 1).type And boxs(i, j).type = boxs(i - 1, j - 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j - 1).x, boxs(i, j - 1).y
Exit Function
End If
Next j
Next i
'>| 情况
For i = 1 To 6
For j = 0 To 6
If boxs(i, j).type = boxs(i + 1, j + 1).type And boxs(i, j).type = boxs(i - 1, j + 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j + 1).x, boxs(i, j + 1).y
Exit Function
End If
Next j
Next i
'--_ 情况
For i = 1 To 7
For j = 2 To 7
If boxs(i, j).type = boxs(i - 1, j - 2).type And boxs(i, j).type = boxs(i - 1, j - 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i - 1, j).x, boxs(i - 1, j).y
Exit Function
End If
Next j
Next i
'-- - 情况
For i = 0 To 7
For j = 3 To 7
If boxs(i, j).type = boxs(i, j - 2).type And boxs(i, j).type = boxs(i, j - 3).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j - 1).x, boxs(i, j - 1).y
Exit Function
End If
Next j
Next i
'_ _- 情况
For i = 0 To 6
For j = 2 To 7
If boxs(i, j).type = boxs(i + 1, j - 2).type And boxs(i, j).type = boxs(i + 1, j - 1).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i + 1, j).x, boxs(i + 1, j).y
Exit Function
End If
Next j
Next i
'- -- 情况
For i = 0 To 7
For j = 0 To 4
If boxs(i, j).type = boxs(i, j + 2).type And boxs(i, j).type = boxs(i, j + 3).type Then
mouseClick boxs(i, j).x, boxs(i, j).y
mouseClick boxs(i, j + 1).x, boxs(i, j + 1).y
Exit Function
End If
Next j
Next i
End Function
'这里的鼠标模拟用了鼠标事件.你也可以用sendmessage来实现.
'**********过程名:mouseClick******************
'作者:Cyril
'书写日期:2004.10.23
'编辑日期:2002.10.23
'目的:模拟鼠标单击
'方法:mouseClick(ByVal x As Long, ByVal y As Long)
'说明:x -- 方块相对游戏窗口的X; y -- 方块相对游戏窗口的Y;
'返回值:无
'应用于:MainMod模块
'********************************************
Public Sub mouseClick(ByVal x As Long, ByVal y As Long)
Dim po As POINTAPI 'po点击前鼠标位置
Dim kX As Long '方块的绝对X坐标
Dim kY As Long '方块的绝对Y坐标
Dim winRECT As RECT '游戏窗口的RECT
'获得游戏窗口的RECT
GetWindowRect g_WindowHwnd, winRECT
'绝对坐标 = 游戏窗口左上角坐标 + 游戏中的相对坐标
kX = winRECT.Left + x
kY = winRECT.Top + y
'获得鼠标点击前位置
GetCursorPos po
'模拟鼠标移动
mouse_event MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE, kX * 65535 / 1024, kY * 65535 / 768, 0&, 0&
'模拟鼠标按下弹起
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP Or MOUSEEVENTF_ABSOLUTE, 0, 0, 0&, 0&
'点击后返回原先位置
SetCursorPos po.x, po.y
End Sub
'**********过程名:getColor******************
'作者:Cyril
'书写日期:2004.10.23
'编辑日期:2002.10.23
'目的:取游戏中某点颜色
'方法:getColor(ByVal newX As Long, ByVal newY As Long) As Long
'说明:newX -- 要取颜色的点的X; newY -- 要取颜色的点的Y;(X,Y均相对于游戏窗口)
'返回值:Long
'应用于:MainMod模块
'********************************************
Public Function getColor(ByVal newX As Long, ByVal newY As Long) As Long
Dim windowDC As Long
'获取游戏场景
windowDC = GetDC(g_WindowHwnd)
'取场景中 (newX,newY)坐标的颜色
getColor = GetPixel(windowDC, newX, newY)
'释放场景(不要漏掉此项)
ReleaseDC g_WindowHwnd, windowDC
End Function
'====在窗体中========在窗体中========在窗体中===
Option Explicit
'##################################################################
'## 过程名称:Command5_Click
'## 参数: 无
'##################################################################
Private Sub Command5_Click()
Command6.Enabled = Not Command6.Enabled
Command5.Enabled = Not Command5.Enabled
Timer1.Enabled = True
End Sub
'##################################################################
'## 过程名称:Command6_Click
'## 参数: 无
'##################################################################
Private Sub Command6_Click()
Timer1.Enabled = False
Command6.Enabled = Not Command6.Enabled
Command5.Enabled = Not Command5.Enabled
End Sub
'##################################################################
'## 过程名称:Form_Load
'## 参数: 无
'##################################################################
Private Sub Form_Load()
g_WindowHwnd = FindWindow(vbNullString, "对对碰")
'窗口总在前
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub
'##################################################################
'## 过程名称:Label2_Click
'## 参数: 无
'##################################################################
Private Sub Label2_Click()
End Sub
'##################################################################
'## 过程名称:Timer1_Timer
'## 参数: 无
'##################################################################
Private Sub Timer1_Timer()
killBox
End Sub
以上代码相对简单,只实现了对对碰外挂的基本功能。其他的一些功能(如自动使用道具,自动开始)还需要读者自己去扩充。不过只要你理解了取颜色,模拟鼠标动作的话,这些已经不是难题了。:)
另:感谢CONAN的帮助,感谢卢培培的帮助。
本例源码下载:http://www.sguca.com/other/source.rar
作者对对碰外挂作品Easy对对碰 1.5下载:http://www.sguca.com/other/EasyTwins.rar