快速上手!打造属于你的跳一跳辅助神器
跳一跳自从问世以来就吸引了无数的玩家,但是也由于难度松紧不匀,很多玩家半路中途就放弃了,这时候一个小小的辅助神器就能为玩家带来不一样的游戏体验,但是市面上的辅助神器很多都是收费的,为了解决这种问题,我们自己动手打造一款免费的跳一跳辅助神器。
第一步:搭建开发环境
首先需要搭建一个开发环境,为了方便这里选择使用Visual Studio Code作为开发工具。接着需要在电脑上安装node.js运行环境以及npm包管理工具。然后在终端中运行以下命令:
npm install -g cnpm --registry=https://registry.npm.taobao.org
接着执行以下命令创建一个项目文件夹:
mkdir jump && cd jump
第二步:编写代码
接下来就可以开始编写代码了,这里简单介绍一下代码的实现思路。
通过adb连接手机并在电脑上安装跳一跳游戏,然后使用opencv等技术识别屏幕上的小人和方块,并计算出小人到方块中心点的距离和方向。最后通过adb发送模拟点击事件使得小人跳到方块上。
以下是代码的实现,Python是跨平台语言,在很多方面都有着良好的支持。
# -*- coding: utf-8 -*-
import os
import time
import math
import random
import numpy as np
import cv2
adb_path = \"adb\"
screen_path = \"/sdcard/screenshot.png\"
local_path = \"/Users/test\"
temp1_path = \"template.png\" # 小跳棋模板
temp2_path = \"template2.png\" # 游戏结束画面模板
interval = 2.348 # 两点距离与按压时间的比例
swipe_time = 0.3 # 滑动时间
# 重新开始游戏
def restart_game():
os.system(\"%s shell input tap 540 1045\" % adb_path)
time.sleep(1.5)
# 截图并拉到本地
def pull_screenshot():
os.system(\"%s shell screencap -p %s\" % (adb_path, screen_path))
os.system(\"%s pull %s %s\" % (adb_path, screen_path, local_path))
# 根据模板匹配小跳棋
def match(img, template):
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
value = cv2.minMaxLoc(res)[1]
return value
# 得到小人和下一个方块的距离和方向
def get_distance_direction(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, img_binary = cv2.threshold(img_gray, 245, 255, cv2.THRESH_BINARY)
img_edges = cv2.Canny(img_binary, 100, 200)
_, contours, _ = cv2.findContours(img_edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = []
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
if w < 50 and w > 20 and h > 40:
cnts.append(cnt)
cnts.sort(key=lambda c: cv2.boundingRect(c)[0])
if len(cnts) > 1:
cnt_right = cnts[0]
cnt_left = cnts[1]
box_left = cv2.boundingRect(cnt_left)
box_right = cv2.boundingRect(cnt_right)
x_center_left = box_left[0] + box_left[2] / 2
x_center_right = box_right[0] + box_right[2] / 2
distance = x_center_right - x_center_left
direction = 1.0 if x_center_left > x_center_right else -1.0
else:
distance = -1
direction = 0.0
return distance, direction
# 跳一跳主逻辑
def jump(game_times):
restart_game()
for i in range(game_times):
pull_screenshot()
img = cv2.imread(local_path + \"/screenshot.png\", cv2.IMREAD_COLOR)
value = match(img, cv2.imread(temp1_path, cv2.IMREAD_GRAYSCALE))
if value < 0.9:
print(\"找不到小跳棋\")
continue
distance, direction = get_distance_direction(img)
if distance < 0:
print(\"未找到下一个方块\")
continue
press_time = distance * interval
press_x = random.randint(0, 5) + math.floor(540 + direction * random.randint(4, 7))
press_y = random.randint(0, 5) + math.floor(1420 + random.randint(0, 2))
os.system(\"%s shell input swipe %i %i %i %i %s\" % (adb_path, press_x, press_y, press_x, press_y, swipe_time + press_time))
print(\"第 %i 局跳了 %f\" % (i + 1, distance))
time.sleep(random.randint(2, 4) + random.random())
pull_screenshot()
img = cv2.imread(local_path + \"/screenshot.png\", cv2.IMREAD_COLOR)
value = match(img, cv2.imread(temp2_path, cv2.IMREAD_GRAYSCALE))
if value >= 0.95: # 如果出现游戏结束画面
break
# main函数
if __name__ == '__main__':
jump(3)
第三步:测试应用
测试应用前需要先打开USB调试功能以及应用安装来源选项,这两个选项可以在手机设置中找到,这样才能达到应用与手机之间的连接。
然后打开游戏,在命令行中执行以下命令:
python main.py
这样我们就可以看到每一轮跳跃的距离,以及最终游戏结束时所跳的总距离。
当然这只是个小小的Demo,我们可以根据自己的需求不断完善它,剩下的交给你们了,祝大家学习愉快。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至3237157959@qq.com 举报,一经查实,本站将立刻删除。