实现思路:

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

完整代码,测试机Oppo r11

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from PIL import Image, ImageDraw
  5. import math
  6. import os
  7. import time
  8. import subprocess
  9.  
  10. # 小小人底部RGB
  11. role_bottom_rgb = (58, 58, 102)
  12. # 小小人头部RGB
  13. role_top_rgb = (65, 65, 90)
  14. # 白色中心点 RGB
  15. white_point = (245, 245, 245)
  16. # 按压系数
  17. press_coefficient = 1.35
  18.  
  19. def get_screenshot():
  20. """
  21. 获取截图信息
  22. :return:
  23. """
  24.  
  25. process = subprocess.Popen('adb shell screencap -p', shell=True,
  26. stdout=subprocess.PIPE)
  27. binary_screenshot = process.stdout.read()
  28. binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n')
  29. with open('autojump.png', 'wb') as f:
  30. f.write(binary_screenshot)
  31. time.sleep(1)
  32. img = Image.open('autojump.png')
  33. return img
  34.  
  35. def is_similar(rgb1, rgb2, degree=20):
  36. """
  37. 判断颜色是否相近
  38. :param rgb1:
  39. :param rgb2:
  40. :param degree:
  41. :return:
  42. """
  43. return abs(rgb1[0] - rgb2[0]) <= degree and abs(
  44. rgb1[1] - rgb2[1]) <= degree and abs(rgb1[2] - rgb2[2]) <= degree
  45.  
  46. def calculate_jump_distance(img):
  47. """
  48. 计算跳一跳的距离
  49. :param image:
  50. :return:
  51. """
  52. draw = ImageDraw.Draw(img)
  53. im_pixel = img.load()
  54. w, h = img.size
  55. # 设置有效扫描区域
  56. min_x = 100
  57. max_x = w - 10
  58. min_y = 400
  59. max_y = h - 100
  60. # step1:寻找小小人底座位置
  61. role_start_x = 0
  62. role_end_x = 0
  63. role_y = 0
  64. find_role = False
  65. # y轴从下往上扫描
  66. for y in range(max_y, min_y, -1):
  67. # 找到小小人最低部一行,退出y轴循环
  68. if find_role:
  69. break
  70. # y轴未找到最低一行,则继续遍历x轴元素点
  71. # x轴从左到右扫描
  72. for x in range(min_x, max_x):
  73. current_rgb = im_pixel[x, y] # 当前像素RGB值
  74. # 寻找到小人底座首位像素点
  75. if is_similar(current_rgb, role_bottom_rgb, 5):
  76. if not find_role: # 找到首位像素点
  77. find_role = True
  78. role_start_x = x
  79. role_y = y # 小人底座中心点的y轴坐标已确定
  80.  
  81. # 小人底座最右侧像素点定位,条件:首位像素点已找到,在当前y轴上继续遍历x轴,当当前像素不在小小人中时,前一个像素点为底座最右点
  82. if find_role and not is_similar(current_rgb, role_bottom_rgb, 5):
  83. role_end_x = x - 1
  84. break
  85. # 小小人底座中心点
  86. role_x = (role_start_x + role_end_x) / 2
  87. role = (role_x, role_y)
  88. draw.point([role], fill=(255, 0, 0))
  89.  
  90. # step2:寻找棋盘顶点:从上往下,从左到右扫描,寻找首位与初始点不一样的像素
  91. # 解决小小人头部出现在最顶部时的BUG,在x轴扫描时,跳过小小人位置[role_start_x,role_end_x]
  92. top_x = 0
  93. top_y = 0
  94. top_rgb = None
  95. role_top_flag = False
  96. for y in range(min_y, max_y):
  97. for x in range(min_x, max_x):
  98. current_rgb = im_pixel[x, y]
  99. # 首先出现小小人的头部
  100. if not role_top_flag:
  101. if is_similar(current_rgb, role_top_rgb, 40):
  102. print("首先出现小小人头部!")
  103. role_top_flag = True
  104. continue
  105. # 当小小人头部在最顶部时,从上到下扫描时,当x处于小小人位置中时,跳过本次循环
  106. if (role_start_x - 50 <= x <= role_end_x + 50) and role_top_flag:
  107. # 当x轴坐标在小小人位置时
  108. continue
  109.  
  110. # 顶部既不是小小人,又与初始像素点不一样,则定位为棋盘顶部,退出x轴扫描
  111. if not is_similar(current_rgb, im_pixel[min_x, min_y],
  112. 20): # 与背景像素点不一样
  113. top_x = x
  114. # 解决棋盘边上出现一条乱七八糟颜色点将棋盘围起来时的BUG,找到第一个差异点后y轴继续往下5个像素点作为顶点
  115. top_y = y + 4
  116. top_rgb = im_pixel[top_x, top_y]
  117. break
  118.  
  119. if top_rgb: # 找到与初始点不一样的像素点,退出y轴循环
  120. break
  121.  
  122. top = (top_x, top_y)
  123. draw.point([top], fill=(255, 0, 0))
  124.  
  125. # step3:寻找棋盘最右侧点,条件:从top_x 向右,top_y 向下扫描,当与棋盘顶部像素点相似,x轴最大时,所在点为最右点
  126. right_x = top_x
  127. right_y = top_y
  128. find_border = False
  129. check_rgb = top_rgb
  130. for x in range(top_x, max_x):
  131. for y in range(top_y, max_y):
  132. current_rgb = im_pixel[x, y]
  133. # 找到相邻的相似元素点,定位条件:30个像素内,颜色相似
  134. if is_similar(current_rgb, check_rgb, 20) and abs(
  135. x - right_x) <= 5 and abs(y - right_y) <= 5:
  136. check_rgb = current_rgb
  137. find_border = True
  138. right_x = x
  139. right_y = y
  140. break
  141. else:
  142. # 如果当前y轴扫描完毕都没有遇到棋盘相似点(即没有遇到break),说明已经超出了最右侧棋盘点,退出x轴循环
  143. if find_border:
  144. break
  145. right = (right_x, right_y)
  146. draw.point([right], fill=(255, 0, 0))
  147. # step4:定位棋盘中心:扫描棋盘,判断是否存在中心白点,否则初略可认为棋盘中心点位置是顶点和右侧点交叉位置
  148. border = (top_x, right_y)
  149. # 先排除初略中心点位置与白色中心点相似的情况,否则遇到白色版面会定位错误
  150. if not is_similar(im_pixel[top_x, right_y], white_point, 4):
  151. find_white_point = False
  152. for y in range(top_y + 5, right_y + 5):
  153. for x in range(top_x * 2 - right_x + 5, right_x - 5):
  154. if is_similar(im_pixel[x, y], white_point, 2):
  155. # 寻找到白色中心点
  156. find_white_point = True
  157. border = (x, y + 10)
  158. print("寻找到白色中心点!")
  159. break
  160. # 寻找到白色中心点,退出y轴循环
  161. if find_white_point:
  162. break
  163. draw.point([border], fill=(255, 0, 0))
  164.  
  165. # draw.line([top, right], fill=(255, 0, 0), width=10)
  166. draw.line([role, top, right, border], fill=(255, 0, 0), width=10)
  167. # img.show()
  168. img.save("debug.png")
  169. return math.sqrt((role[0] - border[0]) ** 2 + (role[1] - border[1]) ** 2)
  170.  
  171. def jump(distance):
  172. press_time = distance * press_coefficient
  173. press_time = max(press_time, 200) # 设置 200ms 是最小的按压时间
  174. press_time = int(press_time)
  175. cmd = 'adb shell input swipe 400 400 400 400 {duration}'.format(
  176. duration=press_time)
  177. print(cmd)
  178. os.system(cmd)
  179. return press_time
  180.  
  181. if __name__ == '__main__':
  182. i = 1
  183. # img = get_screenshot()
  184. # img=Image.open('autojump.png')
  185. # distance = calculate_jump_distance(img)
  186. # img.close()
  187. # jump(distance)
  188.  
  189. while True:
  190. img = get_screenshot()
  191. distance = calculate_jump_distance(img)
  192. jump(distance)
  193. img.close()
  194. i += 1
  195. if i == 10:
  196. time.sleep(2)
  197. time.sleep(2)
  198. 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. 简明python教程八----输入/输出

    通过创建一个file类的对象来打开一个文件,分别使用file类的read.readline或write方法来读写文件. 最后调用一个close方法来告诉Python我们完成了对文件的使用. poem= ...

  2. PAT 1033 To Fill or Not to Fill[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  3. Objective-C中的alloc和init问题

    从开始学的NSString *name=[[NSString alloc] init] 起,仅仅这句话是分配内存空间,一直在用,从来没考虑过它的内部是怎么实现的.今天无意中看到了这一句代码: NSSt ...

  4. python全栈开发从入门到放弃之socket并发编程之协程

    一.为什么会有协程 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情 ...

  5. Delphi APP 開發入門(十)REST Client 開發

    Delphi APP 開發入門(十)REST Client 開發 分享: Share on facebookShare on twitterShare on google_plusone_share ...

  6. eclipse 创建jsp报错

  7. 【c++ primer, 5e】【try语句块】

    p172~p177:c++的try语句块和异常处理: 1.通常,与用户交互的代码和对象相加(底层的代码)是分离开的,异常由与用户交互的代码处理(底层代码抛出异常就可以了). 2.C++的runtime ...

  8. Top-Down笔记 #01# 计算机网络概述

    因特网 网络核心 分组交换网中的时延.丢包和吞吐量 协议层次及其服务模型 面对攻击的网络 计算机网络和因特网的历史 小结(自己写的...) [什么是因特网?] 具体构成描述 1.与因特网相连的设备被称 ...

  9. Kali视频学习6-10

    Kali视频学习6-10 kali信息收集之主机探测 主机探测指识别目标机器是否可用(简单来说是否在线),在探测过程中,需要得到目标是否online等信息.由于IDS和(入侵检测系统)和IPS(入侵保 ...

  10. 2017杭电ACM集训队单人排位赛 - 6

    2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...