> 文档中心 > 蚂蚁森林收集能量之AutoJs实现(精度优化版)

蚂蚁森林收集能量之AutoJs实现(精度优化版)

蚂蚁森林收集能量之AutoJs实现(精度优化版)

  • 前言
  • 一、先上效果图
  • 二、脚本思路
  • 三、具体实现(2022.3.27更新)
  • 总结

前言

  它来了,它来了,你被偷的能量回来了!
  在5月份的时候,我写了一篇AutoJs去拯救能量的文章(这是链接),转眼间已经五个月过去了,之前的拯救能量脚本只能保证在前10秒可以很好的点击能量,但是在最后5秒的时候就会疯狂错位,无法精准点击。因为我也不是一个沉迷偷能量的人,所以那个问题从我上篇文章发出去后就一直没管,但最近实在闲着无聊,点开了CSDN的数据发现,这篇文章的阅读量一直有在涨,偶尔也会涨个粉丝,于是今天我又打开了这个脚本,给拯救能量的最后5秒进行了一个调试,经过我的不(xia)懈(ji)努(ba)力(gao),终于这个脚本最后5秒的精准度也有了不错的提升,但由于最近双十一,拯救能量时会掉落一些喵糖,偶尔会挡到能量的收取,导致能量收取率不能稳定达到100%,但起码也是有95%以上的。
  话不多说,直接看效果!前面收集普通能量的时候,做了加速处理,后面拯救能量是正常速度,不然时间太长了。
本文链接:https://blog.csdn.net/weixin_44337681/article/details/121062515


一、先上效果图

我点我点我点点点点点!

二、脚本思路

  本次更新只是优化了拯救能量最后5秒的点击逻辑,其实就是根据点击次数的增加,去增加了一个偏移量,让后面的能量也能准确收取,其他逻辑没有改变,感兴趣的朋友可以看我前面的两篇文章(蚂蚁森林自动收能量之AutoJs实现)、(蚂蚁森林拯救能量之AutoJs实现)

