自动扫雷 python
自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式。
// 2018.8.10更新
代码已上传至GitHub
https://github.com/chestnut-egg/GoMine
一、准备工作
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.找到游戏窗口与坐标
#扫雷游戏窗口
class_name = "TMain"
title_name = "Minesweeper Arbiter "
hwnd = win32gui.FindWindow(class_name, title_name) #窗口坐标
left =
top =
right =
bottom = 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 +=
top +=
right -=
bottom -= #抓取雷区图像
rect = (left, top, right, bottom)
img = ImageGrab.grab().crop(rect)
3.各图像的RGBA值
#数字1-8 周围雷数
#0 未被打开
#ed 被打开 空白
#hongqi 红旗
#boom 普通雷
#boom_red 踩中的雷
rgba_ed = [(, (, , )), (, (, , ))]
rgba_hongqi = [(, (, , )), (, (, , )), (, (, , )), (, (, , )), (, (, , ))]
rgba_0 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_1 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_2 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_3 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_4 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_5 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_6 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_8 = [(, (, , )), (, (, , ))]
rgba_boom = [(, (, , )), (, (, , )), (, (, , )), (, (, , ))]
rgba_boom_red = [(, (, , )), (, (, , )), (, (, , )), (, (, , ))]
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 + ) * block_width, (y + ) * block_height))
if this_image.getcolors() == rgba_0:
map[y][x] =
elif this_image.getcolors() == rgba_1:
map[y][x] =
elif this_image.getcolors() == rgba_2:
map[y][x] =
elif this_image.getcolors() == rgba_3:
map[y][x] =
elif this_image.getcolors() == rgba_4:
map[y][x] =
elif this_image.getcolors() == rgba_5:
map[y][x] =
elif this_image.getcolors() == rgba_6:
map[y][x] =
elif this_image.getcolors() == rgba_8:
map[y][x] =
elif this_image.getcolors() == rgba_ed:
map[y][x] = -
elif this_image.getcolors() == rgba_hongqi:
map[y][x] = -
elif this_image.getcolors() == rgba_boom or this_image.getcolors() == rgba_boom_red:
global gameover
gameover =
break
#sys.exit()
else:
print("无法识别图像")
print("坐标")
print((y,x))
print("颜色")
print(this_image.getcolors())
sys.exit()
#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 <= map[y][x] and map[y][x] <= :
boom_number = map[y][x]
block_white =
block_qi =
for yy in range(y-,y+):
for xx in range(x-,x+):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not (yy == y and xx == x):if map[yy][xx] == :
block_white +=
elif map[yy][xx] == -:
block_qi += if boom_number == block_white + block_qi:for yy in range(y - , y + ):
for xx in range(x - , x + ):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not (yy == y and xx == x):
if map[yy][xx] == :
win32api.SetCursorPos([left+xx*block_width, top+yy*block_height])
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, , , , )
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, , , , )
showmap() #点击白块
def dig():
showmap()
iscluck =
for y in range(blocks_y):
for x in range(blocks_x):
if <= map[y][x] and map[y][x] <= :
boom_number = map[y][x]
block_white =
block_qi =
for yy in range(y - , y + ):
for xx in range(x - , x + ):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not (yy == y and xx == x):
if map[yy][xx] == :
block_white +=
elif map[yy][xx] == -:
block_qi += if boom_number == block_qi and block_white > :for yy in range(y - , y + ):
for xx in range(x - , x + ):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not(yy == y and xx == x):
if map[yy][xx] == :
win32api.SetCursorPos([left + xx * block_width, top + yy * block_height])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, , , , )
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, , , , )
iscluck =
if iscluck == :
luck() #随机点击
def luck():
fl =
while(fl):
random_x = random.randint(, blocks_x - )
random_y = random.randint(, blocks_y - )
if(map[random_y][random_x] == ):
win32api.SetCursorPos([left + random_x * block_width, top + random_y * block_height])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, , , , )
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, , , , )
fl =
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 3.6.1python的第三方库:win ...
- 1秒内通关扫雷?他创造属于自己的世界记录!Python实现自动扫雷
五一劳动节假期,我们一起来玩扫雷吧.用Python+OpenCV实现了自动扫雷,突破世界记录,我们先来看一下效果吧. 中级 - 0.74秒 3BV/S=60.81 相信许多人很早就知道有扫雷这么一款经 ...
- [Dynamic Language] 用Sphinx自动生成python代码注释文档
用Sphinx自动生成python代码注释文档 pip install -U sphinx 安装好了之后,对Python代码的文档,一般使用sphinx-apidoc来自动生成:查看帮助mac-abe ...
- 如何使用python来模拟鼠标点击(将通过实例自动化模拟在360浏览器中自动搜索"python")
一.准备工作: 安装pywin32,后面开发需要pywin32的支持,否则无法完成与windows层面相关的操作. pywin32的具体安装及注意事项: 1.整体开发环境: 基于windows7操作系 ...
- PyCharm配置autopep8,自动格式化Python代码
1. 关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性. 官网地址:h ...
- nginx tomcat 自动部署python脚本【转】
#!/usr/bin/env python #--coding:utf8-- import sys,subprocess,os,datetime,paramiko,re local_path='/ho ...
- 文件参数化-utp框架之根据yaml文件自动生成python文件+utp运行用例
根据yaml文件自动生成python文件 utp框架: bin目录:存放执行文件(run.py) cases目录:存放生成的用例的python文件(该目录下的文件为根据data目录下的测试用例生成的p ...
- SAE部署Python-让云端自动运行Python代码
之前写过模拟登录新浪微博的帖子,然而我并没有去爬过微博的数据,觉得有点浪费,于是就想写一个代码来发微博.写完之后觉得如果能自动发微博就好了,但是我又不可能24小时开始(晚上12点后还会断网),也没有v ...
- windows 10 如何设定计划任务自动执行 python 脚本?
我用 python 写了一些脚本,有一些是爬虫脚本,比如爬取知乎特定话题的热门问题,有一些是定期的统计分析脚本,输出统计结果到文档中.之前我都是手动执行这些脚本,现在我希望如何这些脚本能自动定时执行. ...
随机推荐
- hashlib的md5计算
hashlib的md5计算 hashlib概述 涉及加密服务:Cryptographic Services 其中 hashlib是涉及 安全散列 和 消息摘要 ,提供多个不同的加密算法借口,如SHA1 ...
- Java中的二分查找
二分查找:(折半查找) 前提:数组必须是有序的. 思想:每次都猜中间的那个元素,比较大或者小,就能减少一半的元素.思路:A:定义最小索引,最大索引. B:比较出中间索引 C:拿中间索引的值和要查找的元 ...
- 痞子衡嵌入式:大话双核i.MXRT1170之Cortex-M7与Cortex-M4互相激活之道
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是恩智浦i.MXRT1170上Cortex-M7与Cortex-M4内核互相激活的方法. 痞子衡最近在深耕i.MXRT1170这颗划时代的 ...
- 如何用hugo 搭建博客
1,Hugo 简介 搭建个人博客有很多开源的博客框架,我们要介绍的框架叫作Hugo.Hugo 是一个基于Go 语言的框架,可以快速方便的创建自己的博客. Hugo 支持Markdown 语法,我们可以 ...
- Hexo博客插入图片的方法
Hexo博客插入图片的方法 hexo图片blog hexo blog 插入图片的方法总结 hexo 的blog 内容是根据 markdown 文件的内容生成的html文件, 生成的文件全部在 /pub ...
- 理解分布式一致性:Paxos协议之Multi-Paxos
理解分布式一致性:Paxos协议之Multi-Paxos Multi-Paxos without failures Multi-Paxos when phase 1 can be skipped Mu ...
- js 实现图片瀑布流效果,可更改配置参数 带完整版解析代码[waterFall.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS实现图片瀑布流效果 页面需求 1 ...
- 尤雨溪的vue怎么学,应该从vue-cli开始,为什么?
带手机验证码登陆, 带全套购物车系统 带数据库 前后端分离开发 带定位用户功能 数据库代码为本地制作好了 带支付宝支付系统 带django开发服务器接口教程 地址: https://www.dua ...
- 实战经验:CentOS 7.3安装完整开发环境
系统版本 CentOS 7.3(1611) 安装开发环境 1) 通过group安装 # yum groups mark install “Development Tools” # yum groups ...
- ubuntu16 安装redis
ubuntu16 安装redis并开机自启 1.redis-3.2.5.tar.gz解压到/usr/local下 tar -xvf redis-3.2.5.tar.gz 2.进入源码包/usr/loc ...