成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

利用Python實現自動掃雷小腳本

開發 后端
自動掃雷一般分為兩種,一種是讀取內存數據,而另一種是通過分析圖片獲得數據,并通過模擬鼠標操作,這里我用的是第二種方式。

 自動掃雷一般分為兩種,一種是讀取內存數據,而另一種是通過分析圖片獲得數據,并通過模擬鼠標操作,這里我用的是第二種方式。

一、準備工作

1.掃雷游戲

我是win10,沒有默認的掃雷,所以去掃雷網下載

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

2.python 3

我的版本是 python 3.6.1

3.python的第三方庫

win32api,win32gui,win32con,Pillow,numpy,opencv

可通過 pip install --upgrade SomePackage 來進行安裝

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

我給出我的第三方庫和版本僅供參考

二、關鍵代碼組成

1.找到游戲窗口與坐標 

  1. #掃雷游戲窗口  
  2. class_name = "TMain"  
  3. title_name = "Minesweeper Arbiter "  
  4. hwnd = win32gui.FindWindow(class_name, title_name)  
  5. #窗口坐標  
  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("窗口坐標:")  
  15.     print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom))  
  16. else:  
  17.     print("未找到窗口") 

2.鎖定并抓取雷區圖像 

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

3.各圖像的RGBA值 

  1. #數字1-8 周圍雷數  
  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.掃描雷區圖像保存至一個二維數組map 

  1. #掃描雷區圖像  
  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("無法識別圖像")  
  34.                 print("坐標")  
  35.                 print((y,x))  
  36.                 print("顏色")  
  37.                 print(this_image.getcolors()) 
  38.                 sys.exit(0)  
  39.     #print(map) 

5.掃雷算法

這里我采用的最基礎的算法

1.首先點出一個點

2.掃描所有數字,如果周圍空白+插旗==數字,則空白均有雷,右鍵點擊空白插旗

3.掃描所有數字,如果周圍插旗==數字,則空白均沒有雷,左鍵點擊空白

4.循環2、3,如果沒有符合條件的,則隨機點擊一個白塊 

  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. #點擊白塊  
  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. #隨機點擊  
  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()  

 

責任編輯:龐桂玉 來源: 馬哥Linux運維
相關推薦

2014-03-18 10:19:55

Hadoop部署hadoop集群腳本

2020-03-18 09:23:24

Python數據SQL

2010-03-10 12:33:10

Python腳本

2022-08-02 11:24:22

菜鳥Python網站自動簽到

2023-05-04 09:51:07

ChatGPTAI

2011-03-23 08:42:36

Visual Stud

2010-09-27 09:13:36

Visual Stud

2022-09-02 15:08:02

Python郵件發送

2009-06-24 10:44:08

2020-09-23 06:00:04

ShellLinux郵件監控

2022-10-17 15:59:40

Shell腳本終端

2024-04-30 08:00:00

人工智能自動化文件處理

2021-11-01 10:26:08

傳感器農業自動化物聯網

2021-09-09 06:55:44

Python瀏覽器程序

2023-06-28 00:05:44

人工智能聊天機器人ChatGPT

2022-04-28 18:37:50

PythonExcel

2024-07-01 18:07:30

Python腳本自動化

2021-10-13 09:33:26

Python 多任務進程

2010-03-04 15:45:56

Python程序調試

2023-11-09 09:28:09

Java代碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲欧美日韩中文在线 | 97日韩精品 | 久久久xx | 国产高清一区二区三区 | av日韩精品| 国产精品永久免费视频 | 一级毛片视频 | 久视频在线观看 | 亚洲精品久久区二区三区蜜桃臀 | 久久久久久久一区二区 | 国产成人精品免费视频大全最热 | 欧美视频精品 | 亚洲欧美aⅴ | 国产视频综合 | av在线三级 | 国产美女一区二区 | 99国产精品99久久久久久 | 国产成人精品网站 | 久久精品16 | 日韩精品二区 | 久久久久国产一区二区三区 | 国产激情视频在线 | 视频二区在线观看 | 国产精品九九九 | 岛国一区| 欧美日韩一区二区视频在线观看 | 欧美日韩综合视频 | 人人看人人干 | 黄色一级大片视频 | 欧美淫| 天天干天天插天天 | jizz中国日本 | 国产精品18久久久久久久 | 全免费a级毛片免费看视频免 | 亚洲精品在线观看网站 | 亚洲视频中文字幕 | 久久91精品国产 | 午夜理伦三级理论三级在线观看 | 欧美国产91 | 日韩视频精品在线 | 国产精品综合视频 |