胖头猫游戏提供热门游戏下载和手游攻略!

Python开发:制作扫雷游戏辅助工具教程

发布时间:2024-10-16浏览:69

各位老铁们,大家好,今天由我来为大家分享Python开发:制作扫雷游戏辅助工具教程,以及的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!

一、准备工作

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

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 = 0def 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写作弊神器!这款工具真厉害,轻松解锁所有雷区,终于不用像以前那样慢慢推测了。

    有6位网友表示赞同!

致命伤

这个Python开发的扫雷作弊器太酷炫了吧!我刚学习Python不久,看到这种应用场景真的超级兴奋,以后可以试试自己写个版本的辅助工具!

    有13位网友表示赞同!

素婉纤尘

对于喜欢玩扫雷的人来说,这确实是一个方便的功能,不用再费尽脑汁慢慢解谜,一键完成真是爽歪歪! 不过作为游戏爱好者,我还是建议大家坚持靠自己的能力挑战游戏乐趣。

    有9位网友表示赞同!

妄灸

虽然这作弊器很厉害,但我觉得用它会破坏扫雷的趣味性啊!玩它就应该感受那种逐步揭开未知的过程带来的成就感呀~

    有7位网友表示赞同!

又落空

Python开发的扫雷作弊器真的太赞了!之前总觉得扫雷这个游戏运气成分很大,有了这个工具就能完全掌控局势,我可是个高手了!

    有12位网友表示赞同!

熏染

有点担心这种作弊器的出现会不会影响到扫雷游戏的公平性啊?不过还是挺佩服作者的代码水平,能把Python应用在游戏辅助上确实很厉害!

    有17位网友表示赞同!

孤街浪途

哈哈, 这个Python开发的扫雷作弊器真是太有意思了!这下终于可以轻松打败我的电脑对手啦!不过还是要提醒一下小伙伴们,切勿滥用这工具哦~

    有17位网友表示赞同!

一样剩余

我觉得这种作弊器用起来可能会失去玩游戏的乐趣啊。玩扫雷应该是在未知中探索、推理的过程吧?如果一切都提前知道就太没意思了

    有10位网友表示赞同!

久爱不厌

看到有人提到了Python开发的扫雷作弊器,突然想起大学的时候自己学过一门算法课程…真怀念当年那个激情满满的状态!希望现在能有机会再重拾编程之旅。

    有11位网友表示赞同!

裸睡の鱼

很想知道这个 Python 开发的扫雷作弊器具体是如何实现的呢? 应该是用各种算法把雷的位置算出来吧? 感觉太神奇了!

    有11位网友表示赞同!

走过海棠暮

玩久了扫雷这种游戏,发现真的有点无聊 现在有了Python开发的作弊器,真是太好了! 可以解锁所有雷区,瞬间乐趣倍增!

    有9位网友表示赞同!

怪咖

其实我更喜欢挑战自己,靠自己的思维能力慢慢解开扫雷!用辅助工具感觉反而没有成就感呀。 不过还是要感谢作者分享这种技能啊,学习Python一直是我的梦想!

    有20位网友表示赞同!

╯念抹浅笑

这个Python开发的扫雷作弊器挺有用的啊!之前在玩扫雷的时候总输给电脑,现在有了它就能轻松战胜对手了!

    有11位网友表示赞同!

暮染轻纱

我觉得使用作弊器会让游戏变得没意思,失去原本的游戏初心。 还是希望大家都能享受游戏的乐趣,而不是依赖工具来获得胜利。

    有20位网友表示赞同!

来自火星球的我

如果有人用Python开发的扫雷作弊器在比赛中获胜,会不会被取消资格呢? 这样看来,这种作弊工具用的地方确实有限啊!

    有6位网友表示赞同!

她的风骚姿势我学不来

这 Python 开发的扫雷作弊器应该可以作为学习编程的案例研究吧? 通过观察代码结构和逻辑可以更好地理解Python语言的应用,真羡慕那些能写出这种高级功能程序的人!

    有14位网友表示赞同!

像从了良

我更喜欢用脑子思考游戏,而不是直接使用作弊器。 虽然这个 Python 开发的扫雷作弊器真的很厉害,但我更想享受挑战的过程!

    有13位网友表示赞同!

热点资讯