利用Python实现自动扫雷小脚本


Posted in Python onDecember 17, 2020

自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式。

一、准备工作

1.扫雷游戏

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

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

利用Python实现自动扫雷小脚本

2.python 3

我的版本是 python 3.6.1

3.python的第三方库

win32api,win32gui,win32con,Pillow,numpy,opencv

可通过 pip install --upgrade SomePackage 来进行安装

注意:有的版本是下载pywin32,但是有的要把pywin32升级到最高并自动下载了pypiwin32,具体情况每个python版本可能都略有不同

我给出我的第三方库和版本仅供参考

利用Python实现自动扫雷小脚本

二、关键代码组成

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: 
 print("找到窗口") 
 left, top, right, bottom = win32gui.GetWindowRect(hwnd) 
 #win32gui.SetForegroundWindow(hwnd) 
 print("窗口坐标:") 
 print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom)) 
else: 
 print("未找到窗口")

2.锁定并抓取雷区图像

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

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.扫描雷区图像保存至一个二维数组map

#扫描雷区图像 
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)

5.扫雷算法

这里我采用的最基础的算法

1.首先点出一个点

2.扫描所有数字,如果周围空白+插旗==数字,则空白均有雷,右键点击空白插旗

3.扫描所有数字,如果周围插旗==数字,则空白均没有雷,左键点击空白

4.循环2、3,如果没有符合条件的,则随机点击一个白块

#插旗 
def banner(): 
 showmap() 
 for y in range(blocks_y): 
  for x in range(blocks_x): 
   if 1 <= map[y][x] and map[y][x] <= 5: 
    boom_number = map[y][x] 
    block_white = 0 
    block_qi = 0 
    for yy in range(y-1,y+2): 
     for xx in range(x-1,x+2): 
      if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: 
       if not (yy == y and xx == x):if map[yy][xx] == 0: 
         block_white += 1 
        elif map[yy][xx] == -4: 
         block_qi += 1if boom_number == block_white + block_qi:for yy in range(y - 1, y + 2): 
      for xx in range(x - 1, x + 2): 
       if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: 
        if not (yy == y and xx == x): 
         if map[yy][xx] == 0: 
          win32api.SetCursorPos([left+xx*block_width, top+yy*block_height]) 
          win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) 
          win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) 
           showmap() 
#点击白块 
def dig(): 
 showmap() 
 iscluck = 0 
 for y in range(blocks_y): 
  for x in range(blocks_x): 
   if 1 <= map[y][x] and map[y][x] <= 5: 
    boom_number = map[y][x] 
    block_white = 0 
    block_qi = 0 
    for yy in range(y - 1, y + 2): 
     for xx in range(x - 1, x + 2): 
      if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: 
       if not (yy == y and xx == x): 
        if map[yy][xx] == 0: 
         block_white += 1 
        elif map[yy][xx] == -4: 
         block_qi += 1if boom_number == block_qi and block_white > 0:for yy in range(y - 1, y + 2): 
      for xx in range(x - 1, x + 2): 
       if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x: 
        if not(yy == y and xx == x): 
         if map[yy][xx] == 0: 
          win32api.SetCursorPos([left + xx * block_width, top + yy * block_height]) 
          win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 
          win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 
          iscluck = 1 
 if iscluck == 0: 
  luck() 
#随机点击 
def luck(): 
 fl = 1 
 while(fl): 
  randomrandom_x = random.randint(0, blocks_x - 1) 
  randomrandom_y = random.randint(0, blocks_y - 1) 
  if(map[random_y][random_x] == 0): 
   win32api.SetCursorPos([left + random_x * block_width, top + random_y * block_height]) 
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 
   fl = 0 
def gogo(): 
 win32api.SetCursorPos([left, top]) 
 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 
 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 
 showmap() 
 global gameover 
 while(1): 
  if(gameover == 0): 
   banner() 
   banner() 
   dig() 
  else: 
   gameover = 0 
   win32api.keybd_event(113, 0, 0, 0) 
   win32api.SetCursorPos([left, top]) 
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 
   win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 
   showmap()

