百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

python开发扫雷游戏作弊器 python扫雷游戏代码可复制不用pygame

off999 2024-12-28 14:43 32 浏览 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()

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

相关推荐

阿里云国际站ECS:阿里云ECS如何提高网站的访问速度?

TG:@yunlaoda360引言:速度即体验,速度即业务在当今数字化的世界中,网站的访问速度已成为决定用户体验、用户留存乃至业务转化率的关键因素。页面加载每延迟一秒,都可能导致用户流失和收入损失。对...

高流量大并发Linux TCP性能调优_linux 高并发网络编程

其实主要是手里面的跑openvpn服务器。因为并没有明文禁p2p(哎……想想那么多流量好像不跑点p2p也跑不完),所以造成有的时候如果有比较多人跑BT的话,会造成VPN速度急剧下降。本文所面对的情况为...

性能测试100集(12)性能指标资源使用率

在性能测试中,资源使用率是评估系统硬件效率的关键指标,主要包括以下四类:#性能测试##性能压测策略##软件测试#1.CPU使用率定义:CPU处理任务的时间占比,计算公式为1-空闲时间/总...

Linux 服务器常见的性能调优_linux高性能服务端编程

一、Linux服务器性能调优第一步——先搞懂“看什么”很多人刚接触Linux性能调优时,总想着直接改配置,其实第一步该是“看清楚问题”。就像医生看病要先听诊,调优前得先知道服务器“哪里...

Nginx性能优化实战:手把手教你提升10倍性能!

关注△mikechen△,十余年BAT架构经验倾囊相授!Nginx是大型架构而核心,下面我重点详解Nginx性能@mikechen文章来源:mikechen.cc1.worker_processe...

高并发场景下,Spring Cloud Gateway如何抗住百万QPS?

关注△mikechen△,十余年BAT架构经验倾囊相授!大家好,我是mikechen。高并发场景下网关作为流量的入口非常重要,下面我重点详解SpringCloudGateway如何抗住百万性能@m...

Kubernetes 高并发处理实战(可落地案例 + 源码)

目标场景:对外提供HTTPAPI的微服务在短时间内收到大量请求(例如每秒数千至数万RPS),要求系统可弹性扩容、限流降级、缓存减压、稳定运行并能自动恢复。总体思路(多层防护):边缘层:云LB...

高并发场景下,Nginx如何扛住千万级请求?

Nginx是大型架构的必备中间件,下面我重点详解Nginx如何实现高并发@mikechen文章来源:mikechen.cc事件驱动模型Nginx采用事件驱动模型,这是Nginx高并发性能的基石。传统...

Spring Boot+Vue全栈开发实战,中文版高清PDF资源

SpringBoot+Vue全栈开发实战,中文高清PDF资源,需要的可以私我:)SpringBoot致力于简化开发配置并为企业级开发提供一系列非业务性功能,而Vue则采用数据驱动视图的方式将程序...

Docker-基础操作_docker基础实战教程二

一、镜像1、从仓库获取镜像搜索镜像:dockersearchimage_name搜索结果过滤:是否官方:dockersearch--filter="is-offical=true...

你有空吗?跟我一起搭个服务器好不好?

来人人都是产品经理【起点学院】,BAT实战派产品总监手把手系统带你学产品、学运营。昨天闲的没事的时候,随手翻了翻写过的文章,发现一个很严重的问题。就是大多数时间我都在滔滔不绝的讲理论,却很少有涉及动手...

部署你自己的 SaaS_saas如何部署

部署你自己的VPNOpenVPN——功能齐全的开源VPN解决方案。(DigitalOcean教程)dockovpn.io—无状态OpenVPNdockerized服务器,不需要持久存储。...

Docker Compose_dockercompose安装

DockerCompose概述DockerCompose是一个用来定义和管理多容器应用的工具,通过一个docker-compose.yml文件,用YAML格式描述服务、网络、卷等内容,...

京东T7架构师推出的电子版SpringBoot,从构建小系统到架构大系统

前言:Java的各种开发框架发展了很多年,影响了一代又一代的程序员,现在无论是程序员,还是架构师,使用这些开发框架都面临着两方面的挑战。一方面是要快速开发出系统,这就要求使用的开发框架尽量简单,无论...

Kubernetes (k8s) 入门学习指南_k8s kubeproxy

Kubernetes(k8s)入门学习指南一、什么是Kubernetes?为什么需要它?Kubernetes(k8s)是一个开源的容器编排系统,用于自动化部署、扩展和管理容器化应用程序。它...

取消回复欢迎 发表评论: