<魔域>按键精灵脚本

用了三天时间才写完,实现了通过图片识别读取坐标数值,自动寻路等简单功能。

主要的难点在于游戏中的坐标系,和电脑屏幕的坐标系存在三维旋转关系,难以换算。

  1 //全局变量:第一个数左上右下坐标
  2 Global first1x 
  3 Global first1y 
  4 Global first2x 
  5 Global first2y
  6 
  7 //全局变量:xp技点位置
  8 Global xpx 
  9 Global xpy
 10 
 11 //全局变量:读取到的数字
 12 Global x1
 13 Global x2
 14 Global x3
 15 Global x4
 16 Global y1
 17 Global y2
 18 Global y3
 19 Global y4
 20 
 21 //全局变量:读到的坐标
 22 Global nowx
 23 Global nowy
 24 
 25 //全局变量:屏幕中间点实际坐标
 26 Global actualx
 27 Global actualy
 28 
 29 
 30 //主体
 31 
 32 //初始化变量
 33 Call init()
 34 
 35 //自动按键
 36 Call startKey()
 37 
 38 //自动点击XP技
 39 If Form1.CheckBox1.Value = 1 Then 
 40     BeginThread findclick()
 41 End If
 42 
 43 //自动寻路
 44 Select Case Form1.ComboBox1.ListIndex
 45 Case 0
 46     Call go1()
 47 Case Else
 48 End Select
 49 
 50 
 51 // 子程序:按1寻路
 52 Sub go1()
 53     While 1
 54         Call headTo(592, 765)
 55         Call headTo(592, 930)
 56         Call headTo(592, 765)
 57         Call headTo(540, 765)
 58         Call headTo(540, 923)
 59         Call headTo(540, 765)
 60     Wend
 61 End Sub
 62 
 63 //子程序:初始化
 64 Sub init()
 65 
 66     first1x = 1848
 67     first1y = 21
 68     first2x = 1855
 69     first2y = 32
 70     
 71     xpx = 1784
 72     xpy = 984
 73 
 74     //获取屏幕中间点实际坐标
 75     actualx = Plugin.GetSysInfo.GetScreenResolutionX / 2
 76     actualy = Plugin.GetSysInfo.GetScreenResolutionY / 2
 77 
 78 End Sub
 79 
 80 //子程序:得到当前坐标,保存到nowx,nowy
 81 Sub get()
 82 
 83     //得出X坐标
 84     x1 = find(first1x, first1y, first2x, first2y)
 85   
 86     x2 = find(first1x + 6, first1y, first2x + 6, first2y)
 87   
 88     If x2 >= 0 Then  
 89         x3 = find(first1x + 12, first1y, first2x + 12, first2y)
 90         If x3 >= 0 Then 
 91             x4 = find(first1x + 18, first1y, first2x + 18, first2y)
 92             If x4 >= 0 Then 
 93                 nowx = x1 * 1000 + x2 * 100 + x3 * 10 + x4
 94             Else 
 95                 nowx = x1*100 + x2*10 + x3
 96             End If
 97         Else 
 98             nowx = x1*10 + x2
 99         End If
