import os\nimport 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## 使用方法:\n\n该代码是一个自动玩微信跳一跳游戏的脚本,需要使用 adb 工具实现手机与电脑的连接。使用前需要确保已经安装并配置好 adb 工具。\n\n使用步骤:\n1. 将手机连接到电脑,并确保手机已经打开了 USB 调试模式。\n2. 打开微信跳一跳游戏,并点击开始游戏。\n3. 在电脑上运行该脚本,它会自动截取游戏窗口的一部分,并在窗口中显示出来。\n4. 按下空格键,脚本会计算两次点击的距离,并模拟按压屏幕。\n5. 可以按下 q 键退出游戏。\n\n注意事项:\n- 脚本默认截取游戏窗口的区域为 (300, 400, 900, 900),可以根据实际情况进行调整。\n- 由于不同手机的分辨率和游戏窗口大小可能不同,可能需要根据实际情况调整截取窗口的位置和大小。\n- 脚本使用的计算跳跃距离的方法是计算两次点击中心点的欧几里得距离,可能不适用于所有情况,可以根据实际情况进行调整。


原文地址: https://www.cveoy.top/t/topic/pOK3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录