热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python自动扫雷_python实战教程之自动扫雷

1.找到游戏窗口与坐标#扫雷游戏窗口class_nameTMaintitle_nameMinesweeperArbiterhwndwin32gui.FindWindow(c

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 <&#61; map[y][x] and map[y][x] <&#61; 5:

boom_number &#61; map[y][x]

block_white &#61; 0

block_qi &#61; 0

for yy in range(y-1,y&#43;2):

for xx in range(x-1,x&#43;2):

if 0 <&#61; yy and 0 <&#61; xx and yy

if not (yy &#61;&#61; y and xx &#61;&#61; x):if map[yy][xx] &#61;&#61; 0:

block_white &#43;&#61; 1

elif map[yy][xx] &#61;&#61; -4:

block_qi &#43;&#61; 1if boom_number &#61;&#61; block_white &#43; block_qi:for yy in range(y - 1, y &#43; 2):

for xx in range(x - 1, x &#43; 2):

if 0 <&#61; yy and 0 <&#61; xx and yy

if not (yy &#61;&#61; y and xx &#61;&#61; x):

if map[yy][xx] &#61;&#61; 0:

win32api.SetCursorPos([left&#43;xx*block_width, top&#43;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 &#61; 0

for y in range(blocks_y):

for x in range(blocks_x):

if 1 <&#61; map[y][x] and map[y][x] <&#61; 5:

boom_number &#61; map[y][x]

block_white &#61; 0

block_qi &#61; 0

for yy in range(y - 1, y &#43; 2):

for xx in range(x - 1, x &#43; 2):

if 0 <&#61; yy and 0 <&#61; xx and yy

if not (yy &#61;&#61; y and xx &#61;&#61; x):

if map[yy][xx] &#61;&#61; 0:

block_white &#43;&#61; 1

elif map[yy][xx] &#61;&#61; -4:

block_qi &#43;&#61; 1if boom_number &#61;&#61; block_qi and block_white > 0:for yy in range(y - 1, y &#43; 2):

for xx in range(x - 1, x &#43; 2):

if 0 <&#61; yy and 0 <&#61; xx and yy

if not(yy &#61;&#61; y and xx &#61;&#61; x):

if map[yy][xx] &#61;&#61; 0:

win32api.SetCursorPos([left &#43; xx * block_width, top &#43; yy * block_height])

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

iscluck &#61; 1

if iscluck &#61;&#61; 0:

luck()

#随机点击

def luck():

fl &#61; 1

while(fl):

random_x &#61; random.randint(0, blocks_x - 1)

random_y &#61; random.randint(0, blocks_y - 1)

if(map[random_y][random_x] &#61;&#61; 0):

win32api.SetCursorPos([left &#43; random_x * block_width, top &#43; 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 &#61; 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 &#61;&#61; 0): banner() banner() dig() else: gameover &#61; 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

这个算法在初级和中级通过率都不错&#xff0c;但是在高级成功率惨不忍睹&#xff0c;主要是没有考虑逻辑组合以及白块是雷的概率问题&#xff0c;可以对这两个点进行改进&#xff0c;提高成功率



推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 20210304力扣 根据前序遍历和中序遍历确定二叉树 快速排序思想
    力扣根据前序遍历和中序遍历确定二叉树基本思路前序遍历确定根节点是哪个(第一个就是根节点)中序遍历根据已知根节点确定左右子树的元素组成根节点左左子树根节点右右子树再根据前序遍历确定左 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • EzPP 0.2发布,新增YAML布局渲染功能
    EzPP发布了0.2.1版本,新增了YAML布局渲染功能,可以将YAML文件渲染为图片,并且可以复用YAML作为模版,通过传递不同参数生成不同的图片。这个功能可以用于绘制Logo、封面或其他图片,让用户不需要安装或卸载Photoshop。文章还提供了一个入门例子,介绍了使用ezpp的基本渲染方法,以及如何使用canvas、text类元素、自定义字体等。 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
author-avatar
洪可婷60134
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有