python自动扫雷实现方法

本文实例讲述了python自动扫雷实现方法。分享给大家供大家参考。具体如下:

#pywinminecrack.py
# coding: utf-8
import win32gui
import win32process
import win32con
import win32api
from ctypes import *
#雷区最大行列数
max_rows = 24
max_columns = 30
#雷区格子在窗体上的起始坐标及每个格子的宽度
mine_begin_x = 0xc
mine_begin_y = 0x37
mine_grid_width = 0x10
mine_grid_height = 0x10
#边框、无雷、有雷的内部表示
mine_boarder = 0x10
mine_safe = 0x0f
mine_danger = 0x8f
#“雷区”在 扫雷程序中的存储地址
board_addr = 0x1005340
class 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.readprocessmemory
writeprocessmemory = kernel32.writeprocessmemory
openprocess = kernel32.openprocess
ctrldata = 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 = 0
ctrldata.columns = 0
for i in range(0, max_columns + 2):
if mine_boarder == ctrldata.board[0]:
ctrldata.columns += 1
else :
break
ctrldata.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程序设计有所帮助。