利用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

2023-06-28 00:05:44

人工智能聊天机器人ChatGPT

2020-09-23 06:00:04

ShellLinux邮件监控

2022-04-28 18:37:50

PythonExcel

2022-10-17 15:59:40

Shell脚本终端

2021-11-01 10:26:08

传感器农业自动化物联网

2021-10-13 09:33:26

Python 多任务进程

2010-03-04 15:45:56

Python程序调试

2021-09-09 06:55:44

Python浏览器程序

2023-11-09 09:28:09

Java代码

2010-10-19 14:30:34

SQL SERVER自

2012-02-17 00:01:21

heartbeat节点高可用
点赞
收藏

51CTO技术栈公众号