需求,当手机游戏战斗结束时的判断。因为游戏战斗时间不稳定,也不好固定时间touch,所以才有这个需求。
战斗结束后一般都会进入结算状态,结算状态一般都是固定不动的,所以,隔一段时间 进行截图对比,假如2张图片的相似度大于90%,说明是固定不动的,说明已经到了结算页面。然后返回True,假如小于90%,则返回False。通过判断True然后再执行下一步操作
具体代码实现
__author__ = "xiaoyun461"
from airtest.core.api import *
from airtest.aircv.aircv import *
from airtest.core.cv import *
from airtest.aircv.template import *
#获得屏幕宽高
def height_width():
global width
global height
if G.DEVICE.display_info['orientation'] in [1,3]:
height = G.DEVICE.display_info['width']
width = G.DEVICE.display_info['height']
else:
height = G.DEVICE.display_info['height']
width = G.DEVICE.display_info['width']
global half_num
half_num = (0.5*width,0.5*height)
return [height,width]
#每隔多少秒截图 对比相似度
def search_equal(sleep_num):
x_min =width/4
y_min =height/4
x_max = width/4*3
y_max = height/4*3
#截图到内存
screen_01 = G.DEVICE.snapshot()
sleep(sleep_num)
screen_02 = G.DEVICE.snapshot()
#裁剪图片
predict_screen_01 = aircv.crop_image(screen_01, (x_min, y_min, x_max, y_max))
predict_screen_02 = aircv.crop_image(screen_02, (x_min, y_min, x_max, y_max))
like_num = find_template(screen_01,screen_02,-1)['confidence']
print("当前相似度:" , like_num)
if like_num >0.9:
return True
else:
return False
#战斗业务逻辑
def win_exists():
#开始战斗
sleep(10)
#永动机,每隔5秒对比,当相似度大于90%退出当前循环
while True:
if search_equal(5):
break
#执行下接下来的业务逻辑
#初始化长宽
height_width()
#战斗
win_exists()