auto.js蚂蚁森林收能量

  • 在网上看了一些自动收能量的脚本
  • 写了一个脚本
  • 使用AutoJs运行
    • AutoJS下载地址 :here
  • 运行步骤如下:

    • 1.解锁(点亮屏幕,切换到输入密码界面,输入密码)
    • 2.打开支付宝
    • 3.找到蚂蚁森林
    • 4.收取自己的能量
    • 5.偷取他人能量
  • 参考资料:

  • 源码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299

    var morningTime = "07:22";//自己运动能量生成时间
    var startTime = "07:00";
    var endTime = "7:35";
    var screen_width = 1080; //设置屏幕的宽度,像素值
    var screen_height = 2340; //设置屏幕的高度,像素值

    sleep(2000);
    unlock();
    sleep(2000);

    threads.start(function(){
    //在子线程中调用observeKey()从而使按键事件处理在子线程执行
    events.observeKey();
    events.on("key_down", function(keyCode, events){
    //音量键关闭脚本
    if(keyCode == keys.volume_down){
    toast("您选择退出脚本!")
    sleep(2000);
    exit();
    }
    });
    });

    mainEntrence();

    //程序主入口
    function mainEntrence(){

    do{
    //尝试打开支付宝
    if (!openAlipay()) {
    toastLog("打开支付宝失败,退出程序");
    exit();
    }
    // 尝试进入自己的蚂蚁森林,若失败跳过
    if(!enterMyMainPage()){
    toastLog("尝试进入自己的蚂蚁森林,若失败退出程序");
    exit();
    }
    if(!collectEnergy("收取自己能量中")){
    toastLog("如果不在蚂蚁森林,退出程序");
    exit();
    }
    // 找能量
    while(findOthers()){
    back();
    sleep(300);
    }
    // 执行返回 4 次
    whenComplete(4);
    // 睡眠 1 秒,等待下一次收集
    sleep(1000);
    }while(checkTime());

    exit();
    }
    /*
    * 收其他人能量
    * return 是否有能量可收
    */
    function findOthers(){

    if(textEndsWith("种树").exists()){
    // 到了自己能量时间
    if(myEnergyTime()){
    collectEnergy("收取自己能量");
    }
    // 点击按钮寻找能量,不同手机需要更改位置参数
    click(960,1570);
    sleep(500);
    return collectEnergy("收其他人能量中");
    }
    toastLog("没有能量了");
    return false;
    }

    /**
    * 解锁函数
    */
    function unlock(){
    if(!device.isScreenOn()){
    //点亮屏幕
    device.wakeUp();
    //由于MIUI的解锁有变速检测,因此要点开时间以进入密码界面
    sleep(1000);
    swipe(500, 0, 500, 1900, 2000);
    click(100,150);
    //输入屏幕解锁密码,其他密码请自行修改
    sleep(2000);
    click(250,1440);
    sleep(500);

    click(550,1640);
    sleep(500);

    click(250,1440);
    sleep(500);

    click(840,1640);
    sleep(500);
    }
    }


    /**
    * 收能量函数
    * return 是否收取成功
    */
    function collectEnergy(info) {
    // 判断是否在蚂蚁森林
    if(!textContains("最新动态").exists()){
    return false;
    }
    for(var row=screen_height*0.256;row<screen_height*0.376;row+=80){
    for(var col=screen_width*0.185;col<screen_width*0.815;col+=80){
    sleep(100);
    click(col,row);
    }
    }
    toastLog(info);
    sleep(100);
    return true;
    }

    /**
    * 从支付宝主页进入蚂蚁森林我的主页
    * return 是否进入
    */
    function enterMyMainPage(){
    //五次尝试蚂蚁森林入
    var i=0;
    // 拉至顶端
    swipe(screen_width*0.5,screen_height*0.5,screen_width*0.5,screen_height*0.25,500);
    sleep(500);
    swipe(screen_width*0.5,screen_height*0.25,screen_width*0.5,screen_height*0.5,500);

    while (!textEndsWith("蚂蚁森林").exists() && !descEndsWith("蚂蚁森林").exists() && i<=5){
    sleep(1000);
    i++;
    }
    if(i>=5){
    toastLog("没有找到蚂蚁森林入口,尝试中");
    clickByTextDesc("全部",0);
    sleep(2000);
    swipe(screen_width*0.5,screen_height*0.3,screen_width*0.5,screen_height*0.7,1000);
    sleep(2000);
    swipe(screen_width*0.5,screen_height*0.3,screen_width*0.5,screen_height*0.7,1000);
    sleep(2000);
    }
    clickByTextDesc("蚂蚁森林",0);

    //等待进入自己的主页,10次尝试
    sleep(3000);
    i=0;
    while (!textEndsWith("种树").exists() && !descEndsWith("种树").exists() && i<=10){
    sleep(1000);
    i++;
    }
    toastLog("第"+i+"次尝试进入自己主页");
    if(i>=10){
    toastLog("进入自己能量主页失败");
    return false;
    }
    return true;
    }
    /**
    * 自定义的点击函数
    * @param {*} energyType
    * @param {*} paddingY
    */
    function clickByTextDesc(energyType,paddingY){
    var clicked = false;
    if(descEndsWith(energyType).exists()){
    descEndsWith(energyType).find().forEach(function(pos){
    var posb=pos.bounds();
    if(posb.centerX()<0 || posb.centerY()-paddingY<0){
    return false;
    }
    //toastLog(pos.id());
    var str = pos.id();
    if(str != null){
    if(str.search("search") == -1){
    click(posb.centerX(),posb.centerY()-paddingY);
    //toastLog("get it 1");
    clicked = true;
    }
    }else{
    click(posb.centerX(),posb.centerY()-paddingY);
    //toastLog("get it 2");
    clicked = true;
    }
    sleep(100);
    });
    }

    if(textEndsWith(energyType).exists() && clicked == false){
    textEndsWith(energyType).find().forEach(function(pos){
    var posb=pos.bounds();
    if(posb.centerX()<0 || posb.centerY()-paddingY<0){
    return false;
    }
    //toastLog(pos.id());
    var str = pos.id();
    if(str != null){
    if(str.search("search") == -1){
    click(posb.centerX(),posb.centerY()-paddingY);
    //toastLog("get it 3");
    clicked = true;
    }
    }else{
    click(posb.centerX(),posb.centerY()-paddingY);
    //toastLog("get it 4");
    clicked = true;
    }
    sleep(100);
    });
    }

    return clicked;
    }

    /**
    * 结束后返回主页面
    * @param value : 返回次数
    */
    function whenComplete(value) {
    toastLog("结束");
    while(value>0){
    back();
    sleep(1500);
    value--;
    }
    }

    /**
    * 检测当前时间是否在指定范围内
    * return 是:否
    */
    function checkTime(){
    var now =new Date();
    var hour=now.getHours();
    var minu=now.getMinutes();
    var time_a=startTime.split(":");
    var time_b=endTime.split(":");
    var timea = 60*Number(time_a[0])+Number(time_a[1]);
    var timeb = 60*Number(time_b[0])+Number(time_b[1]);
    var time = 60*hour + minu;
    if(time>=timea && time<=timeb){
    //sleep(2000);
    return true;
    }else{
    return false;
    }
    }

    /**
    * 是否到了收自己能量的时间
    */
    function myEnergyTime(){
    var now =new Date();
    var hour=now.getHours();
    var minu=now.getMinutes();
    var mytime=morningTime.split(":");

    if(mytime[0]==hour && (mytime[1]==minu || mytime[1]==minu-1) ){
    return true;
    }else{
    return false;
    }
    }

    /**
    * 唤醒支付宝
    * return 是否成功
    */
    function openAlipay(){
    launchApp("支付宝");
    toastLog("等待支付宝启动");
    // sleep(3000);
    var i=0;

    while (!textEndsWith("扫一扫").exists() && !descEndsWith("扫一扫").exists() && i<=5){
    sleep(2000);
    // 点击首页,防止进入后在其他菜单
    clickByTextDesc("首页",0);
    i++;
    }
    toastLog("第"+i+"次尝试进入支付宝主页");
    if(i>=5){
    toastLog("没有找到支付宝首页");
    sleep(1000);
    return false;
    }
    return true;
    }