Autohotkey ImageSearch未检测图像
我正在尝试与Autohotkey进行机器人,以玩一个名为 btd6 对我来说。
问题在于我需要检测到胜利屏幕何时出现,然后单击按钮。因此,我尝试使用images搜索
搜索中的按钮,而
循环,但循环永无止境。
我制作了测试脚本波纹管,它可以正常工作:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
^b::
ImageSearch, ix, iy, 0, 0, A_ScreenWidth, A_ScreenHeight, next.png
Click, %ix% %iy%
Return
Escape::
ExitApp
Return
但是当我将其放入主要的机器人代码中时,它不起作用,我认为这是因为循环永远不会退出:
(滚动到代码的底部)
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode Pixel
^b::
While true {
Click, 1223 1553
Sleep, 1000
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 801 1097
Sleep, 1000
Click, 882 668
Sleep, 1000
Click, 1994 720
Sleep, 5000
Click, 1444 1265,
Sleep, 1000
Send {k down}{k up}
MouseMove, 2381, 1100, 20
Sleep, 100
Click
Sleep, 1000
Click
Sleep, 1000
Send {, down}{, up}
Sleep, 100
Send {, down}{, up}
Sleep, 100
Send {/ down}{/ up}
Sleep, 100
Send {/ down}{/ up}
Sleep, 1000
Click, 1453 816
Sleep, 1000
Send {z down}{z up}
MouseMove, 2281, 971, 20
Sleep, 100
Click
Sleep, 1000
Click
Sleep, 1000
Send {. down}{. up}
Sleep 100
Send {. down}{. up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Click, 1453 816
Sleep, 1000
Send {f down}{f up}
MouseMove, 2397, 967, 20,
Sleep, 100
Click
Sleep, 1000
Click
Sleep, 1000
Send {, down}{, up}
Sleep 100
Send {, down}{, up}
Sleep 100
Send {, down}{, up}
Sleep 100
Send {, down}{, up}
Sleep 100
Send {. down}{. up}
Sleep 100
Send {. down}{. up}
Sleep 100
Click, 1453 816
Sleep, 1000
Send {Space down}{Space up}
Sleep, 1000
While true {
ImageSearch, foundx, foundy, 0, 0, A_ScreenWidth, A_ScreenHeight, next.png
if (foundx or foundy) {
Break
} else {
Sleep, 2000
}
}
Click, %foundx% %foundy%
foundx := 0
foundy := 0
Sleep, 1000
Click, 1002 1419
Sleep, 5000
}
Return
Escape::
ExitApp
Return
I'm trying to make a bot with AutoHotkey to play a game called BTD6 for me.
The problem is that I need to detect when the victory screen appears, and click a button. So I tried using ImageSearch
to search for the button inside a While
loop, but the loop never ends.
I made the test script bellow and it works fine:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
^b::
ImageSearch, ix, iy, 0, 0, A_ScreenWidth, A_ScreenHeight, next.png
Click, %ix% %iy%
Return
Escape::
ExitApp
Return
But when I put it in the main bot code, it does not work, I think it's because the loop never exits:
(scroll to the bottom of the code)
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode Pixel
^b::
While true {
Click, 1223 1553
Sleep, 1000
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 2581 693
Sleep, 100
Click, 801 1097
Sleep, 1000
Click, 882 668
Sleep, 1000
Click, 1994 720
Sleep, 5000
Click, 1444 1265,
Sleep, 1000
Send {k down}{k up}
MouseMove, 2381, 1100, 20
Sleep, 100
Click
Sleep, 1000
Click
Sleep, 1000
Send {, down}{, up}
Sleep, 100
Send {, down}{, up}
Sleep, 100
Send {/ down}{/ up}
Sleep, 100
Send {/ down}{/ up}
Sleep, 1000
Click, 1453 816
Sleep, 1000
Send {z down}{z up}
MouseMove, 2281, 971, 20
Sleep, 100
Click
Sleep, 1000
Click
Sleep, 1000
Send {. down}{. up}
Sleep 100
Send {. down}{. up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Send {/ down}{/ up}
Sleep 100
Click, 1453 816
Sleep, 1000
Send {f down}{f up}
MouseMove, 2397, 967, 20,
Sleep, 100
Click
Sleep, 1000
Click
Sleep, 1000
Send {, down}{, up}
Sleep 100
Send {, down}{, up}
Sleep 100
Send {, down}{, up}
Sleep 100
Send {, down}{, up}
Sleep 100
Send {. down}{. up}
Sleep 100
Send {. down}{. up}
Sleep 100
Click, 1453 816
Sleep, 1000
Send {Space down}{Space up}
Sleep, 1000
While true {
ImageSearch, foundx, foundy, 0, 0, A_ScreenWidth, A_ScreenHeight, next.png
if (foundx or foundy) {
Break
} else {
Sleep, 2000
}
}
Click, %foundx% %foundy%
foundx := 0
foundy := 0
Sleep, 1000
Click, 1002 1419
Sleep, 5000
}
Return
Escape::
ExitApp
Return
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我将您的代码与Settimer一起使用,而不是尝试使用连续循环。 应该创建与连续循环相同的效果。
尽管我自己从不拼写游戏,但有更好的方法可以做到这一点,但是当您了解更多语言时,我会让您弄清楚这一点。
OK so I used your code with SetTimer instead of trying to use a continuous loop. It should create the same effect as a continuous loop.
Although I never script games myself, there are better ways to do this, but I'll let you figure that out as you learn more of the language.