python自动扫雷_利用Python实现自动扫雷
自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式。
一、准备工作
我的版本是 python 3.6.1
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 = 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
#python学习群592539176
#扫描雷区图像
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,如果没有符合条件的,则随机点击一个白块
#python学习群592539176
#插旗
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):
random_x = random.randint(0, blocks_x - 1)
random_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每天自动打印练习题
在日常教学工作中,我几乎每天都会给班上的每位同学打印一份口算练习题.为了防止出现抄袭的现象,给每位同学的练习题都不相同. 通过网上下载的一个小软件自动生成的练习题. 之前的操作是通过网上下载的软件,自 ...
- python京东自动签到_利用python Selenium实现自动登陆京东签到领金币功能
如何自动登陆京东? 我们先来看一下京东的登陆页面,如下图所示: [插入图片,登陆页面] 登陆框就是右面这一个框框了,但是目前我们遇到一个困呐,默认的登陆方式是扫码登陆,如果我们想要以用户民个.密码的形 ...
- python提醒事件_利用python实现短信和电话提醒功能的例子
有时候,我们需要程序帮我们自动检测某些事件的发生 这个需求是广泛存在的 因此,这里整理了利用python实现短信和电话提醒功能的方法 主要需要完成以下4个步骤: - 安装核心库:twilio - 注册 ...
- python发送邮箱_利用Python自动发送电子邮件
在利用Python进行发送邮件时主要借助smtplib和email两个模块,其中smtplib主要用来建立服务器链接.服务器断开的工作,而email模块主要用来设置一些与邮件本身相关的内容,比如收件人 ...
- 怎么用python编写记事本_利用Python开发实现简单的记事本
利用Python开发实现简单的记事本 最近想对 python 加深学习一下,同时也是想试着做一些东西,所以使用 python, 结合 Tkinter 来做一个简单的跨平台记事本.最终实现的记事本如下, ...
- python计算器程序_利用Python代码编写计算器小程序
1 importtkinter2 importtkinter.messagebox3 importmath4 classJSQ:5 6 7 def __init__(self):8 #创建主界面 9 ...
- python 取反_利用python怎么对bool布尔值进行取反
利用python怎么对bool布尔值进行取反 发布时间:2020-12-14 14:49:17 来源:亿速云 阅读:71 这期内容当中小编将会给大家带来有关利用python怎么对bool布尔值进行取反 ...
- python selenium截图_利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)...
对WebElement截图 WebDriver.Chrome自带的方法只能对当前窗口截屏,且不能指定特定元素.若是需要截取特定元素或是窗口超过了一屏,就只能另辟蹊径了. WebDriver.Phant ...
- python接口 同花顺_利用python探索股票市场数据指南
虽然同花顺之类的金融理财应用的数据足够好了,但还是有自己定制的冲动, 数据自然不会不会比前者好很多,但是按照自己的想法来定制还是不错的. 目标 通过免费的数据接口获取数据,每日增量更新标的历史交易数据 ...
- python高斯求和_利用Python进行数据分析(3)- 列表、元组、字典、集合
本文主要是对Python的数据结构进行了一个总结,常见的数据结构包含:列表list.元组tuple.字典dict和集合set. image 索引 左边0开始,右边-1开始 通过index()函数查看索 ...
最新文章
- 关于微信手机端IOS系统中input输入框无法输入的问题
- AppleScript
- 二维均匀分布的边缘密度函数_理解概率密度函数
- ES6的新特性(5)——数值的扩展
- 然爸读书笔记(2013-3)----用户体验的要素
- java-weixin-tools接入微信
- Python解析JSON对象
- visual studio 2015 无法打开源文件“stdafx.h“
- 一键搭建kms激活服务器
- Python+tkinter应用程序设置背景图片
- STM32工具使用---STM32 ST-Link Utility
- markdown 语法大全
- php滑动拼图验证,如何在PHP环境下实现滑动拼图验证
- CSS表格与浮动定位
- 什么是 JavaBeans ?
- 知识图谱入门学习笔记(二)-知识表示
- p光圈和dc光圈的区别_什么是光圈?
- 你做的网页在哪些浏览器测试过,这些浏览器的内核分别是什么?
- Mvvm中的Lifecycle
- 3 基于matplotlib的python数据可视化——导入Excel数据绘制组合图表
热门文章
- 研究生,导师不是你的保姆……
- 肠·道 | 朱元方:产检消毒恐误伤菌脉,6大举措则促菌脉相承
- python中使用squarify包可视化treemap图:treemap将分层数据显示为一组嵌套矩形,每一组都用一个矩形表示,该矩形的面积与其值成正比、自定义设置每一个数据格的颜色
- R语言计算平均值的标准误差(standard error of the mean):自定义函数计算平均值的标准误差、使用plotrix包的std.error函数计算平均值的标准误差
- R语言ggplot2可视化交互作用图(Interaction Plot):可视化不同分组(分类变量1)在不同剂量下(分类变量2)的箱图(box plot)、均值计算并连接成线图(line plot)
- pandas使用groupby.last函数获取每个组中的最后一个值实战:groupby.last函数获取每个组中的最后一个值、groupby.nth函数获取每个组中的最后一个值
- R语言对数正态分布分布函数Log Normal Distribution(dlnorm, plnorm, qlnorm rlnorm)实战
- 机器学习类别/标称(categorical)数据处理:序号编码(Ordinal Encoding)
- MLNG_机器学习的动机与应用
- 新版IntelliJ IDEA Web项目配置完整流程