作者:chestnut_egg
自動掃雷一般分為兩種,一種是讀取內存數據,而另一種是通過分析圖片獲得數據,并通過模擬鼠標操作,這里我用的是第二種方式。
一、準備工作
我是win10,沒有默認的掃雷,所以去掃雷網下載
我的版本是 python 3.6.1
win32api,win32gui,win32con,Pillow,numpy,opencv可通過 pip install --upgrade SomePackage 來進行安裝注意:有的版本是下載pywin32,但是有的要把pywin32升級到最高并自動下載了pypiwin32,具體情況每個python版本可能都略有不同
我給出我的第三方庫和版本僅供參考
二、關鍵代碼組成
#掃雷游戲窗口
class_name = "TMain"
title_name = "Minesweeper Arbiter "
hwnd = win32gui.FindWindow(class_name, title_name)
#窗口坐標
left = 0
top = 0
right = 0
bottom = 0
if hwnd:
print("找到窗口")
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
#win32gui.SetForegroundWindow(hwnd)
print("窗口坐標:")
print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom))
else:
print("未找到窗口")
#鎖定雷區坐標
#去除周圍功能按鈕以及多余的界面
#具體的像素值是通過QQ的截圖來判斷的
left += 15
top += 101
right -= 15
bottom -= 42
#抓取雷區圖像
rect = (left, top, right, bottom)
img = ImageGrab.grab().crop(rect)
#數字1-8 周圍雷數
#0 未被打開
#ed 被打開 空白
#hongqi 紅旗
#boom 普通雷
#boom_red 踩中的雷
rgba_ed = [(225, (192, 192, 192)), (31, (128, 128, 128))]
rgba_hongqi = [(54, (255, 255, 255)), (17, (255, 0, 0)), (109, (192, 192, 192)), (54, (128, 128, 128)), (22, (0, 0, 0))]
rgba_0 = [(54, (255, 255, 255)), (148, (192, 192, 192)), (54, (128, 128, 128))]
rgba_1 = [(185, (192, 192, 192)), (31, (128, 128, 128)), (40, (0, 0, 255))]
rgba_2 = [(160, (192, 192, 192)), (31, (128, 128, 128)), (65, (0, 128, 0))]
rgba_3 = [(62, (255, 0, 0)), (163, (192, 192, 192)), (31, (128, 128, 128))]
rgba_4 = [(169, (192, 192, 192)), (31, (128, 128, 128)), (56, (0, 0, 128))]
rgba_5 = [(70, (128, 0, 0)), (155, (192, 192, 192)), (31, (128, 128, 128))]
rgba_6 = [(153, (192, 192, 192)), (31, (128, 128, 128)), (72, (0, 128, 128))]
rgba_8 = [(149, (192, 192, 192)), (107, (128, 128, 128))]
rgba_boom = [(4, (255, 255, 255)), (144, (192, 192, 192)), (31, (128, 128, 128)), (77, (0, 0, 0))]
rgba_boom_red = [(4, (255, 255, 255)), (144, (255, 0, 0)), (31, (128, 128, 128)), (77, (0, 0, 0))]
#掃描雷區圖像
def showmap():
img = ImageGrab.grab().crop(rect)
for y in range(blocks_y):
for x in range(blocks_x):
this_image = img.crop((x * block_width, y * block_height, (x + 1) * block_width, (y + 1) * block_height))
if this_image.getcolors() == rgba_0:
map[y][x] = 0
elif this_image.getcolors() == rgba_1:
map[y][x] = 1
elif this_image.getcolors() == rgba_2:
map[y][x] = 2
elif this_image.getcolors() == rgba_3:
map[y][x] = 3
elif this_image.getcolors() == rgba_4:
map[y][x] = 4
elif this_image.getcolors() == rgba_5:
map[y][x] = 5
elif this_image.getcolors() == rgba_6:
map[y][x] = 6
elif this_image.getcolors() == rgba_8:
map[y][x] = 8
elif this_image.getcolors() == rgba_ed:
map[y][x] = -1
elif this_image.getcolors() == rgba_hongqi:
map[y][x] = -4
elif this_image.getcolors() == rgba_boom or this_image.getcolors() == rgba_boom_red:
global gameover
gameover = 1
break
#sys.exit(0)
else:
print("無法識別圖像")
print("坐標")
print((y,x))
print("顏色")
print(this_image.getcolors())
sys.exit(0)
#print(map)
這里我采用的最基礎的算法
1.首先點出一個點
2.掃描所有數字,如果周圍空白+插旗==數字,則空白均有雷,右鍵點擊空白插旗
3.掃描所有數字,如果周圍插旗==數字,則空白均沒有雷,左鍵點擊空白
4.循環2、3,如果沒有符合條件的,則隨機點擊一個白塊
你還記得那個一玩一整天的掃雷游戲嗎?作為 Windows 自帶的經典益智小游戲之一,曾帶來簡單的快樂,留下了滿滿的回憶。但是掃雷在 Windows 11 中卻不見了。實際上,早在 Windows 10 中,掃雷就從 Windows 標準功能中被移除了。
也許有新朋友會告訴你,可以打開 Microsoft Store,然后搜索 Microsoft Minesweeper,這樣你就可以玩掃雷了。但恐怕你受不了免費版隔三差五地彈廣告,而且 Microsoft Store 需要注冊賬號。
這時老朋友會告訴你,從一臺 Windows 7 的 PC 中,找到掃雷游戲的程序文件,直接復制到 Windows 11 不香嗎?如果照做你會發現,程序根本無法運行,即使右鍵屬性中設置為Windows 7 兼容模式。
沒轍了嗎?有辦法!下載一個HXD這樣的十六進制編輯器,打開 Minesweeper 程序,查找 403acf7507,將其中的 75 修改為 74 保存。再次運行 Minesweeper 程序,熟悉的掃雷又回來了。又是一個充實的假日時光。
實際上,這里簡單粗暴地修改了掃雷的程序代碼,跳過了一個出錯的指令,這個指令調用一個用于某些權限檢查函數:slc.dll里面的SLGetWindowsInformationDWORD。
以上