python实战教程之自动扫雷(自己存下来学习之用)
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 = 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("未找到窗口")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2.锁定并抓取雷区图像
#锁定雷区坐标#去除周围功能按钮以及多余的界面#具体的像素值是通过QQ的截图来判断的
left += 15
top += 101
right -= 15
bottom -= 42
#抓取雷区图像
rect = (left, top, right, bottom)
img = ImageGrab.grab().crop(rect)
1
2
3
4
5
6
7
8
9
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))]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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(http://www.my516.com)
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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
这个算法在初级和中级通过率都不错,但是在高级成功率惨不忍睹,主要是没有考虑逻辑组合以及白块是雷的概率问题,可以对这两个点进行改进,提高成功率
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
python实战教程之自动扫雷(自己存下来学习之用)的更多相关文章
- python实战教程之自动扫雷
1.找到游戏窗口与坐标 #扫雷游戏窗口class_name = "TMain"title_name = "Minesweeper Arbiter "hwnd = ...
- 1秒内通关扫雷?他创造属于自己的世界记录!Python实现自动扫雷
五一劳动节假期,我们一起来玩扫雷吧.用Python+OpenCV实现了自动扫雷,突破世界记录,我们先来看一下效果吧. 中级 - 0.74秒 3BV/S=60.81 相信许多人很早就知道有扫雷这么一款经 ...
- 给深度学习入门者的Python快速教程
给深度学习入门者的Python快速教程 基础篇 numpy和Matplotlib篇 本篇部分代码的下载地址: https://github.com/frombeijingwithlove/dlcv_f ...
- kafka实战教程(python操作kafka),kafka配置文件详解
kafka实战教程(python操作kafka),kafka配置文件详解 应用往Kafka写数据的原因有很多:用户行为分析.日志存储.异步通信等.多样化的使用场景带来了多样化的需求:消息是否能丢失?是 ...
- Python实战:Python爬虫学习教程,获取电影排行榜
Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...
- 利用Python实现自动扫雷
自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式. 一.准备工作 我的版本是 python 3.6.1python的第三方库:win ...
- 自动扫雷 python
自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式. // 2018.8.10更新 代码已上传至GitHub https://gith ...
- 给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV
这次博客园的排版彻底残了..高清版请移步: https://zhuanlan.zhihu.com/p/24425116 本篇是前面两篇教程: 给深度学习入门者的Python快速教程 - 基础篇 给深度 ...
- python 入门教程
转载自:http://www.crifan.com/files/doc/docbook/python_beginner_tutorial/release/html/python_beginner_tu ...
随机推荐
- 洛谷P1247取火柴游戏
题目:https://www.luogu.org/problemnew/show/P1247 可以知道必败局面为n[1]^n[2]^...^n[k]=x=0: 而若x不等于0,则一定可以取一次使其变为 ...
- 洛谷P1999三国游戏
题目:https://www.luogu.org/problemnew/show/P1199 仔细想想,两方都拿不到每个武将的配对中最大的: 本来想的是如果有武将a,b,对应最大ma,mb,次大ca, ...
- 使用lsyncd配置数据库备份多异地同步
lsyncd配置文件 settings { logfile = "/var/log/lsyncd.log", --日志路径 status = "/var/log/lsyn ...
- 折半插入排序 之通俗易懂,图文+代码详解-java编程
转自http://blog.csdn.net/nzfxx/article/details/51615439 1.特点及概念介绍 下面给大家讲解一下"二分法查找"这个java基础查找 ...
- JAVA基础--面向对象08
一.final关键字 有的时候不想别人重写我的方法,使用final关键字修饰该方法,final:最后的,最终的,可以修饰类.修饰函数.修饰变量,修饰类:该类不能被继承:直接写在class关键字前面 修 ...
- (水题)洛谷 - P1308 - 统计单词数
https://www.luogu.org/problemnew/show/P1308 简单哈希一下判断,练练手. 注意fgets()的用法,第一个参数传存储位置,第二个参数传内存上限,第三个传std ...
- 3DMAX 9 角色建模3 uv展开
将角色删除一半,展好uv再镜像出来,节省一半工作量(前提是对称) 添加UVW展开编辑器(Unwrap UVW),选择面 打开UV编辑器 这里要注意映射问题,默认打开UV编辑器后所选择的面是映射到与选择 ...
- bzoj 3365: [Usaco2004 Feb]Distance Statistics 路程统计【容斥原理+点分治】
统计在一个root下的两个子树,每个子树都和前面的运算一下再加进去对于这种需要排序的运算很麻烦,所以考虑先不去同子树内点对的算出合法点对个数,然后减去每一棵子树内的合法点对(它们实际上是不合法的,相当 ...
- 11.6NOIP模拟赛
[数据规模和限制] 对于全部测试数据,满足 N,M,K≤,W≤ 各个测试点的数据规模及特殊性质如下表. 测试点 N M K ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ 师 更多咨询:北京信息学窦老师 QQ ...
- 洛谷P4199 万径人踪灭(manacher+FFT)
传送门 题目所求为所有的不连续回文子序列个数,可以转化为回文子序列数-回文子串数 回文子串manacher跑一跑就行了,考虑怎么求回文子序列数 我们考虑,如果$S_i$是回文子序列的对称中心,那么只要 ...