python如何用ctypes模拟点击

888次阅读
没有评论

python如何用ctypes模拟点击

 

在小编学习python中的模拟点击之前,我们想要对某一项操作进行自动指令的重复,可以选择大家熟知的按键ipipgo。那么对比python的模拟点击,小编还是觉得python中使用更加方便。这样说不能让有些小伙伴信服,下面小编就以一个以小游戏为例,在我们写完ctypes模拟点击后用python运行,看看游戏体验效果。

按键ipipgo提供的窗口api性能并不算的上太好。但是将整个逻辑搬到python上,并提供了自己所写的api后,速度有了很大的提升。

直接用python调用,获取特定点位置上的颜色,非白色就发送点击指令。然后循环等待下一个黑色块的到来。同时设定定时时间,若长时间依旧是这个颜色,证明游戏结束,直接退出。代码如下:

WindowFunction = ctypes.windll.LoadLibrary("E:\\Python Hack\\DLL\\ScreenFunction.dll")
    DllGetPixel = WindowFunction.GetWindowPixel
    DllGetPixel.argtypes=[ctypes.wintypes.HWND,ctypes.wintypes.c_int,ctypes.wintypes.c_int]
    DllGetPixel.restypes=[ctypes.wintypes.c_uint32]
    DllGetMultiPixel = WindowFunction.GetWindowMultiPixel
    DllGetMultiPixel.argtypes=[ctypes.wintypes.HWND,ctypes.wintypes.c_void_p,ctypes.wintypes.c_void_p]
    DllGetMultiPixel.restypes=[ctypes.wintypes.c_int]
cMulti = (ctypes.wintypes.c_int * 17)(Pos0.x,Pos0.y,Pos1.x,Pos1.y,Pos2.x,Pos2.y,Pos3.x,Pos3.y,
                                         Pos0.x,Pos0.y-5,Pos1.x,Pos1.y-5,Pos2.x,Pos2.y-5,Pos3.x,Pos3.y-5,
                                         0)
    dwLen = DllGetMultiPixel(wHWND,byref(cMulti),None)
    RGB = (ctypes.wintypes.DWORD * dwLen)()
    quit = False
    while not quit:
        DllGetMultiPixel(wHWND,byref(cMulti),byref(RGB))        
        flag = 0
        if not RGB[0] == 0xfff5f5f5 or not RGB[4] == 0xfff5f5f5:
            EmuCursorClick(rect.left+Pos0.x,rect.top+Pos0.y)
            flag = 1
        elif not RGB[1] == 0xfff5f5f5 or not RGB[5] == 0xfff5f5f5:
            EmuCursorClick(rect.left+Pos1.x,rect.top+Pos1.y)
            flag = 2
        elif not RGB[2] == 0xfff5f5f5 or not RGB[6] == 0xfff5f5f5:
            EmuCursorClick(rect.left+Pos2.x,rect.top+Pos2.y)
            flag = 3
        elif not RGB[3] == 0xfff5f5f5 or not RGB[7] == 0xfff5f5f5:
            EmuCursorClick(rect.left+Pos3.x,rect.top+Pos3.y)
            flag = 4
        cot = 0
        if flag == 0:
            quit=True
        elif flag == 1:
            RGB0 = DllGetPixel(wHWND,Pos0.x,Pos0.y) & 0xffffffff
            while not RGB0 == 0xfff5f5f5:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break                
                RGB0 = DllGetPixel(wHWND,Pos0.x,Pos0.y) & 0xffffffff
        elif flag == 2:        
            RGB1 = DllGetPixel(wHWND,Pos1.x,Pos1.y) & 0xffffffff
            while not RGB1 == 0xfff5f5f5:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break
                RGB1 = DllGetPixel(wHWND,Pos1.x,Pos1.y) & 0xffffffff
        elif flag == 3:
            RGB2 = DllGetPixel(wHWND,Pos2.x,Pos2.y) & 0xffffffff
            while not RGB2 == 0xfff5f5f5:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break                
                RGB2 = DllGetPixel(wHWND,Pos2.x,Pos2.y) & 0xffffffff
        elif flag == 4:
            RGB3 = DllGetPixel(wHWND,Pos3.x,Pos3.y) & 0xffffffff
            while not RGB3 == 0xfff5f5f5:
                time.sleep(0.05)
                cot += 1
                if cot > 20:
                    quit=True
                    break                
                RGB3 = DllGetPixel(wHWND,Pos3.x,Pos3.y) & 0xffffffff   
    print 'end'

 

到这里我们就可以用以上的代码玩简单的小程序游戏了,小编经过试验后对比两种方法,按键ipipgo确实要比修改代码后的ctypes模拟点击要逊色不少,这也是我们学习python简单操作的优势的体现。

 

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-22发表,共计3404字。
新手QQ群:570568346,欢迎进群讨论 Python51学习