到此这篇关于利用Python实现自动扫雷小脚本的文章就介绍到这了,更多相关Python实现自动扫雷小脚本内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中操作文件之write()方法的使用教程
May 25 Python
Python2.x中文乱码问题解决方法
Jun 02 Python
Python读写unicode文件的方法
Jul 10 Python
Django实现简单分页功能的方法详解
Dec 05 Python
Python matplotlib画图实例之绘制拥有彩条的图表
Dec 28 Python
python3.4控制用户输入与输出的方法
Oct 17 Python
在双python下设置python3为默认的方法
Oct 31 Python
Python3 Post登录并且保存cookie登录其他页面的方法
Dec 28 Python
python实现剪切功能
Jan 23 Python
Python正则表达式急速入门(小结)
Dec 16 Python
python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例
Mar 06 Python
python用700行代码实现http客户端
Jan 14 Python
用python读取xlsx文件
Dec 17 #Python
Python实现自动整理文件的脚本
Dec 17 #Python
Python用access判断文件是否被占用的实例方法
Dec 17 #Python
Python实例教程之检索输出月份日历表
Dec 16 #Python
使用Python判断一个文件是否被占用的方法教程
Dec 16 #Python
python中类与对象之间的关系详解
Dec 16 #Python
python简单实现插入排序实例代码
Dec 16 #Python
You might like
用PHP制作静态网站的模板框架(三)
2006/10/09 PHP
PHP中使用FFMPEG获取视频缩略图和视频总时长实例
2014/05/04 PHP
改写ThinkPHP的U方法使其路由下分页正常
2014/07/02 PHP
Yii框架关联查询with用法分析
2014/12/02 PHP
php mailer类调用远程SMTP服务器发送邮件实现方法
2016/03/04 PHP
php版阿里大于(阿里大鱼)短信发送实例详解
2016/11/30 PHP
jQuery 获取浏览器所在的IP地址的小例子
2013/11/08 Javascript
JavaScript基本数据类型及值类型和引用类型
2015/08/25 Javascript
ajax 提交数据到后台jsp页面及页面跳转问题
2017/01/19 Javascript
nodejs制作爬虫实现批量下载图片
2017/05/19 NodeJs
Angular2.0实现modal对话框的方法示例
2018/02/18 Javascript
vue实现密码显示隐藏切换功能
2018/02/23 Javascript
Vue服务端渲染实践之Web应用首屏耗时最优化方案
2019/03/22 Javascript
基于Vue实现电商SKU组合算法问题
2019/05/29 Javascript
JavaScript制作3D旋转相册
2020/08/02 Javascript
JavaScript日期库date-fn.js使用方法解析
2020/09/09 Javascript
深入了解Vue动态组件和异步组件
2021/01/26 Vue.js
Node.js中的异步生成器与异步迭代详解
2021/01/31 Javascript
[48:21]林俊杰圣堂刺客超神杀戮秀
2014/10/29 DOTA
使用python BeautifulSoup库抓取58手机维修信息
2013/11/21 Python
使用Python多线程爬虫爬取电影天堂资源
2016/09/23 Python
Python正则替换字符串函数re.sub用法示例
2017/01/19 Python
Python 自动化表单提交实例代码
2017/06/08 Python
python操作mysql代码总结
2018/06/01 Python
Pycharm中出现ImportError:DLL load failed:找不到指定模块的解决方法
2019/09/17 Python
python抓取多种类型的页面方法实例
2019/11/20 Python
详解python中的闭包
2020/09/07 Python
html5 canvas移动浏览器上实现图片压缩上传
2016/03/11 HTML / CSS
《雷鸣电闪波尔卡》教学反思
2014/02/23 职场文书
作文评语大全
2014/04/23 职场文书
会计系毕业求职信
2014/08/07 职场文书
市场部经理岗位职责
2015/02/02 职场文书
2015年学生会部门工作总结
2015/04/21 职场文书
干货:如何写好观后感 !
2019/05/21 职场文书
uniapp开发小程序的经验总结
2021/04/08 Javascript
ES6 解构赋值的原理及运用
2021/05/25 Javascript