# -*- coding: utf-8 -*-
import random
import time
import wda
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image
# 截图距离 * time_coefficient = 按键时长
# time_coefficient: iphonex: 0.00125; iphone6: 0.00196; iphone6s plus: 0.00120;
time_coefficient = 0.00196
VERSION = "1.1.4"
c = wda.Client()
s = c.session()
def pull_screenshot():
c.screenshot('autojump.png')
fig = plt.figure() # 返回Figure(640x480)像素640x480的一个画图屏幕
pull_screenshot() # 手机截图
img = np.array(Image.open('autojump.png')) # 生成一个截图的数组
im = plt.imshow(img, animated=True) # 将截图在plt图片上准备显示 im== AxesImage(80,52.8;496x369.6)
# 定义几个全局变量
update = True
click_count = 0
cor = []
def jump(distance):
press_time = distance * time_coefficient
# 此处位置可变
a = random.random()*200+100
b = random.random()*200+100
s.tap_hold(a, b, press_time)
def update_data():
return np.array(Image.open('autojump.png'))
def updatefig(*args):
global update
if update:
time.sleep(1)
pull_screenshot() # 如果update值为true(第一次执行updatefig和每次jump后),间隔一秒,再截图一次
im.set_array(update_data()) # 更新数据一次img,# 将截图在plt图片上显示出来
update = False
return im,
def on_click(event):
global update # 更新值为true
global ix, iy
global click_count # 点击数为0
global cor # 这个变量为[]
ix, iy = event.xdata, event.ydata # 点击的x和y
coords = [(ix, iy)]
print('now = ', coords) # now = [(111,222)]
cor.append(coords) # [[(111,222)]]
click_count += 1 # 1
if click_count > 1:
click_count = 0
cor1 = cor.pop()
cor2 = cor.pop()
distance = (cor1[0][0] - cor2[0][0])**2 + (cor1[0][1] - cor2[0][1])**2
distance = distance ** 0.5
print('distance = ', distance)
jump(distance)
update = True
fig.canvas.mpl_connect('button_press_event', on_click) # 将图片与点击事件与点击后的函数联系起来
ani = animation.FuncAnimation(fig, updatefig, interval=1000, blit=True) # 每过一个interval的毫秒数就执行一次updatefig,不阻塞后面的语句。
plt.show() # 这个show执行了以后,后面的语句不再执行
home.png
- 相关函数解析
plt.figure()==>点击查看
wda.Client.screenshot('autojump.png')==>点击查看
网友评论