蚂蚁森林自动收能量的最新脚本

环境必须是安卓7.0以上版本。中文界面。

安装软件,执行Auto.js代码。完成。注意自启动,锁定后台,无障碍服务,悬浮窗等权限允许。

注意需要把脚本和图片放到执行文件夹的同一目录下。

首先所有资料链接如下:

https://pan.baidu.com/s/1XJTHl1IASTGmDZGkTCGJ0g

对网络上的代码下载,修改后,代码如下:

/**
* @Title: 蚂蚁森林
* @Description:蚂蚁森林自动收取能量和偷取能量
* @date: 2019-2-23 
*/

sleep(1000);
mainEntrence();

//解锁
function unlock(){
    if(!device.isScreenOn()){
        //点亮屏幕
        device.wakeUp();
        sleep(1000);
        //滑动屏幕到输入密码界面
        swipe(520,1560,520,100,1000);
        sleep(1000);
        
       // click(200,1000);
        sleep(500);
       //swipe(520,1560,520,100,1000);
       // click(200,1000);
        sleep(500);
       // click(200,1000);
        sleep(500);
        swipe(520,1560,520,100,1000);
       // click(200,1000);
        sleep(500);
    
    }
}
/**
 * 日志输出
 */
function tLog(msg) {
    toast(msg);
    console.log(msg)
}
 
/**
 * 获取权限和设置参数
 */
function prepareThings(){
    unlock();
    setScreenMetrics(1080, 1920);
    //请求截图
   if(!requestScreenCapture()){
        tLog("请求截图失败");
        exit();
    }
    
}
/**
 * 设置按键监听 当脚本执行时候按音量减 退出脚本
 */
function registEvent() {
    //启用按键监听
    events.observeKey();
    //监听音量上键按下
    events.onKeyDown("volume_down", function(event){
        tLog("脚本手动退出");
        exit();
    });
}

/**
 * 默认程序出错提示操作
 */
function defaultException() {
    tLog("程序当前所处状态不合预期,脚本退出");
    exit();
}
/**
 * 等待加载收集能量页面,采用未找到指定组件阻塞的方式,等待页面加载完成
 */
function waitPage(type){
    // 等待进入自己的能量主页
    if(type==0){
        desc("种树").findOne();
    }
    // 等待进入他人的能量主页
    else if(type==1){
        desc("浇水").findOne();
    }
    //再次容错处理
    sleep(1000);
}
/**
 * 从支付宝主页进入蚂蚁森林我的主页
 */
function enterMyMainPage(){
    launchApp("支付宝");
    tLog("等待支付宝启动");
    var i=0;
    sleep(1000);
    //五次尝试蚂蚁森林入口
    while (!textEndsWith("蚂蚁森林").exists() && i<=5){
        sleep(2000);
        i++;
    }
    clickByText("蚂蚁森林",true,"请把蚂蚁森林入口添加到主页我的应用");
    //等待进入自己的主页
    tLog("蚂蚁森林启动完成");
    waitPage(0);
    tLog("蚂蚁森林启动结束");
}
/**
 * 进入排行榜
 */
function enterRank(){
    tLog("寻找排行榜");
    swipe(520,1860,520,100,2000);
    sleep(500);
    swipe(520,1860,520,100,2000);
    sleep(500);

    clickByDesc("查看更多好友",0,true,"程序未找到排行榜入口,脚本退出");
    var i=0;
    //等待排行榜主页出现
    tLog("等待排行榜");
    sleep(500);
    while (!textEndsWith("排行榜").exists() && i<=5){
        sleep(2000);
        i++;
    }
    tLog("等待结束");
    if(i>=5){
        tLog("等待结束!");
        defaultException();
    }
}
/**
 * 从排行榜获取可收集好友的点击位置
 * @returns {*}
 */
function  getHasEnergyfriend(type) {
    //var img = getCaptureImg();
    var p=null;
    if(type==1){
        //img 是图片
        //"#1DA06D" 第一个颜色
        //[0, 33, "#1DA06D"] 第二颜色和它的相对坐标
        //[34,45, "#ffffff"] 第三个颜色和他的相对坐标
        //region: [1030, 100, 1, 1700] 第一个颜色的检测区域1030,100为起始坐标,1,1700为区域宽度!!!
        //p = images.findMultiColors(img, "#1DA06D",[[20, 0, "#1DA06D"], [21,11, "#ffffff"]]);
        //p = images.findMultiColors(img, "#1DA06D",[[20, 0, "#1DA06D"], [48,45, "#ffffff"]]);
        var picture = images.read("./hand.jpg");
        
        var cap=captureScreen();
        sleep(1000);
        
        var p = findImage(cap, picture,{ threshold: 0.5 });
        
        tLog("OK");
    }
    if(p!=null){
        return p;
    }else {
        return null;
    }
}
/**
 * 判断是否好有排行榜已经结束
 * @returns {boolean}
 */
function isRankEnd() {
    if(descEndsWith("没有更多了").exists()){
        var b=descEndsWith("没有更多了").findOne();
        var bs=b.bounds();
        if(bs.centerY()<1920){
            return true;
        }
    }
    return false;
}
/**
 * 在排行榜页面,循环查找可收集好友
 * @returns {boolean}
 */
