Python 自动跳跃游戏脚本 - 使用 OpenCV 和 ADB
{ "title": "Python 自动跳跃游戏脚本 - 使用 OpenCV 和 ADB", "description": "使用 Python 脚本实现自动跳跃游戏,通过 OpenCV 识别游戏画面中的障碍物并计算跳跃距离,使用 ADB 命令控制手机模拟点击跳跃。", "keywords": "Python, 自动化, 游戏, OpenCV, ADB, 跳跃, 识别, 脚本, 障碍物", "content": ""import cv2\nimport numpy as np\nfrom PIL import ImageGrab\nimport time\nimport math\nimport random\n\ndef jump(distance):\n press_time = int(distance * 1.35)\n rand_time = random.randint(0, 9) * 0.01\n press_time += rand_time\n cmd = 'adb shell input swipe 320 410 320 410 ' + str(press_time)\n os.system(cmd)\n\ndef get_center(img):\n gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n ret, binary = cv2.threshold(gray, 230, 255, cv2.THRESH_BINARY)\n contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n cnt = max(contours, key=cv2.contourArea)\n M = cv2.moments(cnt)\n cX = int(M['m10'] / M['m00'])\n cY = int(M['m01'] / M['m00'])\n return cX, cY\n\ndef main():\n while True:\n img = ImageGrab.grab(bbox=(300, 400, 900, 900)) # 截取游戏窗口的一部分\n img_np = np.array(img)\n img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)\n\n cX, cY = get_center(img_rgb)\n cv2.circle(img_rgb, (cX, cY), 10, (0, 0, 255), -1)\n\n cv2.imshow("Jump Game", img_rgb)\n\n if cv2.waitKey(1) & 0xFF == ord('q'):\n break\n\n if cv2.waitKey(1) & 0xFF == ord(' '):\n # 计算两次点击的距离\n img = ImageGrab.grab(bbox=(300, 400, 900, 900))\n img_np = np.array(img)\n img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)\n cX_new, cY_new = get_center(img_rgb)\n distance = math.sqrt((cX_new - cX) ** 2 + (cY_new - cY) ** 2)\n jump(distance)\n\n cv2.destroyAllWindows()\n\nif name == "main":\n main()\n"\n}
原文地址: https://www.cveoy.top/t/topic/pOKf 著作权归作者所有。请勿转载和采集!