步骤一:下载源码及其abd驱动(电脑连接手机并操作手机的驱动)地址:快手跳一跳.zip - 蓝奏云 放置在简介中。
步骤二 :配置环境变量 如图所示
步骤三:手机打开开发者模式 连接手机
步骤四:运行代码
代码原理:鼠标点击第一次获得人物的起点坐标,点击第二次获取目的终点的坐标,然后通过勾股定理算出距离,从而估算出按压时间。我这里的系数比设置的是2.45,如果感觉不好用可以自己微调。
交流群:934724496,致力于it爱好者交流,欢迎大家加入。
import os
import random
import time
import cv2
sign=False
start_x=0
start_y=0
end_x=0
end_y=0
distance=0
def get_screenshot():
# 截取手机的屏幕
os.system('adb shell screencap -p /sdcard/01.png')
# 把模拟器里面的文件或文件夹传到电脑上
os.system('adb pull /sdcard/01.png d://python//picture')
def jump(distance):
# 设置按压时间,系数为1.35
press_time = int(distance * 2.45)
# 生成随机手机屏幕模拟触摸点,防止成绩无效
# 生成随机整数(0-9),最终数值为(0-90)
rand = random.randint(0, 9) * 10
# adb长按操作,即在手机屏幕上((320-410),(410-500))坐标处长按press_time毫秒
cmd = ('adb shell input swipe %i %i %i %i ' + str(press_time)) % (320 + rand, 410 + rand, 320 + rand, 410 + rand)
# 输出adb命令
print(cmd)
# 执行adb命令
os.system(cmd)
def get_point(event, x, y, flags, param):
# 鼠标单击事件
global sign
global start_x
global start_y
global end_x
global end_y
global distance
if event == cv2.EVENT_LBUTTONDOWN:
# 输出坐标
print('坐标值: ', x, y)
# 在传入参数图像上画出该点
#cv2.circle(param, (x, y), 1, (255, 255, 255), thickness=-1)
img = param.copy()
# 输出坐标点的像素值
print('像素值:',param[y][x]) # 注意此处反转,(纵,横,通道)
# 显示坐标与像素
text = "("+str(x)+','+str(y)+')'+str(param[y][x])
# 说明是第一次
if(sign ==False):
start_x=x
start_y=y
sign = True
else:
end_x=x
end_y=y
distance=((start_x-end_x)**2 +(start_y-end_y)**2)**0.5
print(distance)
jump(distance)
sign = False
if __name__ == "__main__":
# 定义两幅图像
font = cv2.FONT_HERSHEY_SIMPLEX
# 显示图像
while(True):
start_time = time.time()
get_screenshot()
image = cv2.imread('picture/01.png')
image=cv2.resize(image,(500,1020))
# image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# 定义两个窗口 并绑定事件 传入各自对应的参数
cv2.namedWindow('image')
cv2.setMouseCallback('image', get_point, image)
cv2.resizeWindow("image",500,1020)
cv2.putText(image, "FPS: "+ str(round(1.0 / (time.time() - start_time),1)), (50, 50), font, 1, (180, 100, 255), 2, cv2.LINE_AA)
cv2.imshow('image', image)
print("FPS: ", 1.0 / (time.time() - start_time)) # FPS = 1 / time to process loop
if cv2.waitKey(20) & 0xFF == 27:
break