2025年12月13日星期六

蚂蚁森林自动收能量任务脚本

1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/Bg6shY若失效,可用地址

1.购买服务器

阿里云:

服务器购买地址

https://t.aliyun.com/U/Bg6shY

若失效,可用地址

https://www.aliyun.com/daily-act/ecs/activity_selection?source=5176.29345612&userCode=49hts92d

腾讯云:

https://curl.qcloud.com/wJpWmSfU

若失效,可用地址

https://cloud.tencent.com/act/cps/redirect?redirect=2446&cps_key=ad201ee2ef3b771157f72ee5464b1fea&from=console

华为云

https://activity.huaweicloud.com/cps.html?fromacct=64b5cf7cc11b4840bb4ed2ea0b2f4468&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905

2.部署教程

2024年最新青龙面板跑脚本教程(一)持续更新中

3.代码如下

import cv2import numpy as npimport uiautomator2 as u2import timeimport settings
class AntForestAutomation:    def __init__(self):        # 连接手机 (如果只连接了一台,可以使用默认连接)        try:            # self.d = u2.connect("172.16.20.66:5555")  # WIFI 调试            self.d = u2.connect()  # USB 调试            print(f"设备连接成功: {self.d.info.get('marketingName')}")        except Exception as e:            print(f"连接失败,请检查USB调试是否打开: {e}")            exit()
        # ================= 配置区域 (请根据实际手机修改) =================
        # 变量1: 九宫格坐标 (格式: {数字: (x, y)})        # 请通过"开发者选项"->"指针位置"获取你手机的具体坐标        self.grid_coords = settings.grid_coords
        # 变量2: 密码顺序列表        self.password_seq = settings.password_seq
        # ==============================================================
    def wake_and_unlock(self):        """点亮屏幕并解锁"""        if self.d.info['screenOn']:            print("屏幕已解锁, 无需唤醒...")            return        print("正在唤醒屏幕...")        self.d.screen_on()  # 点亮屏幕        time.sleep(1)
        # 检查是否需要上滑显示密码盘 (很多手机亮屏后需要上滑)        # 这里模拟一个从下往上的滑动        w, h = self.d.window_size()        self.d.swipe(w * 0.5, h * 0.9, w * 0.5, h * 0.40.1)        time.sleep(1)
        print(f"开始输入图案密码: {self.password_seq}")        try:            # 将数字序列转换为坐标列表            points = [self.grid_coords[num] for num in self.password_seq]            # 执行手势滑动,0.2秒完成            self.d.swipe_points(points, 0.2)            time.sleep(2)            print("解锁完成")        except Exception as e:            print(f"解锁过程出错: {e}")
    def find_btn_coord(self, btn):        """        :param btn:        find : 找能量按钮        harvest: 一键收按钮        :return: (x, y) coord or None        """        # 2. 配置参数(替换为你的模板图片路径)        if btn not in ["find""harvest"]:            print(f"btn 参数错误, 预期值 find / harvest, 实际值 {btn}")            return        template_path = f"template/{btn}.png"
        threshold = 0.8  # 匹配阈值,0-1之间
        # 3. 截取屏幕并转换格式(PIL → OpenCV)        screen = self.d.screenshot()  # 截取屏幕,返回PIL.Image对象        screen_cv = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2BGR)  # 转OpenCV格式
        # 4. 读取模板图片并匹配        template = cv2.imread(template_path)        h, w = template.shape[:2]        result = cv2.matchTemplate(screen_cv, template, cv2.TM_CCOEFF_NORMED)        _, max_val, _, max_loc = cv2.minMaxLoc(result)
        # 5. 匹配成功则点击中心位置        if max_val >= threshold:            center_x = max_loc[0] + w / 2            center_y = max_loc[1] + h / 2            return center_x, center_y        else:            print(f"匹配失败,匹配值:{max_val} < 阈值:{threshold}")            return
    def enter_ant_forest(self):        """打开支付宝并进入蚂蚁森林"""        print("启动支付宝...")        self.d.app_start("com.eg.android.AlipayGphone")        time.sleep(2)  # 等待启动
        # 尝试点击"蚂蚁森林"        # 策略:先在首页找,找不到可能需要点击"全部"或者已经在森林里        print("正在寻找蚂蚁森林入口...")
        # 如果当前不在森林界面,点击首页的图标        if not self.d(text="种树").exists:            forest_icon = self.d(text="蚂蚁森林")            if forest_icon.exists:                forest_icon.click()            else:                print("未在首页找到蚂蚁森林入口,请确保它在首页可见位置")                return False
        time.sleep(2)  # 等待森林加载        return True
    def collect_energy_loop(self):        """循环收能量"""        print("开始收集能量循环...")        count = 0        # 循环检查"找能量"按钮        # 支付宝机制:点击"找能量"会跳转到好友森林,或者右上角有"找能量"跳转下一个        while True:            count += 1            # 查找页面上是否有"找能量"按钮 (通常在右上角或者列表中)            # 注意:不同版本支付宝文案可能不同,这里以"找能量"为例            find_energy_btn = self.find_btn_coord(btn="find")
            if find_energy_btn:                print(f"点击 '找能量' 跳转第 {count} 好友...")                self.d.click(find_energy_btn[0], find_energy_btn[1])                time.sleep(1)  # 等待进入好友页面
                # 检查是否有"一键收" (通常是背包道具) 或者直接点击能量球                # 这里响应你的需求:点击"一键收"                one_click_btn = self.find_btn_coord(btn="harvest")
                if one_click_btn:                    print(f"发现 '一键收',点击收取!")                    self.d.click(one_click_btn[0], one_click_btn[1])                    time.sleep(0.1)                else:                    # 如果没有一键收,可能需要手动点球(此处代码可扩展)                    # 现在的版本通常会自动收,或者需要点击一个个球                    # 简单处理:尝试点击明显的能量球(需要更复杂的图像识别,这里略过)                    print("未发现一键收,跳过...")            else:                print("未找到 '找能量' 按钮,任务结束。")                break
    def lock_screen(self):        """锁屏"""        self.d.app_stop("com.eg.android.AlipayGphone")        print("执行锁屏...")        # 回到桌面 (可选)        self.d.press("home")        time.sleep(1)        # 关闭屏幕        self.d.screen_off()
    def run(self):        self.wake_and_unlock()        if self.enter_ant_forest():            self.collect_energy_loop()        self.lock_screen()

