python开发扫雷游戏作弊器 python扫雷游戏代码可复制不用pygame
off999 2024-12-28 14:43 19 浏览 0 评论
自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式。
一、准备工作
1.扫雷游戏
我是win10,没有默认的扫雷,所以去扫雷网下载
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 = 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
#扫描雷区图像
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,如果没有符合条件的,则随机点击一个白块
#插旗
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语言,运行你的第一行代码
-
#01安装Python访问Python官方(https://www.python.org/),下载并安装最新版本的Python。确保安装过程中勾选“Addpython.exetoPAT...
- Python推导式家族深度解析:字典/集合/生成器的艺术
-
一、为什么需要其他推导式?当你在处理数据时:o需要快速去重→集合推导式o要建立键值映射→字典推导式o处理海量数据→生成器表达式这些场景是列表推导式无法完美解决的,就像工具箱需要不同工...
- 别再用循环创建字典了!Python推导式让你的代码起飞
-
当同事还在用for循环吭哧吭哧创建字典时,我早已用推导式完成3个需求了!这个被90%新手忽视的语法,今天让你彻底掌握字典推导式的4大高阶玩法,文末彩蛋教你用1行代码搞定复杂数据转换!基础语法拆解#传...
- 什么是Python中的生成器推导式?(python生成器的好处)
-
编程派微信号:codingpy本文作者为NedBatchelder,是一名资深Python工程师,目前就职于在线教育网站Edx。文中蓝色下划线部分可“阅读原文”后点击。Python中有一种紧凑的语法...
- Python 列表转换为字符串:实用指南
-
为什么在Python中将列表转换为字符串?Python列表非常灵活,但它们并非在所有地方都适用。有时你需要以人类可读的格式呈现数据——比如在UI中显示标签或将项目保存到CSV文件。可能还...
- 生成器表达式和列表推导式(生成器表达式的计算结果)
-
迭代器的输出有两个很常见的使用方式,1)对每一个元素执行操作,2)选择一个符合条件的元素子集。比如,给定一个字符串列表,你可能想去掉每个字符串尾部的空白字符,或是选出所有包含给定子串的字符串。列表...
- python学习——038python中for循环VS列表推导式
-
在Python中,for循环和列表推导式(ListComprehension)都可以用于创建和处理列表,但它们的语法、性能和适用场景有所不同。以下是两者的详细对比:1.语法结构for循环使用...
- python中列表推导式怎么用?(列表 python)
-
这个问题,我们不妨用近期很火的ChatGPT来试试,来看看人工智能是如何解答的?在Python中,列表解析是一种简洁的方法,用于生成列表。它是一种快速,简洁的方法,可以在一行代码中生成列表,而不需...
- Python列表推导式:让你的代码优雅如诗!
-
每次写for循环都要三四行代码?处理数据时总被嵌套结构绕晕?学会列表推导式,一行代码就能让代码简洁十倍!今天带你解锁这个Python程序员装(偷)逼(懒)神器!一、为什么你需要列表推导式?代码...
- python学习——038如何将for循环改写成列表推导式
-
在Python里,列表推导式是一种能够简洁生成列表的表达式,可用于替换普通的for循环。下面是列表推导式的基本语法和常见应用场景。基本语法result=[]foriteminite...
- 太牛了!Python 列表推导式,超级总结!这分析总结也太到位了!
-
Python列表推导式,超级总结!一、基本概念列表推导式是Python中创建列表的一种简洁语法,它允许你在一行代码内生成列表,替代传统的for循环方式。其核心思想是**"对可迭代对...
- 25-2-Python网络编程-TCP 编程示例
-
2-TCP编程示例应用程序通常通过“套接字”(socket)向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通信。Python语言提供了两种访问网络服务的功能。其中低级别的网络服...
- python编程的基础与进阶(周兴富)(python编程基础视频)
-
前不久我发文:《懂了,if__name=='__main__'》。想不到的是,这个被朋友称之为“读晕了”的文章,其收藏量数百,有效阅读量竟然过万。所谓“有效阅读量”,就是读到尾部才退...
- Python 闭包:深入理解函数式编程的核心概念
-
一、简介在Python编程领域,闭包(Closure)是一个既基础又强大的概念,它不仅是装饰器、回调函数等高级特性的实现基础,更是函数式编程思想的重要体现。理解闭包的工作原理,能够帮助开发者编写出...
- Python小白逆袭!7天吃透PyQt6,独立开发超酷桌面应用
-
PythonGUI编程:PyQt6从入门到实战的全面指南在Python的庞大生态系统中,PyQt6作为一款强大的GUI(GraphicalUserInterface,图形用户界面)编程框架,为开...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)