Board logo

标题: [原创代码] [Python]Win7版自动扫雷 [打印本页]

作者: broly    时间: 2012-8-18 01:22     标题: [Python]Win7版自动扫雷

本帖最后由 broly 于 2012-8-18 01:25 编辑

这个脚本需要Python Win32 Extensions的支持,我也把它做成了exe上传了,有需要的朋友可以下载来玩玩,仅供娱乐。

由于是点击部分靠模拟鼠标按键来实现的,坐标位置不能很好定位,所以最好把扫雷程序的窗口高度调到1024左右。


  1. '''############################################################################
  2. #
  3. #-->    AutoSweeper for Windows 7
  4. #
  5. #           Code By Broly
  6. #           From http://www.dreamlikes.net
  7. #
  8. #-->    Testing environment:
  9. #           Windows 7 Ultimate with SP1 (x86)
  10. #           Python 2.7
  11. #
  12. #-->    Note:
  13. #           1.Please make sure that you have open the MineSweeper.exe
  14. #            program before running this script. Moreover, the
  15. #            MineSweeper.exe should be neither maximized nor minimized.
  16. #   
  17. #           2.When this script running, don't move your mouse.
  18. #   
  19. ############################################################################
  20. '''
  21. # import
  22. import win32api
  23. import win32gui
  24. import win32con
  25. import win32process
  26. import time
  27. from ctypes import *
  28. #const variable
  29. TH32CS_SNAPMODULE = 0x00000008
  30. PROCESS_ALL_ACCESS = 0x1F0FFF
  31. HWND_NOTOPMOST = -2
  32. HWND_TOPMOST = -1
  33. SWP_NOSIZE = 0x0001
  34. MOUSEEVENTF_LEFTDOWN = 0x0002
  35. MOUSEEVENTF_LEFTUP = 0x0004
  36. MOUSEEVENTF_RIGHTDOWN = 0x0008
  37. MOUSEEVENTF_RIGHTUP = 0x0010
  38. #struct
  39. class MODULEENTRY32(Structure):
  40.     _fields_ = [ ( 'dwSize' , c_long ) ,
  41.                 ( 'th32ModuleID' , c_long ),
  42.                 ( 'th32ProcessID' , c_long ),
  43.                 ( 'GlblcntUsage' , c_long ),
  44.                 ( 'ProccntUsage' , c_long ) ,
  45.                 ( 'modBaseAddr' , c_long ) ,
  46.                 ( 'modBaseSize' , c_long ) ,
  47.                 ( 'hModule' , c_void_p ) ,
  48.                 ( 'szModule' , c_char * 256 ),
  49.                 ( 'szExePath' , c_char * 260 ) ]
  50.    
  51. ## LoadLibrary
  52. kernel32 = windll.LoadLibrary("kernel32.dll")
  53. ## OpenProcess
  54. OpenProcess = kernel32.OpenProcess
  55. ## CreateToolhelp32Snapshot
  56. CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot
  57. CreateToolhelp32Snapshot.reltype = c_long
  58. CreateToolhelp32Snapshot.argtypes = [ c_int , c_int ]
  59. ## Module32First
  60. Module32First = kernel32.Module32First
  61. Module32First.argtypes = [ c_void_p , POINTER(MODULEENTRY32) ]
  62. Module32First.rettype = c_int
  63. ## Module32Next
  64. Module32Next = kernel32.Module32Next
  65. Module32Next.argtypes = [ c_void_p , POINTER(MODULEENTRY32) ]
  66. Module32Next.rettype = c_int
  67. ## CloseHandle
  68. CloseHandle = kernel32.CloseHandle
  69. CloseHandle.argtypes = [ c_void_p ]
  70. CloseHandle.rettype = c_int
  71. ## ReadProcessMemory
  72. ReadProcessMemory = kernel32.ReadProcessMemory
  73. #function
  74. def autoSweep():
  75.     hWnd = win32gui.FindWindow('Minesweeper',None)
  76.     if hWnd == win32con.NULL:
  77.         print 'Failed to find the Minesweeper\'s window.'
  78.         return 1
  79.         
  80.         
  81.     ThreadID,ProcessID = win32process.GetWindowThreadProcessId(hWnd)
  82.     hProcess = OpenProcess(PROCESS_ALL_ACCESS,
  83.                                     False ,
  84.                                     ProcessID)
  85.     if hProcess == win32con.NULL:
  86.         print 'Failed to open the Minesweeper\'s process.'
  87.         return 1
  88.    
  89.    
  90.     hSnapshot = c_void_p(0)
  91.     me32 = MODULEENTRY32()
  92.     me32.dwSize = sizeof(MODULEENTRY32)
  93.    
  94.     hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessID)
  95.     ret = Module32First(hSnapshot, pointer(me32))
  96.     if ret == False:
  97.         CloseHandle(hSnapshot)
  98.         print 'Failed to enumerate the module'
  99.         return 1
  100.    
  101.     while ret:
  102.          if cmp(me32.szModule.lower(),'minesweeper.exe') == 0:
  103.             mineBaseAddr = int(me32.modBaseAddr)
  104.          ret = Module32Next(hSnapshot, pointer(me32))
  105.         
  106.     CloseHandle(hSnapshot)
  107.    
  108.    
  109.     buffer = c_void_p(0)
  110.     dwDate = c_void_p(0)
  111.     topBaseAddr = mineBaseAddr + 0x868B4
  112.     MineInfo = {'count' : 0,'row' : 0,'column' : 0}
  113.    
  114.     try:
  115.         ReadProcessMemory(hProcess, topBaseAddr, pointer(buffer), 4, win32con.NULL)
  116.         ReadProcessMemory(hProcess, buffer.value + 0x10, pointer(buffer), 4, win32con.NULL)
  117.         ReadProcessMemory(hProcess, buffer.value + 0x04, pointer(dwDate), 4, win32con.NULL)
  118.         MineInfo['count'] = dwDate.value
  119.         ReadProcessMemory(hProcess, buffer.value + 0x08, pointer(dwDate), 4, win32con.NULL)
  120.         MineInfo['row'] = dwDate.value
  121.         ReadProcessMemory(hProcess, buffer.value + 0x0C, pointer(dwDate), 4, win32con.NULL)
  122.         MineInfo['column'] = dwDate.value
  123.     except:
  124.         print 'Failed to read the Minesweeper\'s memory date.'
  125.         return 1
  126.    
  127.    
  128.     win32gui.SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE)
  129.     cleft, ctop, cright, cbottom = win32gui.GetClientRect(hWnd)
  130.     if cright<=0 or cbottom<=0:
  131.         print 'Failed to get the rectangle of th Minesweeper\'s window.'
  132.         return 1
  133.         
  134.    
  135.     x, y = win32gui.ClientToScreen(hWnd,(cleft,ctop))
  136.     edge = cright/(MineInfo['column'] + 4) + 1
  137.    
  138.     originX = edge*2 + edge/2 + x
  139.     originY = (cbottom-MineInfo['row']*edge)/2 + edge/2 + y
  140.    
  141.    
  142.     win32api.SetCursorPos((originX,originY))
  143.    
  144.     time.sleep(0.05)
  145.     win32api.mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,0,0,0,0)
  146.     win32api.mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,0,0,0,0)
  147.                
  148.     time.sleep(1)
  149.    
  150.     ReadProcessMemory(hProcess, buffer.value + 0x44, pointer(buffer), 4, win32con.NULL)
  151.     ReadProcessMemory(hProcess, buffer.value + 0x0C, pointer(buffer), 4, win32con.NULL)
  152.    
  153.    
  154.     buffer2 = c_void_p(0)
  155.     dwDate2 = c_void_p(0)
  156.     bFlag = [[0 for col in range(MineInfo['row'])] for row in range(MineInfo['column'])]
  157.    
  158.    
  159.     for i in range(0, MineInfo['column']):
  160.         ReadProcessMemory(hProcess, buffer.value + 4*i, pointer(buffer2), 4, win32con.NULL)
  161.         ReadProcessMemory(hProcess, buffer2.value + 0x0C, pointer(buffer2), 4, win32con.NULL)   
  162.         for j in range(0, MineInfo['row']):
  163.             ReadProcessMemory(hProcess, buffer2.value + j, pointer(dwDate2), 1, win32con.NULL)         
  164.             if dwDate2.value==None:
  165.                 bFlag[i][j] = 0
  166.             else:
  167.                 bFlag[i][j] = dwDate2.value
  168.                
  169.                
  170.     CloseHandle(hProcess)
  171.    
  172.     Count = 0
  173.     for i in range(0, MineInfo['row']):
  174.         for j in range(0, MineInfo['column']):
  175.             if win32gui.FindWindow('Minesweeper',None) != win32con.NULL:
  176.                 win32api.SetCursorPos((originX+j*edge,originY+i*edge))
  177.                 if bFlag[j][i] == 0:
  178.                     win32api.mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,0,0,0,0)
  179.                     Count += 1
  180.                 else:
  181.                     if Count == (MineInfo['row']*MineInfo['column']-MineInfo['count']):
  182.                         break
  183.                     win32api.mouse_event(MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP,0,0,0,0)
  184.    
  185.                 time.sleep(0.05)
  186.             else:
  187.                 print 'Failed to find the Minesweeper\'s window.'
  188.                 return 1
  189.    
  190.    
  191.     win32gui.SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE)
  192.     return 0
  193. # main
  194. if __name__ == '__main__' :
  195.     print __doc__
  196.     print '\n\n'
  197.     bSuccess = autoSweep()
  198.     if bSuccess == 1:
  199.         input('Press ENTER to exit.')
复制代码



下载地址:AutoSweeper4win7.rar

—————————————————————-

转载注明出处,本文地址:http://www.dreamlikes.net/archives/493


作者: QIAOXINGXING    时间: 2012-8-18 16:59

xp sp3 python 2.72 测试出错
xp里扫雷叫winmine
作者: broly    时间: 2012-8-18 17:17

回复 2# QIAOXINGXING


    节操啊。标题都没看清?

这个只适合win7的。XP不适用
作者: garyng    时间: 2012-8-18 17:21

回复 2# QIAOXINGXING


    楼主都注明了"win7版"的嘛,于其它版本都不兼容,原因是不同版本的扫雷的基地址都不同

ps 我有个兼容XP的自动扫雷,VB6写的,挺不错的,每次都秒杀  哈哈
作者: QIAOXINGXING    时间: 2012-8-19 14:31

回复  QIAOXINGXING


     节操啊。标题都没看清?

这个只适合win7的。XP不适用
broly 发表于 2012-8-18 17:17



    汗。。。。。。。。。
作者: PowerShell    时间: 2013-7-7 09:12

能谈谈原理么?




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2