def jump(distance): """ 跳跃一定的距离 """ press_time = distance * press_coefficient press_time = max(press_time, 200) # 设置 200ms 是最小的按压时间 press_time = int(press_time) cmd = 'shell input swipe {x1} {y1} {x2} {y2} {duration}'.format( x1=swipe_x1, y1=swipe_y1, x2=swipe_x2, y2=swipe_y2, duration=press_time ) print('{} {}'.format(adb.adb_path, cmd)) adb.run(cmd) return press_time
def _get_screen_size(): """ 获取手机屏幕大小 """ size_str = adb.run('shell wm size') m = re.search(r'(\d+)x(\d+)', size_str) if m: return "{height}x{width}".format(height=m.group(2), width=m.group(1)) return "1920x1080"
def pull_screenshot(): """ 获取屏幕截图,目前有 0 1 2 3 四种方法,未来添加新的平台监测方法时, 可根据效率及适用性由高到低排序 """ global SCREENSHOT_WAY if 1 <= SCREENSHOT_WAY <= 3: process = subprocess.Popen(adb.adb_path + ' shell screencap -p', shell=True, stdout=subprocess.PIPE) binary_screenshot = process.stdout.read() if SCREENSHOT_WAY == 2: binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n') elif SCREENSHOT_WAY == 1: binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n') f = open('autojump.png', 'wb') f.write(binary_screenshot) f.close() elif SCREENSHOT_WAY == 0: adb.run('shell screencap -p /sdcard/autojump.png') adb.run('pull /sdcard/autojump.png .')
def dump_device_info(): """ 显示设备信息 """ size_str = adb.run('shell wm size') device_str = adb.run('shell getprop ro.product.device') phone_os_str = adb.run('shell getprop ro.build.version.release') density_str = adb.run('shell wm density') print("""********** Screen: {size} Density: {dpi} Device: {device} Phone OS: {phone_os} Host OS: {host_os} Python: {python} **********""".format(size=size_str.strip(), dpi=density_str.strip(), device=device_str.strip(), phone_os=phone_os_str.strip(), host_os=sys.platform, python=sys.version))
def jump(distance, delta_piece_y): """ 跳跃一定的距离 """ # 计算程序长度与截图测得的距离的比例 scale = 0.945 * 2 / head_diameter actual_distance = distance * scale * (math.sqrt(6) / 2) press_time = (-945 + math.sqrt(945 ** 2 + 4 * 105 * 36 * actual_distance)) / (2 * 105) * 1000 press_time = max(press_time, 200) # 设置 200ms 是最小的按压时间 press_time = int(press_time) cmd = 'shell input swipe {x1} {y1} {x2} {y2} {duration}'.format( x1=swipe_x1, y1=swipe_y1, x2=swipe_x2, y2=swipe_y2, duration=press_time + delta_piece_y ) print('{} {}'.format(adb.adb_path, cmd)) adb.run(cmd) return press_time
DEBUG_SWITCH = False # Magic Number,不设置可能无法正常执行,请根据具体截图从上到下按需 # 设置,设置保存在 config 文件夹中 config = config.open_accordant_config() under_game_score_y = config['under_game_score_y'] # 长按的时间系数,请自己根据实际情况调节 press_coefficient = config['press_coefficient'] # 二分之一的棋子底座高度,可能要调节 piece_base_height_1_2 = config['piece_base_height_1_2'] # 棋子的宽度,比截图中量到的稍微大一点比较安全,可能要调节 piece_body_width = config['piece_body_width'] # 图形中圆球的直径,可以利用系统自带画图工具,用直线测量像素,如果可以实现自动识别圆球直径,那么此处将可实现全自动。 head_diameter = config.get('head_diameter') if head_diameter == None: density_str = adb.run('shell wm density') matches = re.search(r'\d+', density_str) density_val = int(matches.group(0)) head_diameter = density_val / 8 def set_button_position(im): """ 将 swipe 设置为 `再来一局` 按钮的位置 """ global swipe_x1, swipe_y1, swipe_x2, swipe_y2 w, h = im.size left = int(w / 2) top = int(1584 * (h / 1920.0)) left = int(random.uniform(left - 50, left + 50)) top = int(random.uniform(top - 10, top + 10)) # 随机防 ban
if next_start >len(y_score): break jump(math.sqrt((board_x - piece_x) ** 2 + (board_y - piece_y) ** 2)) if DEBUG_SWITCH: debug.save_debug_screenshot(ts, im, piece_x, piece_y, board_x, board_y) debug.backup_screenshot(ts) im.close() i += 1 j += 1 if i == next_rest: print('已经连续打了 {} 下,休息 {}s'.format(i, next_rest_time)) for j in range(next_rest_time): sys.stdout.write('\r程序将在 {}s 后继续'.format(next_rest_time - j)) sys.stdout.flush() time.sleep(1) print('\n继续') i, next_rest, next_rest_time = (0, random.randrange(30, 100), random.randrange(10, 60)) # 为了保证截图的时候应落稳了,多延迟一会儿,随机值防 ban time.sleep(random.uniform(0.9, 1.2)) if __name__ == '__main__': try: main() except KeyboardInterrupt: adb.run('kill-server') print('bye') exit(0)