实现思路:

  1. 调用adb命令,截图
  2. 寻找小小人的底部中心点role(从下到上扫描,直到找到小小人相同像素的点,至于小小人像素点rgb是什么,可以使用photoshop查看)
  3. 寻找棋盘最高点top,然后寻找棋盘最右点。根据最高点与最右点,确定棋盘中心点border
  4. 计算role与border之间的直线距离,然后设置按压时间=距离*按压系数
  5. 调用adb 命令,按压屏幕

完整代码,测试机Oppo r11

#!/usr/bin/env python
# -*- coding: utf-8 -*- from PIL import Image, ImageDraw
import math
import os
import time
import subprocess # 小小人底部RGB
role_bottom_rgb = (58, 58, 102)
# 小小人头部RGB
role_top_rgb = (65, 65, 90)
# 白色中心点 RGB
white_point = (245, 245, 245)
# 按压系数
press_coefficient = 1.35 def get_screenshot():
"""
获取截图信息
:return:
""" process = subprocess.Popen('adb shell screencap -p', shell=True,
stdout=subprocess.PIPE)
binary_screenshot = process.stdout.read()
binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n')
with open('autojump.png', 'wb') as f:
f.write(binary_screenshot)
time.sleep(1)
img = Image.open('autojump.png')
return img def is_similar(rgb1, rgb2, degree=20):
"""
判断颜色是否相近
:param rgb1:
:param rgb2:
:param degree:
:return:
"""
return abs(rgb1[0] - rgb2[0]) <= degree and abs(
rgb1[1] - rgb2[1]) <= degree and abs(rgb1[2] - rgb2[2]) <= degree def calculate_jump_distance(img):
"""
计算跳一跳的距离
:param image:
:return:
"""
draw = ImageDraw.Draw(img)
im_pixel = img.load()
w, h = img.size
# 设置有效扫描区域
min_x = 100
max_x = w - 10
min_y = 400
max_y = h - 100
# step1:寻找小小人底座位置
role_start_x = 0
role_end_x = 0
role_y = 0
find_role = False
# y轴从下往上扫描
for y in range(max_y, min_y, -1):
# 找到小小人最低部一行,退出y轴循环
if find_role:
break
# y轴未找到最低一行,则继续遍历x轴元素点
# x轴从左到右扫描
for x in range(min_x, max_x):
current_rgb = im_pixel[x, y] # 当前像素RGB值
# 寻找到小人底座首位像素点
if is_similar(current_rgb, role_bottom_rgb, 5):
if not find_role: # 找到首位像素点
find_role = True
role_start_x = x
role_y = y # 小人底座中心点的y轴坐标已确定 # 小人底座最右侧像素点定位,条件:首位像素点已找到,在当前y轴上继续遍历x轴,当当前像素不在小小人中时,前一个像素点为底座最右点
if find_role and not is_similar(current_rgb, role_bottom_rgb, 5):
role_end_x = x - 1
break
# 小小人底座中心点
role_x = (role_start_x + role_end_x) / 2
role = (role_x, role_y)
draw.point([role], fill=(255, 0, 0)) # step2:寻找棋盘顶点:从上往下,从左到右扫描,寻找首位与初始点不一样的像素
# 解决小小人头部出现在最顶部时的BUG,在x轴扫描时,跳过小小人位置[role_start_x,role_end_x]
top_x = 0
top_y = 0
top_rgb = None
role_top_flag = False
for y in range(min_y, max_y):
for x in range(min_x, max_x):
current_rgb = im_pixel[x, y]
# 首先出现小小人的头部
if not role_top_flag:
if is_similar(current_rgb, role_top_rgb, 40):
print("首先出现小小人头部!")
role_top_flag = True
continue
# 当小小人头部在最顶部时,从上到下扫描时,当x处于小小人位置中时,跳过本次循环
if (role_start_x - 50 <= x <= role_end_x + 50) and role_top_flag:
# 当x轴坐标在小小人位置时
continue # 顶部既不是小小人,又与初始像素点不一样,则定位为棋盘顶部,退出x轴扫描
if not is_similar(current_rgb, im_pixel[min_x, min_y],
20): # 与背景像素点不一样
top_x = x
# 解决棋盘边上出现一条乱七八糟颜色点将棋盘围起来时的BUG,找到第一个差异点后y轴继续往下5个像素点作为顶点
top_y = y + 4
top_rgb = im_pixel[top_x, top_y]
break if top_rgb: # 找到与初始点不一样的像素点,退出y轴循环
break top = (top_x, top_y)
draw.point([top], fill=(255, 0, 0)) # step3:寻找棋盘最右侧点,条件:从top_x 向右,top_y 向下扫描,当与棋盘顶部像素点相似,x轴最大时,所在点为最右点
right_x = top_x
right_y = top_y
find_border = False
check_rgb = top_rgb
for x in range(top_x, max_x):
for y in range(top_y, max_y):
current_rgb = im_pixel[x, y]
# 找到相邻的相似元素点,定位条件:30个像素内,颜色相似
if is_similar(current_rgb, check_rgb, 20) and abs(
x - right_x) <= 5 and abs(y - right_y) <= 5:
check_rgb = current_rgb
find_border = True
right_x = x
right_y = y
break
else:
# 如果当前y轴扫描完毕都没有遇到棋盘相似点(即没有遇到break),说明已经超出了最右侧棋盘点,退出x轴循环
if find_border:
break
right = (right_x, right_y)
draw.point([right], fill=(255, 0, 0))
# step4:定位棋盘中心:扫描棋盘,判断是否存在中心白点,否则初略可认为棋盘中心点位置是顶点和右侧点交叉位置
border = (top_x, right_y)
# 先排除初略中心点位置与白色中心点相似的情况,否则遇到白色版面会定位错误
if not is_similar(im_pixel[top_x, right_y], white_point, 4):
find_white_point = False
for y in range(top_y + 5, right_y + 5):
for x in range(top_x * 2 - right_x + 5, right_x - 5):
if is_similar(im_pixel[x, y], white_point, 2):
# 寻找到白色中心点
find_white_point = True
border = (x, y + 10)
print("寻找到白色中心点!")
break
# 寻找到白色中心点,退出y轴循环
if find_white_point:
break
draw.point([border], fill=(255, 0, 0)) # draw.line([top, right], fill=(255, 0, 0), width=10)
draw.line([role, top, right, border], fill=(255, 0, 0), width=10)
# img.show()
img.save("debug.png")
return math.sqrt((role[0] - border[0]) ** 2 + (role[1] - border[1]) ** 2) def jump(distance):
press_time = distance * press_coefficient
press_time = max(press_time, 200) # 设置 200ms 是最小的按压时间
press_time = int(press_time)
cmd = 'adb shell input swipe 400 400 400 400 {duration}'.format(
duration=press_time)
print(cmd)
os.system(cmd)
return press_time if __name__ == '__main__':
i = 1
# img = get_screenshot()
# img=Image.open('autojump.png')
# distance = calculate_jump_distance(img)
# img.close()
# jump(distance) while True:
img = get_screenshot()
distance = calculate_jump_distance(img)
jump(distance)
img.close()
i += 1
if i == 10:
time.sleep(2)
time.sleep(2)
print("*" * 100)