if __name__ == "__main__":    task = AntForestAutomation()    task.run()
解析

该脚本是一个 安卓手机端的蚂蚁森林自动收能量脚本,核心流程是:

  1. 通过 uiautomator2 连接手机(USB 调试)。

  2. 点亮并解锁手机(图案解锁)

  3. 自动打开 支付宝 → 进入蚂蚁森林

  4. 通过 截图 + OpenCV 模板匹配,识别:

    • "找能量"按钮;

    • "一键收"按钮;

  5. 循环点击「找能量 → 一键收」,遍历好友收取能量。

  6. 收完后 退出支付宝并锁屏

解锁用到的九宫格坐标和解锁顺序,从 settings.grid_coords 和 settings.password_seq 中读取,方便按你自己手机适配。

主要方法

1. __init__(self)

  • 负责:

    • grid_coords:九宫格每个点的屏幕坐标;

    • password_seq:图案解锁的数字顺序。

    • 连接手机设备:self.d = u2.connect()

    • 从 settings 中读取:

  • 简单来说:建立设备连接 + 加载解锁配置

2. wake_and_unlock(self)

  • 功能:点亮屏幕并通过"图案密码"自动解锁手机

  • 逻辑:

    1. 判断屏幕是否已点亮,已亮则直接返回。

    2. 未亮则 screen_on() 点亮;

    3. 上滑一下(从下往上)唤出解锁界面;

    4. 按 password_seq 对应的 grid_coords 生成一串坐标点;

    5. 调用 swipe_points 一笔划完图案解锁。

3. find_btn_coord(self, btn)

  • 功能:用模板匹配找某个按钮在屏幕上的坐标

  • 参数:

    • "find" → 找能量按钮模板 template/find.png

    • "harvest" → 一键收按钮模板 template/harvest.png

    • btn"find" 或 "harvest"

  • 逻辑:

    1. 当前屏幕截图 → 转成 OpenCV 格式;

    2. 读取对应模板图;

    3. cv2.matchTemplate 做模板匹配;

    4. 如果最大匹配度 ≥ 阈值 0.8,返回按钮中心坐标 (x, y)

    5. 否则打印"匹配失败",返回 None

一句话:截图 → 用 OpenCV 找按钮图标 → 返回可点击的坐标

4. enter_ant_forest(self)

  • 功能:启动支付宝,并进入蚂蚁森林页面

  • 逻辑:

    • 先找 text="蚂蚁森林" 的入口;

    • 找到就点击进入;

    • 找不到则提示"首页没找到入口",返回 False

    1. 调用 app_start("com.eg.android.AlipayGphone") 启动支付宝;

    2. 如果当前界面上没有 text="种树"(说明不在森林页面):

    3. 等待一会儿森林加载,返回 True

5. collect_energy_loop(self)

  • 功能:循环在好友之间跳转,并收取能量

  • 核心逻辑:

    while True:
        找 "找能量" 按钮 → 点击 → 进好友森林
        找 "一键收" 按钮 → 找到就点击收取
        找不到 "一键收" → 打印"跳过"
        如果一开始就找不到 "找能量" → 打印结束并 break
  • 关键点:

    • find_btn_coord("find"):找"找能量"按钮;

    • find_btn_coord("harvest"):找"一键收"按钮;

    • 每次点击后都会 sleep 一点时间,给页面切换缓冲。

一句话:不停点击"找能量",在每个好友森林里尝试按一次"一键收"。

6. lock_screen(self)

  • 功能:结束后清理和锁屏

  • 操作:

    1. app_stop("com.eg.android.AlipayGphone") 关闭支付宝;

    2. 返回桌面 press("home")

    3. screen_off() 熄屏锁定。

7. run(self)

  • 功能:脚本整体流程入口

  • 顺序:

    • 成功 → collect_energy_loop()

    1. wake_and_unlock() → 解锁手机;

    2. enter_ant_forest() → 进入蚂蚁森林;

    3. 最后 lock_screen() → 退出并锁屏。

8. if __name__ == "__main__":

  • 创建 AntForestAutomation 实例,并调用 run() 执行上述完整流程。


注意

本文部分变量已做脱敏处理,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。技术层面需要提供帮助,可以通过打赏的方式进行探讨。


历史脚本txt文件获取>>
服务器搭建,人工服务咨询>>

没有评论:

发表评论

蚂蚁森林自动收能量任务脚本

1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/Bg6shY若失效,可用地址 1.购买服务器 阿里云: 服务器购买地址 https : //t.aliyun.com/U/Bg6shY 若失效,可用地址 https ://www.aliyun....