脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 利用Python实现自动扫雷小脚本

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

2021-08-14 01:0151CTO Python

这篇文章主要介绍了利用Python实现自动扫雷小脚本,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

一、准备工作

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.找到游戏窗口与坐标

  1. #扫雷游戏窗口
  2. class_name = "TMain"
  3. title_name = "Minesweeper Arbiter "
  4. hwnd = win32gui.FindWindow(class_name, title_name)
  5. #窗口坐标
  6. left = 0
  7. top = 0
  8. right = 0
  9. bottom = 0
  10. if hwnd:
  11. print("找到窗口")
  12. left, top, right, bottom = win32gui.GetWindowRect(hwnd)
  13. #win32gui.SetForegroundWindow(hwnd)
  14. print("窗口坐标:")
  15. print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom))
  16. else:
  17. print("未找到窗口")

2.锁定并抓取雷区图像

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

3.各图像的RGBA值

  1. #数字1-8 周围雷数
  2. #0 未被打开
  3. #ed 被打开 空白
  4. #hongqi 红旗
  5. #boom 普通雷
  6. #boom_red 踩中的雷
  7. rgba_ed = [(225, (192, 192, 192)), (31, (128, 128, 128))]
  8. rgba_hongqi = [(54, (255, 255, 255)), (17, (255, 0, 0)), (109, (192, 192, 192)), (54, (128, 128, 128)), (22, (0, 0, 0))]
  9. rgba_0 = [(54, (255, 255, 255)), (148, (192, 192, 192)), (54, (128, 128, 128))]
  10. rgba_1 = [(185, (192, 192, 192)), (31, (128, 128, 128)), (40, (0, 0, 255))]
  11. rgba_2 = [(160, (192, 192, 192)), (31, (128, 128, 128)), (65, (0, 128, 0))]
  12. rgba_3 = [(62, (255, 0, 0)), (163, (192, 192, 192)), (31, (128, 128, 128))]
  13. rgba_4 = [(169, (192, 192, 192)), (31, (128, 128, 128)), (56, (0, 0, 128))]
  14. rgba_5 = [(70, (128, 0, 0)), (155, (192, 192, 192)), (31, (128, 128, 128))]
  15. rgba_6 = [(153, (192, 192, 192)), (31, (128, 128, 128)), (72, (0, 128, 128))]
  16. rgba_8 = [(149, (192, 192, 192)), (107, (128, 128, 128))]
  17. rgba_boom = [(4, (255, 255, 255)), (144, (192, 192, 192)), (31, (128, 128, 128)), (77, (0, 0, 0))]
  18. rgba_boom_red = [(4, (255, 255, 255)), (144, (255, 0, 0)), (31, (128, 128, 128)), (77, (0, 0, 0))]

4.扫描雷区图像保存至一个二维数组map

  1. #扫描雷区图像
  2. def showmap():
  3. img = ImageGrab.grab().crop(rect)
  4. for y in range(blocks_y):
  5. for x in range(blocks_x):
  6. this_image = img.crop((x * block_width, y * block_height, (x + 1) * block_width, (y + 1) * block_height))
  7. if this_image.getcolors() == rgba_0:
  8. map[y][x] = 0
  9. elif this_image.getcolors() == rgba_1:
  10. map[y][x] = 1
  11. elif this_image.getcolors() == rgba_2:
  12. map[y][x] = 2
  13. elif this_image.getcolors() == rgba_3:
  14. map[y][x] = 3
  15. elif this_image.getcolors() == rgba_4:
  16. map[y][x] = 4
  17. elif this_image.getcolors() == rgba_5:
  18. map[y][x] = 5
  19. elif this_image.getcolors() == rgba_6:
  20. map[y][x] = 6
  21. elif this_image.getcolors() == rgba_8:
  22. map[y][x] = 8
  23. elif this_image.getcolors() == rgba_ed:
  24. map[y][x] = -1
  25. elif this_image.getcolors() == rgba_hongqi:
  26. map[y][x] = -4
  27. elif this_image.getcolors() == rgba_boom or this_image.getcolors() == rgba_boom_red:
  28. global gameover
  29. gameover = 1
  30. break
  31. #sys.exit(0)
  32. else:
  33. print("无法识别图像")
  34. print("坐标")
  35. print((y,x))
  36. print("颜色")
  37. print(this_image.getcolors())
  38. sys.exit(0)
  39. #print(map)

5.扫雷算法

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

1.首先点出一个点

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

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

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

  1. #插旗
  2. def banner():
  3. showmap()
  4. for y in range(blocks_y):
  5. for x in range(blocks_x):
  6. if 1 <= map[y][x] and map[y][x] <= 5:
  7. boom_number = map[y][x]
  8. block_white = 0
  9. block_qi = 0
  10. for yy in range(y-1,y+2):
  11. for xx in range(x-1,x+2):
  12. if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:
  13. if not (yy == y and xx == x):if map[yy][xx] == 0:
  14. block_white += 1
  15. elif map[yy][xx] == -4:
  16. block_qi += 1if boom_number == block_white + block_qi:for yy in range(y - 1, y + 2):
  17. for xx in range(x - 1, x + 2):
  18. if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:
  19. if not (yy == y and xx == x):
  20. if map[yy][xx] == 0:
  21. win32api.SetCursorPos([left+xx*block_width, top+yy*block_height])
  22. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
  23. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
  24. showmap()
  25. #点击白块
  26. def dig():
  27. showmap()
  28. iscluck = 0
  29. for y in range(blocks_y):
  30. for x in range(blocks_x):
  31. if 1 <= map[y][x] and map[y][x] <= 5:
  32. boom_number = map[y][x]
  33. block_white = 0
  34. block_qi = 0
  35. for yy in range(y - 1, y + 2):
  36. for xx in range(x - 1, x + 2):
  37. if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:
  38. if not (yy == y and xx == x):
  39. if map[yy][xx] == 0:
  40. block_white += 1
  41. elif map[yy][xx] == -4:
  42. block_qi += 1if boom_number == block_qi and block_white > 0:for yy in range(y - 1, y + 2):
  43. for xx in range(x - 1, x + 2):
  44. if 0 <= yy and 0 <= xx and yy < blocks_y and xx < blocks_x:
  45. if not(yy == y and xx == x):
  46. if map[yy][xx] == 0:
  47. win32api.SetCursorPos([left + xx * block_width, top + yy * block_height])
  48. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
  49. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
  50. iscluck = 1
  51. if iscluck == 0:
  52. luck()
  53. #随机点击
  54. def luck():
  55. fl = 1
  56. while(fl):
  57. randomrandom_x = random.randint(0, blocks_x - 1)
  58. randomrandom_y = random.randint(0, blocks_y - 1)
  59. if(map[random_y][random_x] == 0):
  60. win32api.SetCursorPos([left + random_x * block_width, top + random_y * block_height])
  61. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
  62. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
  63. fl = 0
  64. def gogo():
  65. win32api.SetCursorPos([left, top])
  66. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
  67. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
  68. showmap()
  69. global gameover
  70. while(1):
  71. if(gameover == 0):
  72. banner()
  73. banner()
  74. dig()
  75. else:
  76. gameover = 0
  77. win32api.keybd_event(113, 0, 0, 0)
  78. win32api.SetCursorPos([left, top])
  79. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
  80. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
  81. showmap()

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

原文链接:http://developer.51cto.com/art/202012/635397.htm

延伸 · 阅读

精彩推荐