专栏名称: python
隔天更新python文章,我希望用我的努力换来劳动的成果帮助更多的人掌握一门技术,因此我要更加努力。
目录
相关文章推荐
Python爱好者社区  ·  英伟达憾失DeepSeek关键人才?美国放走 ... ·  昨天  
Python爱好者社区  ·  离谱!下载DeepSeek最高判刑20年? ·  2 天前  
Python爱好者社区  ·  1885页的Python完全版电子书 ·  3 天前  
Python爱好者社区  ·  多模态,杀疯了 ·  2 天前  
Python开发者  ·  请立即拿下软考证书(政策风口) ·  4 天前  
51好读  ›  专栏  ›  python

利用Python实现阴阳师自动抽卡,SSR手到擒来!

python  · 公众号  · Python  · 2020-02-27 22:35

正文



不简单的具体实现

1、通过adb截图判断是否在抽卡界面

2、在的话,使用matchTemplate()函数识别开始召唤的按钮

3、点击,画符

4、同样使用matchTemplate()函数和模板对比识别SSR和SR并点击

5、若识别到“十连召唤”(即抽卡结束)点击返回

开始抽卡的时候要识别的区域

出现SSR或SR要识别的区域

结束时要识别的区域

这里主要通过adb来实现对手机的模拟点按和滑动

(手机记得要开USB侦错啊QWQ)

import os
#模拟点按
def tap(x0, y0):
    cmdTap = 'adb shell input tap {x1} {y1}'.format(
        x1=x0,
        y1=y0
    )
    print(cmdTap)
    os.system(cmdTap)
#模拟滑动(用来画符的)
def swipe(x0, y0, x1, y1, delay0):
    cmdSwipe = 'adb shell input swipe {x2} {y2} {x3} {y3} {delay1}'.format(
        x2=x0,
        y2=y0,
        x3=x1,
        y3=y1,
        delay1=delay0
    )
    print(cmdSwipe)
    os.system(cmdSwipe)
#截图并返回图片
def screenshot():
    os.system('adb shell screencap -p /sdcard/sh.png')
    os.system('adb pull /sdcard/sh.png .')
    return "sh.png"

上面这个基本上是对手机的所有操作OWO。
(我将这个单独保存在了项目目录的/lib/ats.py中,这样主程序就可以直接调用啦XD)
然后来讲一下这里最核心的操作:图像识别orz

#我们要用的其实就只有这一个库
import cv2
#不过我们还是要。。。
import time
import random
import lib.ats #233 这个就是刚才那段代码
import numpy as np

好的我们先来个例子,就从识别开始抽卡的按钮开始。。。

def start(sh):
    #一看就知道是导入图片orz
    imgSTART = cv2.imread(sh, 0)
    templateSTART = cv2.imread('res/START.png'0#我的模板保存在了项目目录的/res文件夹里
    #和模板对比
    resSTART = cv2.matchTemplate(imgSTART, templateSTART, cv2.TM_CCOEFF_NORMED)
    thresholdSTART = 0.85
    pos = []
    #如果result大于threshold才可以执行(不在界面你抽个啥)
    if (resSTART >= thresholdSTART).any():
        loc = np.where(resSTART >= thresholdSTART)
        for pt in zip(*loc[::-1]): #刚学Python没多久,我只知道这个压缩后切片QAQ
            pos.append(pt) #更新list
        return pos #返回按钮位置
    else:
        return 0

然后是抽到SSR的时候

(其实这个完全可以不写,因为SSR根本不存在(手动滑稽)XD)







请到「今天看啥」查看全文