***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

使用python编写微信跳一跳的自动脚本的更多相关文章

  1. 微信跳一跳辅助自动跳Python

    一.说明 此代码借鉴github一位大神所写,已经做了简化合并处理,如果能成功连上手机并运行,可以实现程序自动玩游戏,刷个1000+的分数轻轻松松 github源码地址 https://github. ...

  2. Python版本微信跳一跳,软件配置

    一.安装python3的环境: 直接从python官方网站下载python3的安装包,直接安装. 记得将python3放到PATH环境变量中,安装的过程中在该配置地方打钩就可以了. 如果安装的过程中出 ...

  3. 利用Python玩微信跳一跳

    创建python项目jump_weixin,新建python程序jump.py 需要4个辅助文件[adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll,fastboot.exe ...

  4. 【辅助工具】Python实现微信跳一跳

    最近迷上了微信跳一跳小游戏,正好也看到知乎上有大神分享了技术贴,我也参考了好多资料,原理就是通过abd命令截取图片,python计算两个点距离,然后转化按压时间,让电脑来完成游戏.我花了很长时间才把程 ...

  5. Python操作微信跳一跳

    “跳一跳”这个东西还是今天刚接触到的,看到了python群中有人再问“微信跳一跳的外挂有人写了没”,“早就有了”,“github”,“等着出个更详细的教程教程没看懂,主要没有用过adb”. 不过没关系 ...

  6. 微信跳一跳,Python辅助自动跳程序

    一.说明 此代码借鉴一位大神提供在gitHub上的源码,已经做了简化合并处理,成功连上手机并运行后,可实现自动玩微信跳一跳游戏,刷个1000+的分数轻轻松松 github源码地址 https://gi ...

  7. C#又能出来装个B了。一步一步微信跳一跳自动外挂

    PS:语言只是载体.思维逻辑才是王道 前天看见了个python的脚本.于是装python.配置环境变量.装pip.折腾了一上午,最终装逼失败. 于是进入博客园,顶部有篇文章吸引了我 .NET开发一个微 ...

  8. 微信跳一跳Python

    微信最新的小程序里面出了个叫“跳一跳”的小游戏,大神们也通过Python实现了自动玩游戏具体代码 如下: Github地址: https://github.com/wangshub/wechat_ju ...

  9. python 微信跳一跳辅助 复现

    本来用的是苹果ios得手机,但是步骤较为复杂,没有吃透,最后妥协用了android的机器搞得. 首先找到大牛的github https://github.com/wangshub/wechat_jum ...

随机推荐

  1. web Servlet 3.0 新特性之web模块化编程,web-fragment.xml编写及打jar包

    web Servlet 3.0 模块化 原本一个web应用的任何配置都需要在web.xml中进行,因此会使得web.xml变得很混乱,而且灵活性差,因此Servlet 3.0可以将每个Servlet. ...

  2. ZenCart分类数量打折Category Quantity Discount插件

    附件:http://files.cnblogs.com/lzj87980239/Category_Quantity_Discount.rar 效果图后台1.将update.sql导入到数据库 2.将Y ...

  3. EntityFramework 6 开篇

    本系列文章主要来讲解理解以及怎样使用EntityFramework,写这个系列主要是因为部门里面准备来使用EF,为了让大家一起来学习,我每天发布1-2篇文章让大家一块参与学习.之前一直写在有道云笔记里 ...

  4. 联想(Lenovo)小新310经典版进bios方法

    1,找到novo按钮. 2,在关机的状态下桶一下小孔,不用任何操作,电脑进入bios选择界面.

  5. C++、Java、JavaScript中的正则表达式

    C++(VS2013编译器):http://msdn.microsoft.com/zh-cn/library/bb982727.aspx#grammarsummary Java:            ...

  6. Flash Builder4注册机

    我的Eclipse下的Flash Builder 4正式版已经过期,之前在网上找到的注册码,都不能用了, 花了很久时间,才找到这个注册机. Flash Builder 4 注册机 Serial Cra ...

  7. JS正则表达式从入门到入土(7)—— 分组

    分组 在使用正则的时候,有时候会想要匹配一串字符串连续出现多次的情况,比如:我想匹配字符串Byron连续出现3次的情况. 有些人会直接写: Byron{3} 但是,这种情况仅仅会匹配Byro加上三个n ...

  8. Android模拟器Intel Atom下载安装配置

    https://software.intel.com 在Android x86模拟器Intel Atom x86 System Image时提示Intel execute disable bit(xd ...

  9. HikariPool-1 - Exception during pool initialization.

    java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required 这是由于springboot自带 mysql-c ...

  10. centos查看是否安装了某个软件

    1. rpm包安装的,可以用rpm -qa看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字". 2. yum方法安装的,可以用yum list ...