日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
利用Python實(shí)現(xiàn)自動(dòng)掃雷小腳本

 自動(dòng)掃雷一般分為兩種,一種是讀取內(nèi)存數(shù)據(jù),而另一種是通過分析圖片獲得數(shù)據(jù),并通過模擬鼠標(biāo)操作,這里我用的是第二種方式。

一、準(zhǔn)備工作

1.掃雷游戲

我是win10,沒有默認(rèn)的掃雷,所以去掃雷網(wǎng)下載

http://www.saolei.net/BBS/

2.python 3

我的版本是 python 3.6.1

3.python的第三方庫(kù)

win32api,win32gui,win32con,Pillow,numpy,opencv

可通過 pip install --upgrade SomePackage 來進(jìn)行安裝

注意:有的版本是下載pywin32,但是有的要把pywin32升級(jí)到最高并自動(dòng)下載了pypiwin32,具體情況每個(gè)python版本可能都略有不同

我給出我的第三方庫(kù)和版本僅供參考

二、關(guān)鍵代碼組成

1.找到游戲窗口與坐標(biāo)

 
 
 
 
  1. #掃雷游戲窗口  
  2. class_name = "TMain"  
  3. title_name = "Minesweeper Arbiter "  
  4. hwnd = win32gui.FindWindow(class_name, title_name)  
  5. #窗口坐標(biāo)  
  6. left = 0  
  7. top = 0  
  8. right = 0  
  9. bottom = 0  
  10. if hwnd:  
  11.     print("找到窗口")  
  12.     left, top, right, bottom = win32gui.GetWindowRect(hwnd)  
  13.     #win32gui.SetForegroundWindow(hwnd)  
  14.     print("窗口坐標(biāo):")  
  15.     print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom))  
  16. else:  
  17.     print("未找到窗口") 

2.鎖定并抓取雷區(qū)圖像

 
 
 
 
  1. #鎖定雷區(qū)坐標(biāo)  
  2. #去除周圍功能按鈕以及多余的界面  
  3. #具體的像素值是通過QQ的截圖來判斷的  
  4. left += 15  
  5. top += 101  
  6. right -= 15  
  7. bottom -= 42  
  8. #抓取雷區(qū)圖像  
  9. rect = (left, top, right, bottom)  
  10. img = ImageGrab.grab().crop(rect) 

3.各圖像的RGBA值

 
 
 
 
  1. #數(shù)字1-8 周圍雷數(shù)  
  2. #0 未被打開  
  3. #ed 被打開 空白  
  4. #hongqi 紅旗  
  5. #boom 普通雷  
  6. #boom_red 踩中的雷  
  7. rgba_ed = [(225, (192, 192, 192)), (31, (128, 128, 128))]  
  8. rgba_hongqi = [(54, (255, 255, 255)), (17, (255, 0, 0)), (109, (192, 192, 192)), (54, (128, 128, 128)), (22, (0, 0, 0))]  
  9. rgba_0 = [(54, (255, 255, 255)), (148, (192, 192, 192)), (54, (128, 128, 128))]  
  10. rgba_1 = [(185, (192, 192, 192)), (31, (128, 128, 128)), (40, (0, 0, 255))]  
  11. rgba_2 = [(160, (192, 192, 192)), (31, (128, 128, 128)), (65, (0, 128, 0))]  
  12. rgba_3 = [(62, (255, 0, 0)), (163, (192, 192, 192)), (31, (128, 128, 128))]  
  13. rgba_4 = [(169, (192, 192, 192)), (31, (128, 128, 128)), (56, (0, 0, 128))]  
  14. rgba_5 = [(70, (128, 0, 0)), (155, (192, 192, 192)), (31, (128, 128, 128))]  
  15. rgba_6 = [(153, (192, 192, 192)), (31, (128, 128, 128)), (72, (0, 128, 128))]  
  16. rgba_8 = [(149, (192, 192, 192)), (107, (128, 128, 128))]  
  17. rgba_boom = [(4, (255, 255, 255)), (144, (192, 192, 192)), (31, (128, 128, 128)), (77, (0, 0, 0))]  
  18. rgba_boom_red = [(4, (255, 255, 255)), (144, (255, 0, 0)), (31, (128, 128, 128)), (77, (0, 0, 0))] 

