import os
import time
from compare import compare #上一段代码我存为了compare.py
class Ldconsole: #请根据自己软件的路径来
console = r'F:\leidian\LDPlayer9\dnconsole.exe '
ld = r'F:\leidian\LDPlayer9\ld.exe'
adb = r'F:\leidian\LDPlayer9\adb.exe'
#这个类其实不用写的(个人意见),参照于http://t.****img.cn/rkTDh,下面使用ld和console的也是基于此博客
def launch(self,index: int): #启动函数
cmd = Ldconsole.console + 'launch --index ' + str(index)
#可以去雷电官方论坛看console和ld的使用格式和函数用途
process = os.popen(cmd) #os是python向cmd发送指令的库
result = process.read() #结果读取
process.close()
return result #结果显示
def app_star(self,index: int, package: str):
#启动app,等会函数内填的是应用名,自行查app调用名(例如:com.bilibili.umamusu(赛马娘,启动!),应该是和安装包同名)
cmd = Ldconsole.console + 'runapp --index %d --packagename %s' % (index, package)
process = os.popen(cmd)
result = process.read()
process.close()
print(result)
return result
def adb_start(self,index: int): #启动adb的函数,这段里面注释掉的是本来应该写在这,但是不注释就报错,但为了纪念我去查资料的时间,所以我留着了ᕕ(◠ڼ◠)ᕗ
#cmd1 ='adb kill-server' 本来在启动要结束之前启动的adb的
#process = os.popen(cmd1)
#result = process.read()
#process.close()
#print(result)
#return result
cmd2 = 'adb start-server'
process = os.popen(cmd2)
process.close()
cmd3 = Ldconsole.adb + ' connect 127.0.0.1:5555' #连接到雷电模拟器的端口号,第一次应该连接不上(我也不知为什么,第一次会自动连接到 emulator-5554),cmd可查此时连接上adb的模拟器的端口号,自行学习
process = os.popen(cmd3)
process.close()
#return result
def tap(self, a, b):#用于点击的函数
cmd = Ldconsole.adb + ' -s emulator-5554 shell input tap {x} {y}'.format(x=a,y=b)
process = os.popen(cmd)
result = process.read()
process.close()
print(result)
return result
def screencap(self,index: int,str): #截屏
cmd = (Ldconsole.adb +
' -s emulator-5554 shell /system/bin/screencap -p /sdcard/{name}.png'.format(name=str))
process = os.popen(cmd)
result = process.read()
process.close()
print(result)
return result
def screenget(self,index: int,str): #将截屏存放到F:\python\pythonProject(这是我的路径,记得自行更改)
cmd = (Ldconsole.adb +
' -s emulator-5554 pull /sdcard/{name}.png F:\python\pythonProject'.format(name=str))
process = os.popen(cmd)
result = process.read()
process.close()
print(result)
return result
def kill_self(self,index: int): #写到这我才发现还是写了个结束进程的函数。。
cmd ='adb kill-server'
process = os.popen(cmd)
result = process.read()
process.close()
print(result)
return result
def main():
a = Ldconsole()# 打开模拟器
a.tap(646, 1524)
a.tap(646, 1524)
a.launch(0) #这里很多延时是因为响应速度问题,删除可能导致某一步没执行
time.sleep(4)
a.kill_self(0)
time.sleep(4)
a.adb_start(0)
time.sleep(4)
#time.sleep(3)
a.app_star(0,'com.bilibili.umamusu')
#time.sleep(10)
#a.tap(590,250) #启动应用的坐标
time.sleep(18)
a.screencap(0,'screengap')
time.sleep(1)
a.screenget(0,'screengap')
time.sleep(2)
x=compare(0,0,0,0,0,"enter") #基于compare.py见上一段
if x>0.82:
a.tap(590,250)
time.sleep(30)
a.screencap(0,'screengap')
time.sleep(2)
a.screenget(0,'screengap')
x=compare(0,0,0,0,0,"mainscreen")
if x >= 0.82:
competition()
else:
print("error")
exit(0)
#a.kill_self(0)
#a.quit(0)
if __name__ == '__main__':
main()
其他:
Python - 控制雷电模拟器(Dnconsole)_python控制雷电模拟器-****博客