100     Else 
101         nowx = x1
102     End If
103 
104     //得出Y坐标
105     y1 = find(first1x + 29, first1y, first2x + 29, first2y)
106   
107     y2 = find(first1x + 35, first1y, first2x + 35, first2y)
108   
109     If y2 >= 0 Then  
110         y3 = find(first1x + 41, first1y, first2x + 41, first2y)
111         If y3 >= 0 Then 
112             y4 = find(first1x + 47, first1y, first2x + 47, first2y)
113             If y4 >= 0 Then 
114                 nowy = y1 * 1000 + y2 * 100 + y3 * 10 + y4
115             Else 
116                 nowy = y1*100 + y2*10 + y3
117             End If
118         Else 
119             nowy = y1*10 + y2
120         End If
121     Else 
122         nowy = y1
123     End If
124 
125 End Sub
126 
127 //函数:通过参数first1x,first1y,first2x,first2y返回检测到的数字
128 Function find(first1x, first1y, first2x, first2y)
129 
130     FindPic first1x,first1y,first2x,first2y,"Attachment:\0.bmp",1,intX,intY
131     If intX > 0 And intY > 0 Then 
132         find = 0
133         Exit Function
134     End If
135     FindPic first1x,first1y,first2x,first2y,"Attachment:\1.bmp",1,intX,intY
136     If intX > 0 And intY > 0 Then 
137         find = 1
138         Exit Function 
139     End If
140     FindPic first1x,first1y,first2x,first2y,"Attachment:\2.bmp",1,intX,intY
141     If intX > 0 And intY > 0 Then 
142         find = 2
143         Exit Function 
144     End If
145     FindPic first1x,first1y,first2x,first2y,"Attachment:\3.bmp",1,intX,intY
146     If intX > 0 And intY > 0 Then 
147         find = 3
148         Exit Function 
149     End If
150     FindPic first1x,first1y,first2x,first2y,"Attachment:\4.bmp",1,intX,intY
151     If intX > 0 And intY > 0 Then 
152         find = 4
153         Exit Function 
154     End If
155     FindPic first1x,first1y,first2x,first2y,"Attachment:\5.bmp",1,intX,intY
156     If intX > 0 And intY > 0 Then 
157         find = 5
158         Exit Function 
159     End If
160     FindPic first1x,first1y,first2x,first2y,"Attachment:\6.bmp",1,intX,intY
161     If intX > 0 And intY > 0 Then 
162         find = 6
163         Exit Function 
164     End If
165     FindPic first1x,first1y,first2x,first2y,"Attachment:\7.bmp",1,intX,intY
166     If intX > 0 And intY > 0 Then 
167         find = 7
168         Exit Function 
169     End If
170     FindPic first1x,first1y,first2x,first2y,"Attachment:\8.bmp",1,intX,intY
171     If intX > 0 And intY > 0 Then 
172         find = 8
173         Exit Function 
174     End If
175     FindPic first1x,first1y,first2x,first2y,"Attachment:\9.bmp",1,intX,intY
176     If intX > 0 And intY > 0 Then 
177         find = 9
178         Exit Function 
179     End If
180 
181     find = -1
182 
183 End Function 
184 
185 
186 //子程序:前往目标地点
187 Sub headTo(x, y)
188     BeginThread click
189     While 1
190         Call get()
191         a = nowx - x
192         b = nowy - y
193         
194         //  判断是否已经到达
195         If (a * a + b * b) < 1000 Then 
196             Exit Sub
197         End If
198         
199         //  屏幕向量      
200         screenx = (b - a) * 1.1
201         screeny = (a + b) * 0.6
202         
203         d = 300 / (Sqr(1 + (screenx * screenx) / (screeny * screeny)))
204         c = (screenx / screeny) * d
205         
206         If c * screenx < 0 Then 
207             c = -c
208         End If
209         
210         If d * screeny > 0 Then 
211             d = -d
212         End If
213         
214         c = c + actualx
215         d = d + actualy
216         
217         //  鼠标指向      
218         MoveTo c, d   
219         Delay 2000        
220     Wend
221 
222 End Sub
223 
224 // 线程:不停点击鼠标
225 Sub click()
226     clickID = GetThreadID()
227     While 1
228         LeftClick 1
229         Delay 500
230     Wend
231 End Sub
232 
233 // 子程序:开始所有键盘点击
234 Sub startkey()
235     Select Case Form1.KeyBox1.ListIndex
236     Case 1
237         ms = Form1.MsBox1.Caption
238         BeginThread presskey(49, ms)
239     Case 2
240         ms = Form1.MsBox1.Caption
241         BeginThread presskey(50, ms)
242     Case 3
243         ms = Form1.MsBox1.Caption
244         BeginThread presskey(51, ms)
245     Case 4
246         ms = Form1.MsBox1.Caption
247         BeginThread presskey(52, ms)
248     Case 5
249         ms = Form1.MsBox1.Caption
250         BeginThread presskey(53, ms)
251     Case 6
252         ms = Form1.MsBox1.Caption
253         BeginThread presskey(54, ms)
254     Case 7
255         ms = Form1.MsBox1.Caption
256         BeginThread presskey(55, ms)
257     Case 8
258         ms = Form1.MsBox1.Caption
259         BeginThread presskey(56, ms)
260     Case 9
261         ms = Form1.MsBox1.Caption
262         BeginThread presskey(57, ms)
263     Case 10
264         ms = Form1.MsBox1.Caption
265         BeginThread presskey(112, ms)
266     Case 11
267         ms = Form1.MsBox1.Caption
268         BeginThread presskey(113, ms)
269     Case 12
270         ms = Form1.MsBox1.Caption
271         BeginThread presskey(114, ms)
272     Case 13
273         ms = Form1.MsBox1.Caption
274         BeginThread presskey(115, ms)
275     Case 14
276         ms = Form1.MsBox1.Caption
277         BeginThread presskey(116, ms)
278     Case 15
279         ms = Form1.MsBox1.Caption
280         BeginThread presskey(117, ms)
281     Case 16
282         ms = Form1.MsBox1.Caption
283         BeginThread presskey(118, ms)
284     Case 17
285         ms = Form1.MsBox1.Caption
286         BeginThread presskey(119, ms)
287     Case 18
288         ms = Form1.MsBox1.Caption
289         BeginThread presskey(120, ms)
290     Case Else
291     End Select
292     
293     Select Case Form1.KeyBox2.ListIndex
294     Case 1
295         ms = Form1.MsBox2.Caption
296         BeginThread presskey(49, ms)
297     Case 2
298         ms = Form1.MsBox2.Caption
299         BeginThread presskey(50, ms)
300     Case 3
301         ms = Form1.MsBox2.Caption
302         BeginThread presskey(51, ms)
303     Case 4
304         ms = Form1.MsBox2.Caption
305         BeginThread presskey(52, ms)
306     Case 5
307         ms = Form1.MsBox2.Caption
308         BeginThread presskey(53, ms)
309     Case 6
310         ms = Form1.MsBox2.Caption
311         BeginThread presskey(54, ms)
312     Case 7
313         ms = Form1.MsBox2.Caption
314         BeginThread presskey(55, ms)
315     Case 8
316         ms = Form1.MsBox2.Caption
317         BeginThread presskey(56, ms)
318     Case 9
319         ms = Form1.MsBox2.Caption
320         BeginThread presskey(57, ms)
321     Case 10
322         ms = Form1.MsBox2.Caption
323         BeginThread presskey(112, ms)
324     Case 11
325         ms = Form1.MsBox2.Caption
326         BeginThread presskey(113, ms)
327     Case 12
328         ms = Form1.MsBox2.Caption
329         BeginThread presskey(114, ms)
330     Case 13
331         ms = Form1.MsBox2.Caption
332         BeginThread presskey(115, ms)
333     Case 14
334         ms = Form1.MsBox2.Caption
335         BeginThread presskey(116, ms)
336     Case 15
337         ms = Form1.MsBox2.Caption
338         BeginThread presskey(117, ms)
339     Case 16
340         ms = Form1.MsBox2.Caption
341         BeginThread presskey(118, ms)
342     Case 17
343         ms = Form1.MsBox2.Caption
344         BeginThread presskey(119, ms)
345     Case 18
346         ms = Form1.MsBox2.Caption
347         BeginThread presskey(120, ms)
348     Case Else
349     End Select
350     
351     Select Case Form1.KeyBox3.ListIndex
352     Case 1
353         ms = Form1.MsBox3.Caption
354         BeginThread presskey(49, ms)
355     Case 2
356         ms = Form1.MsBox3.Caption
357         BeginThread presskey(50, ms)
358     Case 3
359         ms = Form1.MsBox3.Caption
360         BeginThread presskey(51, ms)
361     Case 4
362         ms = Form1.MsBox3.Caption
363         BeginThread presskey(52, ms)
364     Case 5
365         ms = Form1.MsBox3.Caption
366         BeginThread presskey(53, ms)
367     Case 6
368         ms = Form1.MsBox3.Caption
369         BeginThread presskey(54, ms)
370     Case 7
371         ms = Form1.MsBox3.Caption
372         BeginThread presskey(55, ms)
373     Case 8
374         ms = Form1.MsBox3.Caption
375         BeginThread presskey(56, ms)
376     Case 9
377         ms = Form1.MsBox3.Caption
378         BeginThread presskey(57, ms)
379     Case 10
380         ms = Form1.MsBox3.Caption
381         BeginThread presskey(112, ms)
382     Case 11
383         ms = Form1.MsBox3.Caption
384         BeginThread presskey(113, ms)
385     Case 12
386         ms = Form1.MsBox3.Caption
387         BeginThread presskey(114, ms)
388     Case 13
389         ms = Form1.MsBox3.Caption
390         BeginThread presskey(115, ms)
391     Case 14
392         ms = Form1.MsBox3.Caption
393         BeginThread presskey(116, ms)
394     Case 15
395         ms = Form1.MsBox3.Caption
396         BeginThread presskey(117, ms)
397     Case 16
398         ms = Form1.MsBox3.Caption
399         BeginThread presskey(118, ms)
400     Case 17
401         ms = Form1.MsBox3.Caption
402         BeginThread presskey(119, ms)
403     Case 18
404         ms = Form1.MsBox3.Caption
405         BeginThread presskey(120, ms)
406     Case Else
407     End Select
408     
409     Select Case Form1.KeyBox4.ListIndex
410     Case 1
411         ms = Form1.MsBox4.Caption
412         BeginThread presskey(49, ms)
413     Case 2
414         ms = Form1.MsBox4.Caption
415         BeginThread presskey(50, ms)
416     Case 3
417         ms = Form1.MsBox4.Caption
418         BeginThread presskey(51, ms)
419     Case 4
420         ms = Form1.MsBox4.Caption
421         BeginThread presskey(52, ms)
422     Case 5
423         ms = Form1.MsBox4.Caption
424         BeginThread presskey(53, ms)
425     Case 6
426         ms = Form1.MsBox4.Caption
427         BeginThread presskey(54, ms)
428     Case 7
429         ms = Form1.MsBox4.Caption
430         BeginThread presskey(55, ms)
431     Case 8
432         ms = Form1.MsBox4.Caption
433         BeginThread presskey(56, ms)
434     Case 9
435         ms = Form1.MsBox4.Caption
436         BeginThread presskey(57, ms)
437     Case 10
438         ms = Form1.MsBox4.Caption
439         BeginThread presskey(112, ms)
440     Case 11
441         ms = Form1.MsBox4.Caption
442         BeginThread presskey(113, ms)
443     Case 12
444         ms = Form1.MsBox4.Caption
445         BeginThread presskey(114, ms)
446     Case 13
447         ms = Form1.MsBox4.Caption
448         BeginThread presskey(115, ms)
449     Case 14
450         ms = Form1.MsBox4.Caption
451         BeginThread presskey(116, ms)
452     Case 15
453         ms = Form1.MsBox4.Caption
454         BeginThread presskey(117, ms)
455     Case 16
456         ms = Form1.MsBox4.Caption
457         BeginThread presskey(118, ms)
458     Case 17
459         ms = Form1.MsBox4.Caption
460         BeginThread presskey(119, ms)
461     Case 18
462         ms = Form1.MsBox4.Caption
463         BeginThread presskey(120, ms)
464     Case Else
465     End Select
466 End Sub
467 
468 //线程:每隔ms按num键
469 Sub presskey(num,mss)
470     While 1
471         KeyPress num, 1
472         Delay ms
473     Wend
474 End Sub
475 
476 // 查找和点击XP技能
477 Sub findclick()
478     While 1
479         IfColor xpx, xpy, "CF71A9", 2 Then
480             SaveMousePos 
481             MoveTo 1784, 984
482             LeftClick 1
483             RestoreMousePos
484         Else
485         End If
486         Delay 15000
487     Wend
488 End Sub

 

posted @ 2015-01-20 19:01  杨乐达  阅读(5911)  评论(0编辑  收藏  举报