zhangzhennudt--Python来玩微信跳一跳

Python来玩微信跳一跳

话不多说,先上流程图。当然,只是实现手动玩微信跳一跳,纯属个人爱好,不喜勿喷!

准备工作:

1.安卓手机使用开发者模式与计算机通过adb连接成功,不会的自己问度娘
2.使用语言Python3
3.pip安装第三方库numpy,pillow,matplotlib

函数讲解:

1.通过adb手机屏幕截屏get_screen_image()

这里主要是用到adb命令screencap

os.system("adb shell screencap -p /sdcard/screen.png")

截取手机屏幕,存储到sdcard,文件名为screen.png

os.system("adb pull /sdcard/screen.png")
将sdcard上的screen,下载到本地

2.跳一跳jump_to_next(distance)

通过传入的距离参数,计算出需要按压的时间,模拟按压屏幕动作,实现跳一跳功能。

这里注意不同手机系数不同,可以根据实际测试自行修改,vivo手机为2

adb shell input swipe 320 410 320 410

这里是调用adb命令模仿按压屏幕操作,其中第一个坐标(320,410)为第一个按压点位置,第二个坐标320,410)为第二个按压点位置。前后相同,表示在同一点按压屏幕。前后不同,即为滑动屏幕。

3.刷屏update_screen(*args)

读取need_update,判断是否需要刷屏,如需,直接调用 get_screen_image(),返回axes_image,

注意,global need_update,代表判断的全局变量,需要刷屏的标志

return axes_image,

后面加“”是因为返回的是axes_image的数组

4.响应鼠标点击事件on_click(event)

通过鼠标点击确定,跳跃起始点坐标,和终点坐标,通过坐标计算出跳跃距离,并传递给jump_to_next(distance)

#两次鼠标点击X,Y坐标值存入cor
    ix,iy=event.xdata,event.ydata
    coords=[(ix,iy)]
    #cor第一个为第一次点击坐标,第二个为第二次点击坐标
    cor.append(coords)
    click_count+=1

这里由于之后代码设置运行条件为:if click_count>1。其中click_count初始值为0,所以以上代码运行两次,cor中存入两次鼠标点击坐标

5.其他

fig.canvas.mpl_connect("button_press_event", on_click)

接收事件,写回调函数,然后将函数连接到事件管理器,它是FigureCanvasBase的一部分


ani=animation.FuncAnimation(fig,update_screen,interval=50,blit=True)

matplotlib 实现动图的更新,这里注意这个FuncAnimation函数,必须进行实例化才能执行

6.附上全部代码

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 17 14:40:37 2018

@author: zhangzhennudt
"""
import os
import numpy as np
import PIL
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time


#adb命令截屏,返回数组
def get_screen_image():
    os.system("adb shell screencap -p /sdcard/screen.png")
    os.system("adb pull /sdcard/screen.png")
    return np.array(PIL.Image.open("screen.png"))
#传入距离,根据距离计算按压时间,adb命令模拟按压屏幕,调到下一个
def jump_to_next(distance):
    press_time = distance *2
    press_time = int(press_time)
    cmd = 'adb shell input swipe 320 410 320 410 ' + str(press_time)
    os.system(cmd)
#画一个空图像
fig=plt.figure()
#图像传入数组,实时刷新
axes_image=plt.imshow(get_screen_image(),animated=True)
#设置需要更新标志
need_update=True
#鼠标点击计数
click_count = 0
#坐标
cor = []
#更新截屏,返回新图像
def update_screen(*args):
    global need_update   
    if need_update:
        time.sleep(1)
        axes_image.set_array(get_screen_image())
        need_update=False
    return axes_image,
#响应鼠标点击事件
def on_click(event):
    global need_update
    global ix, iy
    global click_count
    global cor

    #鼠标点击X,Y坐标值存入cor
    ix,iy=event.xdata,event.ydata
    coords=[(ix,iy)]
    #cor第一个为第一次点击坐标,第二个为第二次点击坐标
    cor.append(coords)

    click_count+=1
    #鼠标点击初始为0,点一次为1,点两次为2
    if click_count>1:#当鼠标点两次后,执行以下代码
        click_count=0#首先鼠标计数归零重置
        #pop操作,cor1为第二次点击坐标,即为终点坐标
        cor1=cor.pop()
        #继续pop操作,cor2为第一次点击坐标,即为起点坐标
        cor2=cor.pop()
        #勾股定理求距离
        distance = (cor1[0][0] - cor2[0][0])**2 + (cor1[0][1] - cor2[0][1])**2
        distance = distance ** 0.5
        #调用跳跃函数
        jump_to_next(distance)
        #跳完后,需要更新标志重置
        need_update=True
#接收事件,写回调函数,然后将函数连接到事件管理器,它是FigureCanvasBase的一部分
#接收鼠标点击事件,写回调函数on_click
fig.canvas.mpl_connect("button_press_event", on_click)
# matplotlib 实现动图的更新
ani=animation.FuncAnimation(fig,update_screen,interval=50,blit=True)
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值