Python自動掃雷實現方法

來源:互聯網
上載者:User
本文執行個體講述了Python自動掃雷實現方法。分享給大家供大家參考。具體如下:

#pyWinmineCrack.py# coding: utf-8import win32guiimport win32processimport win32conimport win32apifrom ctypes import *#雷區最大行列數MAX_ROWS = 24MAX_COLUMNS = 30#雷區格子在表單上的起始座標及每個格子的寬度MINE_BEGIN_X = 0xCMINE_BEGIN_Y = 0x37MINE_GRID_WIDTH = 0x10MINE_GRID_HEIGHT = 0x10#邊框、無雷、有雷的內部表示MINE_BOARDER = 0x10MINE_SAFE = 0x0FMINE_DANGER = 0x8F#“雷區”在 掃雷程式中的儲存地址BOARD_ADDR = 0x1005340class SMineCtrl(Structure): _fields_ = [("hWnd", c_uint),  ("board", (c_byte * (MAX_COLUMNS + 2)) * (MAX_ROWS + 2)),  ("rows", c_byte),  ("columns", c_byte) ]kernel32 = windll.LoadLibrary("kernel32.dll")ReadProcessMemory = kernel32.ReadProcessMemoryWriteProcessMemory = kernel32.WriteProcessMemoryOpenProcess = kernel32.OpenProcessctrlData = SMineCtrl()#找到掃雷程式並開啟對應進程try: ctrlData.hWnd = win32gui.FindWindow("掃雷", "掃雷")except: win32api.MessageBox(0, "請先運行掃雷程式", "錯誤!", win32con.MB_ICONERROR) exit(0)hreadID, processID = win32process.GetWindowThreadProcessId(ctrlData.hWnd)hProc = OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, processID)#讀取雷區資料bytesRead = c_ulong(0)ReadProcessMemory(hProc, BOARD_ADDR, byref(ctrlData.board), SMineCtrl.board.size, byref(bytesRead))if(bytesRead.value != SMineCtrl.board.size): str = "ReadProcessMemory error, only read ", bytesRead.value, " should read ", SMineCtrl.board.size win32api.MessageBox(0, str, "錯誤!", win32con.MB_ICONERROR) exit()#擷取本次程式雷區的實際大小ctrlData.rows = 0ctrlData.columns = 0for i in range(0, MAX_COLUMNS + 2): if MINE_BOARDER == ctrlData.board[0]: ctrlData.columns += 1 else : breakctrlData.columns -= 2 for i in range(1, MAX_ROWS + 1): if MINE_BOARDER != ctrlData.board[1]: ctrlData.rows += 1 else: break#類比滑鼠點擊動作for i in range(0, ctrlData.rows): for j in range(0, ctrlData.columns): if MINE_SAFE == ctrlData.board[i + 1][j + 1]:  win32api.SendMessage(ctrlData.hWnd,   win32con.WM_LBUTTONDOWN,   win32con.MK_LBUTTON,   win32api.MAKELONG(MINE_BEGIN_X + MINE_GRID_WIDTH * j + MINE_GRID_WIDTH / 2,   MINE_BEGIN_Y + MINE_GRID_HEIGHT * i + MINE_GRID_HEIGHT / 2))win32api.SendMessage(ctrlData.hWnd,   win32con.WM_LBUTTONUP,win32con.MK_LBUTTON,   win32api.MAKELONG(MINE_BEGIN_X + MINE_GRID_WIDTH * j + MINE_GRID_WIDTH / 2,   MINE_BEGIN_Y + MINE_GRID_HEIGHT * i + MINE_GRID_HEIGHT / 2))#搞定!win32api.MessageBox(0, "搞定!", "資訊", win32con.MB_ICONINFORMATION)

希望本文所述對大家的Python程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.