使用Python怎么编写一个自动扫雷的脚本

  介绍

本篇文章给大家分享的是有关使用Python怎么编写一个自动扫雷的脚本,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、准备工作

1。扫雷游戏

我是win10,没有默认的扫雷,所以去扫雷网下载

使用Python怎么编写一个自动扫雷的脚本“> <br/> </p> <h4> 2。python 3 </h4> <p>我的版本是python 3.6.1 </p> <h4> 3。python的第三方库</h4> <p> win32api, win32gui, win32con,枕头,numpy, opencv </p> <p>可通过pip安装,升级SomePackage来进行安装</p> <p>注意:有的版本是下载pywin32,但是有的要把pywin32升级到最高并自动下载了pypiwin32,具体情况每个python版本可能都略有不同</p> <p>我给出我的第三方库和版本仅供参考</p> <p> <img src=

二,关键代码组成

1。找到游戏窗口与坐标

#扫雷游戏窗口,   时间=class_name “TMain",   时间=title_name “Minesweeper  Arbiter “,   时间=hwnd  win32gui.FindWindow (class_name, title_name),   #窗口坐标,   left =, 0,   top =, 0,   right =, 0,   bottom =, 0,   if  hwnd:,   ,打印(“找到窗口“),   ,,,,,,bottom =, win32gui.GetWindowRect (hwnd),   ,# win32gui.SetForegroundWindow (hwnd),   ,打印(“窗口坐标:“),   ,打印(str(左)+ & # 39;,& # 39;+ str(右)+ & # 39;,& # 39;+ str(上)+ & # 39;,& # 39;+ str(底部)),   其他:   ,打印(“未找到窗口“)

2。锁定并抓取雷区图像

#锁定雷区坐标,   #去除周围功能按钮以及多余的界面,   #具体的像素值是通过QQ的截图来判断的,   left  +=, 15,   top  +=, 101,   right  -=, 15,   bottom  -=, 42岁,   #抓取雷区图像,   rect =,(,,,,对,,底部),   时间=img  ImageGrab.grab () .crop(矩形)

3。各图像的RGBA值

#数字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)))

4。扫描雷区图像保存至一个二维数组地图

#扫描雷区图像,   def  showmap ():=,,img  ImageGrab.grab () .crop(矩形),   ,for  y 范围(blocks_y):拷贝,   for 才能;x 范围(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:,   ,,,地图[y] [x],=, 0,   ,,elif  this_image.getcolors (),==, rgba_1:,   ,,,地图[y] [x],=, 1,   ,,elif  this_image.getcolors (),==, rgba_2:,   ,,,地图[y] [x],=, 2,   ,,elif  this_image.getcolors (),==, rgba_3:,   ,,,地图[y] [x],=, 3,   ,,elif  this_image.getcolors (),==, rgba_4:,   ,,,地图[y] [x],=, 4,   ,,elif  this_image.getcolors (),==, rgba_5:,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

使用Python怎么编写一个自动扫雷的脚本