python实现微信跳一跳

一、准备工具

  • adb(密码:p36h)
  • python3.6****(其余版本问题不大)
  • 安卓手机( 必须开启调试模式 )
  • 数据线

二、使用python获取手机的截屏

1、控制手机对当前屏幕进行截屏

直接在有adb.exe的文件夹里按住shift + 鼠标右键——>点击在次处打开命令窗口,这种方式不用配置全局命令,更加方便一些,如果要弄成全局命令的话,那么就可以百度,参考别人的经验,是非常全面的。

import os
os.system('adb shell screencap -p /sdcard/screen.png')#让手机截屏存储在手机的根目录

运行完了之后会在 手机根目录 会出现screen.png图片文件,表明截图成功

2、获取截屏到电脑的当前文件夹里

os.system('adb pull /sdcard/screen.png')#获取这个截图到当前电脑当前文件夹下

成功之后会在当前文件夹里面会有这个截图(可以看得到)

三、显示图片

显示这里使用的是PIL库、numpy库以及matplotlib.pyplot as plt,下面是代码片段

def get_screen_image():
    os.system('adb shell screencap -p /sdcard/screen.png')#让手机截屏存储在手机的根目录
    os.system('adb pull /sdcard/screen.png')
    return = numpy.array(PIL.Image.open('screen.png'))#获取图片像素点的值

fig = plt.figure()#创建一个空白对象
ax = plt.imshow(get_screen_image(),animated = True)#显示图片信息
plt.show()
python实现微信跳一跳_第1张图片
1.png

四、获取两次鼠标点击位置信息

def on_calck(event, coor=[]):#绑定的鼠标单击事件
    coor.append((event.xdata, event.ydata))#获取事件信息
    if len(coor) == 2:#当点击两次之后
        jump_to_next(coor.pop(), coor.pop())#计算跳跃距离与按压时间,并清除数据

figure.canvas.mpl_connect('button_press_event', on_calck)#获取在figure区域的鼠标点击事件的坐标

五、计算两次鼠标点击距离、按压时间、给出按压指令

def jump_to_next(point1, point2):#计算两次鼠标点击位置的距离
    x1, y1 = point1; x2, y2 = point2
    distance = ((x2-x1)**2 + (y2-y1)**2)**0.5
    os.system('adb shell input swipe 320 410 320 410 {}'.format(int(distance*1.35)))

六、循环更新截图

为了便于跟新截图,这里使用一个全局变量 updateFlag = True or False

def update_screen(frame):#更新图片 /从画图片
    global need_update
    if need_update:
        time.sleep(1)
        axes_image.set_array(get_screen_image())
        need_update = False
    return axes_image,
ani = FuncAnimation(figure, update_screen, interval=50, blit=True)
python实现微信跳一跳_第2张图片
2.png

附:代码

# !/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import PIL,numpy
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time

need_update = True

def get_screen_image():
    os.system('adb shell screencap -p /sdcard/screen.png')#获取当前界面的手机截图
    os.system('adb pull /sdcard/screen.png')#下载当前这个截图到当前电脑当前文件夹下
    return numpy.array(PIL.Image.open('screen.png'))

def jump_to_next(point1, point2):#计算两点之间的长度
    x1, y1 = point1; x2, y2 = point2
    distance = ((x2-x1)**2 + (y2-y1)**2)**0.5
    os.system('adb shell input swipe 320 410 320 410 {}'.format(int(distance*1.35)))

def on_calck(event, coor=[]):#绑定的鼠标单击事件
    global need_update
    coor.append((event.xdata, event.ydata))
    if len(coor) == 2:
        jump_to_next(coor.pop(), coor.pop())
    need_update = True

def update_screen(frame):#更新图片
    global need_update
    if need_update:
        time.sleep(1)#暂停1秒
        axes_image.set_array(get_screen_image())
        need_update = False
    return axes_image,

figure = plt.figure()#创建一个空白的图片对象/创建一张图片
axes_image = plt.imshow(get_screen_image(), animated=True)#把获取的图片话在坐标轴上面
figure.canvas.mpl_connect('button_press_event', on_calck)
ani = FuncAnimation(figure, update_screen, interval=50, blit=True)
plt.show()

感谢:潭州学院的直播课以及资料、网上的大牛
代码执行速度比较慢,但是可以实现功能,后期会修改增添一些功能,实现准确跳跃

你可能感兴趣的:(python实现微信跳一跳)