三、具体实现(2022.3.27更新)

  2022.3.27:针对蚂蚁森林页面更新做了调整,现在已经可以正常收集能量了,评论区说不能用的同学可以重新试试。
  代码里会有一些关于坐标的测算(没啥卵用),但是只基于我的p30pro,其他手机也许会有些差异,下面的代码可以直接运行,之前有人反馈过进入蚂蚁森林的逻辑在他的手机不能正确运行,如果也有人出现这种情况的话,可能需要你们根据你们支付宝的布局去进行一些小的调整,后面的收集能量应该是不受影响的。

 main();    function main(){ console.log("等待获取无障碍权限"); auto.waitFor(); console.log("申请截屏权限"); requestScreenCapturePermision(); delay(random(0.5,1)); // rescueEnergy(); console.log("打开支付宝"); launchApp("支付宝"); var search; while (!(search = findViewByClassAndText("Button","搜索"))); console.log("打开蚂蚁森林"); randomDelayClick(1,2,search); while (!(search = findViewByClassAndDesc("TextView","清空"))); randomDelayClick(1,2,search); while (!(search = findViewByClassAndText("TextView","搜索"))); delay(random(1,2)); search.parent().parent().child(1).child(2).setText("蚂蚁森林"); randomDelayClick(1,2,search); while (!(search = findViewByClassAndText("TextView","蚂蚁森林,为你在荒漠种下一棵真树"))); randomDelayClick(1,2,search); console.log("能量收割机"); while (!(findViewByClassAndText("Button","我的大树养成记录"))); delay(random(1,2)); energyHarvester();    }    /**     * 申请截屏权限 个别系统需要每次都申请截图权限 子线程自动允许     */    function requestScreenCapturePermision(){ threads.start(function(){     for (let i = 0; i < 100; i++) {  if (textExists("立即开始")) {      click("立即开始");      threads.currentThread().interrupt();  }     } }); if(!requestScreenCapture()){     toast("请允许截图权限后重试");     exit(); } captureScreen();    }    /**     * 循环获取能量 随机延迟是为了看起来更像人。。。     * 也许大家会有 为什么没有点击找能量代码的疑惑     * 是因为经过我的测试 #ffc2ff01 这个颜色 不仅在能量球上面有 而且找能量按钮也有这个颜色!     * 于是我只需要用一个找色循环就可以实现收取能量球与进入下一页面两个操作 是不是很牛呢? 嘻嘻嘻     */    function energyHarvester(){ var regionArray1 = [device.width * 0.14, 300, device.width * 0.74, 600], regionArray2 = [device.width * 0.75, 1300]; delay(random(0.2,0.5)); // if (clickPoint("#fffdd55b", regionArray1, "收集被好友复活能量 : ")) { //     var view; //     while (!(view = findViewByClassAndText("Button","立即收取"))) { //  console.log("view : " + view); //  randomDelayClick(1, 2, view); //     } //     delay(random(0.2,0.5)); // } while (true) {     while (true) {  delay(random(0.2,0.5));  if (clickPoint("#ffc2ff01", regionArray1, "收集能量 : ")) continue;  else break;     }     delay(1);     if (clickPoint("#fffe8600", regionArray2, "点击找能量,进入下个好友的蚂蚁森林 : ")) {  delay(2);  continue;     }     console.info("森林能量收集完毕");     break; } if (findViewByClassAndText("android.view.View","立即开启"))     rescueEnergy(); console.log("退出支付宝"); while (!(findViewByClassAndText("TextView","扫一扫"))) {     delayBack(1); } console.log("退出"); back();    }    function clickPoint(color, regionArray, logString) { image = captureScreen(); point = findColor(image, color, {threshold: 4,region : regionArray}); if (point) {     click(point.x,point.y);     console.log(logString+point);     return true; } return false;    }    /**     * 拯救消失的能量球     * 观察能量球下落过程  发现大致有五条下落轨道     * 首先截图能量拯救页面  测量能量球直径150px(不知道别的手机会不会不一样)     * 截图测量发现  能量球1s大约下落678px(1513-835)  就是1ms -> 0.67px* 在五条轨道的范围内进行区域找色     */    function rescueEnergy(){ console.info("开始拯救消失的绿色能量"); randomClickBounds(findViewByClassAndText("android.view.View","立即开启")); delay(3); var image, point, time, offset = 0; var regionArray = [100, 800, 900, 150]; var color = "#ff128900"; do {     image = captureScreen();     point = findColor(image, color,{threshold: 4,region : regionArray}); } while (!point); console.info("开始收取能量"); var errorCount = 0; while (errorCount < 30) {     time = Date.now();     image = captureScreen();     point = findColor(image, color,{region : regionArray});     if (point && !className("View").text("送TA机会").findOnce()) {  offset += 0.5;  errorCount = 0;  time = Date.now() - time;  click(point.x, (point.y + time + 70 + offset));  console.log("拯救能量 : "+point + " " + time + " " + offset);  continue;     }     errorCount++; } console.info("拯救能量结束");    }//↓↓↓ 下面是一些工具人方法 用来获取控件、点击、延时之类的    function findViewByClassAndId(name,viewId){ return className(name).id(viewId).findOne(1000);    }    function findViewByClassAndDesc(name,descStr){ return className(name).desc(descStr).findOne(1000);    }    function findViewByClassAndText(name,s){ return className(name).text(s).findOne(1000);    }    function randomDelayClick(t1,t2,view){ delay(random(t1,t2)); randomClickBounds(view);    }    function randomClickBounds(view){ if (view) {     bounds = view.bounds();     return click(random(bounds.left,bounds.right),random(bounds.top,bounds.bottom)); } console.log("randomClickBounds view == null"); return false;    }    function delay(seconds) { sleep(1000 * seconds);    }    function delayBack(seconds){ delay(seconds); back();    }    function textExists(str){ return textContains(str).exists();    }

总结

  总结就是没有总结,觉得不错就给小弟点个赞吧。未经允许,请勿转载。
本文链接:https://blog.csdn.net/weixin_44337681/article/details/121062515

88读书网