4.掃描雷區(qū)圖像保存至一個(gè)二維數(shù)組map

 
 
 
 
  1. #掃描雷區(qū)圖像  
  2. def showmap():  
  3.     img = ImageGrab.grab().crop(rect)  
  4.     for y in range(blocks_y):  
  5.         for x in range(blocks_x):  
  6.             this_image = img.crop((x * block_width, y * block_height, (x + 1) * block_width, (y + 1) * block_height))  
  7.             if this_image.getcolors() == rgba_0:  
  8.                 map[y][x] = 0  
  9.             elif this_image.getcolors() == rgba_1:  
  10.                 map[y][x] = 1  
  11.             elif this_image.getcolors() == rgba_2:  
  12.                 map[y][x] = 2  
  13.             elif this_image.getcolors() == rgba_3:  
  14.                 map[y][x] = 3  
  15.             elif this_image.getcolors() == rgba_4:  
  16.                 map[y][x] = 4  
  17.             elif this_image.getcolors() == rgba_5:  
  18.                 map[y][x] = 5  
  19.             elif this_image.getcolors() == rgba_6:  
  20.                 map[y][x] = 6  
  21.             elif this_image.getcolors() == rgba_8:  
  22.                 map[y][x] = 8  
  23.             elif this_image.getcolors() == rgba_ed:  
  24.                 map[y][x] = -1  
  25.             elif this_image.getcolors() == rgba_hongqi:  
  26.                 map[y][x] = -4  
  27.             elif this_image.getcolors() == rgba_boom or this_image.getcolors() == rgba_boom_red:  
  28.                 global gameover  
  29.                 gameover = 1  
  30.                 break  
  31.                 #sys.exit(0)  
  32.             else:  
  33.                 print("無法識(shí)別圖像")  
  34.                 print("坐標(biāo)")  
  35.                 print((y,x))  
  36.                 print("顏色")  
  37.                 print(this_image.getcolors()) 
  38.                 sys.exit(0)  
  39.     #print(map) 

5.掃雷算法

這里我采用的最基礎(chǔ)的算法

1.首先點(diǎn)出一個(gè)點(diǎn)

2.掃描所有數(shù)字,如果周圍空白+插旗==數(shù)字,則空白均有雷,右鍵點(diǎn)擊空白插旗

3.掃描所有數(shù)字,如果周圍插旗==數(shù)字,則空白均沒有雷,左鍵點(diǎn)擊空白

4.循環(huán)2、3,如果沒有符合條件的,則隨機(jī)點(diǎn)擊一個(gè)白塊

 
 
 
 
  1. #插旗  
  2. def banner():  
  3.     showmap()  
  4.     for y in range(blocks_y):  
  5.         for x in range(blocks_x):  
  6.             if 1 <= map[y][x] and map[y][x] <= 5:  
  7.                 boom_number = map[y][x]  
  8.                 block_white = 0  
  9.                 block_qi = 0  
  10.                 for yy in range(y-1,y+2):  
  11.                     for xx in range(x-1,x+2):  
  12.                         if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:  
  13.                             if not (yy == y and xx == x):if map[yy][xx] == 0:  
  14.                                     block_white += 1  
  15.                                 elif map[yy][xx] == -4:  
  16.                                     block_qi += 1if boom_number == block_white + block_qi:for yy in range(y - 1, y + 2):  
  17.                         for xx in range(x - 1, x + 2):  
  18.                             if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:  
  19.                                 if not (yy == y and xx == x):  
  20.                                     if map[yy][xx] == 0:  
  21.                                         win32api.SetCursorPos([left+xx*block_width, top+yy*block_height])  
  22.                                         win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)  
  23.                                         win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) 
  24.                                          showmap()   
  25. #點(diǎn)擊白塊  
  26. def dig():  
  27.     showmap()  
  28.     iscluck = 0  
  29.     for y in range(blocks_y):  
  30.         for x in range(blocks_x):  
  31.             if 1 <= map[y][x] and map[y][x] <= 5:  
  32.                 boom_number = map[y][x]  
  33.                 block_white = 0  
  34.                 block_qi = 0  
  35.                 for yy in range(y - 1, y + 2): 
  36.                     for xx in range(x - 1, x + 2):  
  37.                         if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:  
  38.                             if not (yy == y and xx == x):  
  39.                                 if map[yy][xx] == 0:  
  40.                                     block_white += 1  
  41.                                 elif map[yy][xx] == -4:  
  42.                                     block_qi += 1if boom_number == block_qi and block_white > 0:for yy in range(y - 1, y + 2):  
  43.                         for xx in range(x - 1, x + 2):  
  44.                             if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:  
  45.                                 if not(yy == y and xx == x):  
  46.                                     if map[yy][xx] == 0:  
  47.                                         win32api.SetCursorPos([left + xx * block_width, top + yy * block_height])  
  48.                                         win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  
  49.                                         win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)  
  50.                                         iscluck = 1  
  51.     if iscluck == 0:  
  52.         luck()  
  53. #隨機(jī)點(diǎn)擊  
  54. def luck():  
  55.     fl = 1  
  56.     while(fl):  
  57.         randomrandom_x = random.randint(0, blocks_x - 1)  
  58.         randomrandom_y = random.randint(0, blocks_y - 1)  
  59.         if(map[random_y][random_x] == 0):  
  60.             win32api.SetCursorPos([left + random_x * block_width, top + random_y * block_height])  
  61.             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  
  62.             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)  
  63.             fl = 0  
  64. def gogo():  
  65.     win32api.SetCursorPos([left, top])  
  66.     win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  
  67.     win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)  
  68.     showmap()  
  69.     global gameover  
  70.     while(1):  
  71.         if(gameover == 0):  
  72.             banner()  
  73.             banner()  
  74.             dig()  
  75.         else:  
  76.             gameover = 0  
  77.             win32api.keybd_event(113, 0, 0, 0)  
  78.             win32api.SetCursorPos([left, top])  
  79.             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  
  80.             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)  
  81.             showmap()  

本文名稱:利用Python實(shí)現(xiàn)自動(dòng)掃雷小腳本
本文URL:http://m.5511xx.com/article/dhcjodh.html