import aircv as ac
import uiautomator2 as u2
import time
import random
d=u2.connect('172.21.236.63')
def match(imsrc,imobj):
result=ac.find_template(imsrc,imobj,0.7)
if(result!=None):
return result['result'][0],result['result'][1]
return None
def click(img):
d.screenshot("a.jpg")
imsrc=ac.imread("C:\\Users\\bj\\Desktop\\a.jpg")
imobj=ac.imread(img)
if(match(imsrc,imobj)!=None):
x,y=match(imsrc,imobj)
print(x,y)
#d.screenshot("a.jpg")
try:
d.long_click(x,y,0.5)
except Exception as e:
pass
def random_xy():
x=random.uniform(0.1, 0.8)
y=random.uniform(0.1, 0.8)
z=random.uniform(0.1, 0.8)
w=random.uniform(0.1, 0.8)
d.drag(x,y,z,w,0.3)
while(True):
time.sleep(1)
click("C:\\Users\\bj\\Desktop\\serch.jpg")
time.sleep(2)
click("C:\\Users\\bj\\Desktop\\search.jpg")
time.sleep(4)
click("C:\\Users\\bj\\Desktop\\c.jpg")
time.sleep(2)
imsrc=ac.imread("C:\\Users\\bj\\Desktop\\a.jpg")
imobj=ac.imread("C:\\Users\\bj\\Desktop\\e.jpg")
if(match(imsrc,imobj)!=None):
click("C:\\Users\\bj\\Desktop\\e.jpg")
else:
time.sleep(3)
d.long_click(0.5,0.3,0.3)
random_xy()
代码中a.jpg,c.jpg,e.jpg,serch.jpg,search.jpg分别代表屏幕截图、资源采集的图片、出征的图片和搜索的图片
本程序的核心代码是采用aircv模块的找图find_template()函数
import aircv as ac
import uiautomator2 as u2
import time
import random
d=u2.connect('172.21.236.66')
def match(imsrc,imobj):
result=ac.find_template(imsrc,imobj,0.6)
if(result!=None):
return result['result'][0],result['result'][1]
return None
def random_xy():
x=random.uniform(0.1, 0.8)
y=random.uniform(0.1, 0.8)
z=random.uniform(0.1, 0.8)
w=random.uniform(0.1, 0.8)
d.drag(x,y,z,w,0.3)
def fight():
d.screenshot("a.jpg")
imsrc=ac.imread("C:\\Users\\bj\\Desktop\\a.jpg")
imobj=ac.imread("C:\\Users\\bj\\Desktop\\e.jpg")
if(match(imsrc,imobj)!=None):
x,y=match(imsrc,imobj)
return x,y
else:
return None
def get_re(x,y):
d.long_click(x,y,0.5)
time.sleep(2)
d.screenshot("a.jpg")
#print(1)
imsrc=ac.imread("C:\\Users\\bj\\Desktop\\a.jpg")
imobj=ac.imread("C:\\Users\\bj\\Desktop\\c.jpg")
if(match(imsrc,imobj)!=None):
z,w=match(imsrc,imobj)
#print(z,w)
d.long_click(z,w,0.5)
if(fight()!=None):
k,l=fight()
d.long_click(k,l,0.5)
else:
d.long_click(0.5,0.2,0.5)
random_xy()
def find_resource(re):
d.screenshot("a.jpg")
imsrc=ac.imread("C:\\Users\\bj\\Desktop\\a.jpg")
imobj=ac.imread("C:\\Users\\bj\\Desktop\\"+re+".jpg")
if(match(imsrc,imobj)!=None):
x,y=match(imsrc,imobj)
get_re(x,y)
else:
random_xy()
while(True):
find_resource("you")#寻找石油