zoukankan      html  css  js  c++  java
  • 跳一跳

    
    

    adb驱动安装

    安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装在一个非常容易记住的路径,这里以C:adb为例。如图:

    
    

    安装完成后我们在CMD控制台输入adb是没有反应的,因为我们还没有配置好adb的环境变量,我们可以右键点击我的电脑,选择属性,点击高级系统设置,在高级中点击环境变量。

    进入环境变量配置页面之后,在系统变量中找到Path,点击Path再点击编辑,然后将刚才安装adb的路径复制,这里我们是以E:adb为例的,所以我们将这个路径加入进环境变量中,点击保存。这时候我们再次打开CMD控制台,输入adb,出现如下界面,即为配置成功。如图

    
    

    
    
    

    手机与电脑连接

    将安卓手机用USB与电脑相连接,在安卓手机上打开开发者模式,并开启USB调试模式,每种安卓手机开启的方法都不一样,这里需要小伙伴自己查询自己型号的手机如何开启开发者模式,以OPPO A57手机为例,在设置中找到手机版本号,连续点击5次即可开启开发者模式。

    如果以上操作没有任何错误,那么在CMD控制台,执行命令 adb devices可显示当前手机连接的端口号。

    注意连接是否成功


    代码如下:

    1
    === 思路 === 2 核心:每次落稳之后截图,根据截图算出棋子的坐标和下一个块顶面的中点坐标, 3 根据两个点的距离乘以一个时间系数获得长按的时间 4 识别棋子:靠棋子的颜色来识别位置,通过截图发现最下面一行大概是一条 5 直线,就从上往下一行一行遍历,比较颜色(颜色用了一个区间来比较) 6 找到最下面的那一行的所有点,然后求个中点,求好之后再让 Y 轴坐标 7 减小棋子底盘的一半高度从而得到中心点的坐标 8 识别棋盘:靠底色和方块的色差来做,从分数之下的位置开始,一行一行扫描, 9 由于圆形的块最顶上是一条线,方形的上面大概是一个点,所以就 10 用类似识别棋子的做法多识别了几个点求中点,这时候得到了块中点的 X 11 轴坐标,这时候假设现在棋子在当前块的中心,根据一个通过截图获取的 12 固定的角度来推出中点的 Y 坐标 13 最后:根据两点的坐标算距离乘以系数来获取长按时间(似乎可以直接用 X 轴距离) 14 """ 15 16 import math 17 import re 18 import random 19 import sys 20 import time 21 from PIL import Image 22 from six.moves import input 23 24 if sys.version_info.major != 3: 25 print('请使用Python3') 26 exit(1) 27 try: 28 from common import debug, config, screenshot, UnicodeStreamFilter 29 from common.auto_adb import auto_adb 30 except Exception as ex: 31 print(ex) 32 print('请将脚本放在项目根目录中运行') 33 print('请检查项目根目录中的 common 文件夹是否存在') 34 exit(1) 35 adb = auto_adb() 36 VERSION = "1.1.4" 37 38 # DEBUG 开关,需要调试的时候请改为 True,不需要调试的时候为 False 39 DEBUG_SWITCH = False 40 adb.test_device() 41 # Magic Number,不设置可能无法正常执行,请根据具体截图从上到下按需 42 # 设置,设置保存在 config 文件夹中 43 config = config.open_accordant_config() 44 under_game_score_y = config['under_game_score_y'] 45 # 长按的时间系数,请自己根据实际情况调节 46 press_coefficient = config['press_coefficient'] 47 # 二分之一的棋子底座高度,可能要调节 48 piece_base_height_1_2 = config['piece_base_height_1_2'] 49 # 棋子的宽度,比截图中量到的稍微大一点比较安全,可能要调节 50 piece_body_width = config['piece_body_width'] 51 # 图形中圆球的直径,可以利用系统自带画图工具,用直线测量像素,如果可以实现自动识别圆球直径,那么此处将可实现全自动。 52 head_diameter = config.get('head_diameter') 53 if head_diameter == None: 54 density_str = adb.test_density() 55 matches = re.search(r'd+', density_str) 56 density_val = int(matches.group(0)) 57 head_diameter = density_val / 8 58 59 60 def set_button_position(im): 61 """ 62 将 swipe 设置为 `再来一局` 按钮的位置 63 """ 64 global swipe_x1, swipe_y1, swipe_x2, swipe_y2 65 w, h = im.size 66 left = int(w / 2) 67 top = int(1584 * (h / 1920.0)) 68 left = int(random.uniform(left - 200, left + 200)) 69 top = int(random.uniform(top - 200, top + 200)) # 随机防 ban 70 after_top = int(random.uniform(top - 200, top + 200)) 71 after_left = int(random.uniform(left - 200, left + 200)) 72 swipe_x1, swipe_y1, swipe_x2, swipe_y2 = left, top, after_left, after_top 73 74 75 def jump(distance, delta_piece_y): 76 """ 77 跳跃一定的距离 78 """ 79 # 计算程序长度与截图测得的距离的比例 80 scale = 0.945 * 2 / head_diameter 81 actual_distance = distance * scale * (math.sqrt(6) / 2) 82 press_time = (-945 + math.sqrt(945 ** 2 + 4 * 105 * 83 36 * actual_distance)) / (2 * 105) * 1000 84 press_time *= press_coefficient 85 press_time = max(press_time, 200) # 设置 200ms 是最小的按压时间 86 press_time = int(press_time) 87 88 cmd = 'shell input swipe {x1} {y1} {x2} {y2} {duration}'.format( 89 x1=swipe_x1, 90 y1=swipe_y1, 91 x2=swipe_x2, 92 y2=swipe_y2, 93 duration=press_time + delta_piece_y 94 ) 95 print(cmd) 96 adb.run(cmd) 97 return press_time 98 99 100 def find_piece_and_board(im): 101 """ 102 寻找关键坐标 103 """ 104 w, h = im.size 105 points = [] # 所有满足色素的点集合 106 piece_y_max = 0 107 board_x = 0 108 board_y = 0 109 scan_x_border = int(w / 8) # 扫描棋子时的左右边界 110 scan_start_y = 0 # 扫描的起始 y 坐标 111 im_pixel = im.load() 112 # 以 50px 步长,尝试探测 scan_start_y 113 for i in range(int(h / 3), int(h * 2 / 3), 50): 114 last_pixel = im_pixel[0, i] 115 for j in range(1, w): 116 pixel = im_pixel[j, i] 117 # 不是纯色的线,则记录 scan_start_y 的值,准备跳出循环 118 if pixel != last_pixel: 119 scan_start_y = i - 50 120 break 121 if scan_start_y: 122 break 123 print('start scan Y axis: {}'.format(scan_start_y)) 124 125 # 从 scan_start_y 开始往下扫描,棋子应位于屏幕上半部分,这里暂定不超过 2/3 126 for i in range(scan_start_y, int(h * 2 / 3)): 127 # 横坐标方面也减少了一部分扫描开销 128 for j in range(scan_x_border, w - scan_x_border): 129 pixel = im_pixel[j, i] 130 # 根据棋子的最低行的颜色判断,找最后一行那些点的平均值,这个颜 131 # 色这样应该 OK,暂时不提出来 132 if (50 < pixel[0] < 60) 133 and (53 < pixel[1] < 63) 134 and (95 < pixel[2] < 110): 135 points.append((j, i)) 136 piece_y_max = max(i, piece_y_max) 137 138 bottom_x = [x for x, y in points if y == piece_y_max] # 所有最底层的点的横坐标 139 if not bottom_x: 140 return 0, 0, 0, 0, 0 141 142 piece_x = int(sum(bottom_x) / len(bottom_x)) # 中间值 143 piece_y = piece_y_max - piece_base_height_1_2 # 上移棋子底盘高度的一半 144 145 # 限制棋盘扫描的横坐标,避免音符 bug 146 if piece_x < w / 2: 147 board_x_start = piece_x 148 board_x_end = w 149 else: 150 board_x_start = 0 151 board_x_end = piece_x 152 153 for i in range(int(h / 3), int(h * 2 / 3)): 154 last_pixel = im_pixel[0, i] 155 if board_x or board_y: 156 break 157 board_x_sum = 0 158 board_x_c = 0 159 160 for j in range(int(board_x_start), int(board_x_end)): 161 pixel = im_pixel[j, i] 162 # 修掉脑袋比下一个小格子还高的情况的 bug 163 if abs(j - piece_x) < piece_body_ 164 continue 165 166 # 检查Y轴下面5个像素, 和背景色相同, 那么是干扰 167 ver_pixel = im_pixel[j, i + 5] 168 if abs(pixel[0] - last_pixel[0]) 169 + abs(pixel[1] - last_pixel[1]) 170 + abs(pixel[2] - last_pixel[2]) > 10 171 and abs(ver_pixel[0] - last_pixel[0]) 172 + abs(ver_pixel[1] - last_pixel[1]) 173 + abs(ver_pixel[2] - last_pixel[2]) > 10: 174 board_x_sum += j 175 board_x_c += 1 176 if board_x_sum: 177 board_x = board_x_sum / board_x_c 178 last_pixel = im_pixel[board_x, i] 179 180 # 首先找到游戏的对称中心,由对称中心做辅助线与x=board_x直线的交点即为棋盘的中心位置 181 # 有了对称中心,可以知道棋子在棋盘上面的相对位置(偏高或偏低,偏高的话测量值比实际值大, 182 # 偏低相反。最后通过delta_piece_y来对跳跃时间进行微调 183 center_x = w / 2 + (24 / 1080) * w 184 center_y = h / 2 + (17 / 1920) * h 185 if piece_x > center_x: 186 board_y = round((25.5 / 43.5) * (board_x - center_x) + center_y) 187 delta_piece_y = piece_y - round((25.5 / 43.5) * (piece_x - center_x) + center_y) 188 else: 189 board_y = round(-(25.5 / 43.5) * (board_x - center_x) + center_y) 190 delta_piece_y = piece_y - round(-(25.5 / 43.5) * (piece_x - center_x) + center_y) 191 192 if not all((board_x, board_y)): 193 return 0, 0, 0, 0, 0 194 return piece_x, piece_y, board_x, board_y, delta_piece_y 195 196 197 def yes_or_no(): 198 """ 199 检查是否已经为启动程序做好了准备 200 """ 201 while True: 202 yes_or_no = str(input('请确保手机打开了 ADB 并连接了电脑,' 203 '然后打开跳一跳并【开始游戏】后再用本程序,确定开始?[y/n]:')) 204 if yes_or_no == 'y': 205 break 206 elif yes_or_no == 'n': 207 print('谢谢使用', end='') 208 exit(0) 209 else: 210 print('请重新输入') 211 212 213 def main(): 214 """ 215 主函数 216 """ 217 print('程序版本号:{}'.format(VERSION)) 218 print('激活窗口并按 CONTROL + C 组合键退出') 219 debug.dump_device_info() 220 screenshot.check_screenshot() 221 222 i, next_rest, next_rest_time = (0, random.randrange(3, 10), 223 random.randrange(5, 10)) 224 while True: 225 im = screenshot.pull_screenshot() 226 # 获取棋子和 board 的位置 227 piece_x, piece_y, board_x, board_y, delta_piece_y = find_piece_and_board(im) 228 ts = int(time.time()) 229 print(ts, piece_x, piece_y, board_x, board_y) 230 set_button_position(im) 231 jump(math.sqrt((board_x - piece_x) ** 2 + (board_y - piece_y) ** 2), delta_piece_y) 232 if DEBUG_SWITCH: 233 debug.save_debug_screenshot(ts, im, piece_x, 234 piece_y, board_x, board_y) 235 debug.backup_screenshot(ts) 236 im.close() 237 i += 1 238 if i == next_rest: 239 print('已经连续打了 {} 下,休息 {}秒'.format(i, next_rest_time)) 240 for j in range(next_rest_time): 241 sys.stdout.write(' 程序将在 {}秒 后继续'.format(next_rest_time - j)) 242 sys.stdout.flush() 243 time.sleep(1) 244 print(' 继续') 245 i, next_rest, next_rest_time = (0, random.randrange(30, 100), 246 random.randrange(10, 60)) 247 # 为了保证截图的时候应落稳了,多延迟一会儿,随机值防 ban 248 time.sleep(random.uniform(1.2, 1.4)) 249 250 251 if __name__ == '__main__': 252 try: 253 yes_or_no() 254 main() 255 except KeyboardInterrupt: 256 adb.run('kill-server') 257 print(' 谢谢使用', end='') 258 exit(0)
  • 相关阅读:
    常用到的Linux基础命令
    adb linux
    adb,monkey,perfdog的区别
    postman中的变量与使用
    fiddler教程-抓包,弱网,断点,mock等
    linux启动服务和开机自启动设置
    .NET Framework 版本和依赖关系
    C#中的Guid
    EPL II 编程打印
    Button 类
  • 原文地址:https://www.cnblogs.com/Adaran/p/13151972.html
Copyright © 2011-2022 走看看