auto(); //无障碍权限
setScreenMetrics(1080, 1920); //设置所适合的屏幕宽高
requestScreenCapture(); //请求截图

//console.show();

function 截图() {
while (true) {
if (图 = captureScreen()) {
return 图;
break;
}
}
} //获取截图,返回图片对象

function save(path, txt) {
try {
files.append(path, txt);
return true;
} catch (e) {
return false;
}
}

function windowGXY(x, y, k) {
if (x < k[0]) {
x = k[0]
};
if (k[2] < x) {
x = k[2]
};
if (y < k[1]) {
y = k[1]
};
if (k[3] < y) {
y = k[3]
};
return {
x: x,
y: y
};
}

toast("123");
launchApp("王者荣耀");
sleep(2000);

var tuzu = [
[1384, 884, 450, 174, "/img_下一步.jpg"],
[1278, 826, 372, 174, "/img_闯关.jpg"],
[1664, 0, 252, 168, "/img_自动关.jpg"],
[514, 928, 894, 152, "/img_点屏.jpg"],
[1110, 900, 348, 162, "/img_返回.jpg"]
];

// [1658,0,258,172,"img_自动开.jpg",1787,43],
//

var 跳线, 确线;
跳线 = threads.start(跳);
确线 = threads.start(确);
threads.start(function(){
while (true) {
for (var i = 0; i < tuzu.length; i++) {
sleep(2000);
if (i == 2) {
sleep(5000)
};
var IMG = 截图();
var img = images.read(files.join(files.path("./img"), tuzu[i][4]));
var X = Math.round(img.getWidth() / 2),
Y = Math.round(img.getHeight() / 2);
var point = findImage(IMG, img, {
region: tuzu[i],
threshold: 0.8
});
if (point) {
var x = parseInt(point.x) + X,
y = parseInt(point.y) + Y;
toast(i);
click(x, y);
//press(x, y, 250);
} else {

    }

}

}
});
//save("./abc.js","var tuzu=[\n["+str.join("],\n[")+"]\n];\n");

function 跳() {
var tu = [1658, 0, 258, 144, "/img_跳过.jpg", 1787, 69];
while (true) {
sleep(2000);
var IMG = 截图();
var img = images.read(files.join(files.path("./img"), tu[4]));
var X = Math.round(img.getWidth() / 2),
Y = Math.round(img.getHeight() / 2);
var point = findImage(IMG, img, {
region: [tu[0], tu[1], tu[2], tu[3]],
threshold: 0.8
});
if (point) {
var x = parseInt(point.x) + X,
y = parseInt(point.y) + Y;
toast(tu[4] + "\nX: " + x + "Y: " + y);
click(x, y);
//press(x, y, 250);

    } else {
        i--;
    }

}

}

function 确() {
var tu = [808, 860, 306, 162, "/img_确定.jpg", 961, 941];
while (true) {
sleep(2000);
var IMG = 截图();
var img = images.read(files.join(files.path("./img"), tu[4]));
var X = Math.round(img.getWidth() / 2),
Y = Math.round(img.getHeight() / 2);
var point = findImage(IMG, img, {
region: [tu[0], tu[1], tu[2], tu[3]],
threshold: 0.8
});
if (point) {
var x = parseInt(point.x) + X,
y = parseInt(point.y) + Y;
toast(tu[4] + "\nX: " + x + "Y: " + y);
click(x, y);
//press(x, y, 250);

    } else {
        i--;
    }

}

}