function enterOthers(){
    tLog("开始检查排行榜");
    var i=1;
    var ePoint=getHasEnergyfriend(1);
    
    //确保当前操作是在排行榜界面
    while(ePoint==null && textEndsWith("好友排行榜").exists()){
        tLog("未找到可取,向下滑");
         //滑动排行榜 root方式的的点击调用.如无root权限,7.0及其以上可采用无障碍模式的相关函数
        sleep(1000);
        swipe(520,1800,520,520,500);
        sleep(1000);
        i++;
        ePoint=getHasEnergyfriend(1);
        //检测是否排行榜结束了
        if(isRankEnd()){
            return false;
        }
        //如果连续32次都未检测到可收集好友,无论如何停止查找(由于程序控制了在排行榜界面,且判断了结束标记,基本已经不存在这种情况了)
        else if(i>32){
            tLog("程序可能出错,连续"+i+"次未检测到可收集好友");
            exit();
        }
    }
    if(ePoint!=null){
        
        //点击位置相对找图后的修正
        tLog("找到可取");
        click(ePoint.x,ePoint.y+20);
        waitPage(1);

        while(1){
            var img = captureScreen();
            toastLog("开始找色");
            //0x1d75b3为编辑器默认主题蓝色字体(if, var等关键字)的颜色
            //找到颜色与0x1d75b3完全相等的颜色
            var point = findColorEquals(img, 0xCBFE5C);
            if(point){
                toastLog("x = " + point.x + ", y = " + point.y);
                click(point.x+30,point.y+30);
            }else{
                toastLog("没有找到");
                break;
            }            
        }
        //进去收集完后,递归调用enterOthers
        back();
        //swipe(520,1800,520,1300,1000);
        sleep(500);
        var j=0;
        //等待返回好有排行榜
        if(!textEndsWith("好友排行榜").exists() && j<=5){
            sleep(2000);
            j++;
        }
        if(j>=5){
            defaultException();
        }
        enterOthers();
    }else{
        defaultException();
    }

}
/**
 * 根据描述值 点击
 * @param energyType
 * @param noFindExit
 */
function clickByDesc(energyType,paddingY,noFindExit,exceptionMsg){
    if(descEndsWith(energyType).exists()){
        descEndsWith(energyType).find().forEach(function(pos){
            var posb=pos.bounds();
            click(posb.centerX(),posb.centerY()-paddingY);
            sleep(2000);
        });
    }else{
        if(noFindExit!=null && noFindExit){
            if(exceptionMsg !=null){
                tLog(exceptionMsg);
                exit();
            }else{
                defaultException();
            }
        }
    }
}

/**
 * 根据text值 点击 * @param energyType * @param noFindExit
 */
function clickByText(energyType,noFindExit,exceptionMsg){
    if(textEndsWith(energyType).exists()){
        textEndsWith(energyType).find().forEach(function(pos){
            var posb=pos.bounds();
            click(posb.centerX(),posb.centerY()-60);
        });
    }else{
        if(noFindExit!=null && noFindExit){
            if(exceptionMsg !=null){
                tLog(exceptionMsg);
                exit();
            }else{
                defaultException();
            }
        }
    }
}
/**
 * 遍历能量类型,收集自己的能量
 */
function collectionMyEnergy(){
    while(1){
        var img = captureScreen();
        toastLog("开始找色");
        //0x1d75b3为编辑器默认主题蓝色字体(if, var等关键字)的颜色
        //找到颜色与0x1d75b3完全相等的颜色
        var point = findColorEquals(img, 0xCBFE5C);
        if(point){
            toastLog("x = " + point.x + ", y = " + point.y);
            click(point.x+30,point.y+30);
        }else{
            toastLog("没有找到");
            break;
        }        
    }
    tLog("自己能量收集完成");
    sleep(1000);
}
/**
 * 结束后返回主页面
 */
function whenComplete() {
    tLog("结束");
    back();
    sleep(1500);
    back();
    sleep(1000);
    home();
    exit();
}
//程序主入口
function mainEntrence(){
    //前置操作
    prepareThings();
    //注册音量下按下退出脚本监听
    registEvent();
    //从主页进入蚂蚁森林主页
    enterMyMainPage();
    //收集自己的能量
    collectionMyEnergy();
    //进入排行榜
    enterRank();
    //在排行榜检测是否有好有的能量可以收集
    enterOthers();
    //结束后返回主页面
    whenComplete();
}

原理简单易懂。

通过方法唤醒屏幕,滑动或者解锁等,然后启动支付宝,

查询到蚂蚁森林字段后确认启动完成,然后点击那里。

打开后找到能量球的颜色,点击,直到该颜色消失。

向下滑动找到排行榜字段。点击排行榜。

打开寻找有没有手的图片在当前屏幕,有可以收集的好友就点击进去。找到能量球的颜色然后点击,没有就退出。

没有找到手的图片就往下滑动,继续检查可收集能量的好友。

通过查找“没有更多了”之类的字段,确定收集结束的标准。

最后返回,返回,主页。完成整个过程。

软件可以定时启动,所以有可能定时收取能量。但是要注意是否可以深度唤醒,后台是否运行了软件,是否无障碍服务正常开启等等。。。

文档详细容易操作。

完。

猜你喜欢

转载自www.cnblogs.com/bai2018/p/10425937.html