碧蓝航线翻牌游戏脚本

匹配相同的牌时是裁下每张牌后,直接对比每张图片的数组是否相同
翻牌的时候想要同时点两张牌,但是查了下adb好像没有支持mult touch,然后想到在模拟器上给每个牌的位置设置快捷键,翻牌的时候调用win32api点击对应的快捷键来翻牌.
最终成绩4.45秒.b站视频:https://www.bilibili.com/video/av44287472/

下面是完整代码.其中vkcode.VK_CODE是win32按键码映射表,在这篇博客
82~89行那些几个sleep的参数是试出来的,再小就容易出错了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os

import cv2
import numpy as np
import win32api
import win32con
from vkcode import VK_CODE


class Blhx:
def __init__(self, adbPATH, sharedPATH):
self.adb = adbPATH
self.sharedPATH = sharedPATH

def getScreen(self, filePath="/sdcard/Pictures/tmp.png"):
os.system(f"{self.adb} -e shell screencap -p {filePath}")
time.sleep(0.5)
return cv2.imread(f"{self.sharedPATH}\\tmp.png")

def tap(self, X, Y):
os.system(f"{self.adb} -e shell input tap {X} {Y}")

def cutEvery(self, img):
ims = []
matched = []
for y in range(3):
for x in range(6):
startX = 148 * x + 238
startY = 162 * y + 179
endX = startX + 90
endY = startY + 129
im = img[startY:endY, startX:endX, :]
thisImInfo = (y, x, im)
hasmatch = False
for i, imgInfo in enumerate(ims):
difference = cv2.subtract(im, imgInfo[2])
result = not np.any(difference)
if result:
hasmatch = True
matched.append(imgInfo[:2])
matched.append(thisImInfo[:2])
del ims[i]
continue
if not hasmatch:
ims.append(thisImInfo)
return matched


keyboardsMap = [
"QWERTY".lower(),
"ASDFGH".lower(),
"ZXCVBN".lower()
]


def mian():
blhx = Blhx(r"E:\programfiles\adb\adb.exe", "J:\ldmnqshare")

blhx.tap(800, 485)
time.sleep(1.1)
print("jietu")
im = blhx.getScreen()
pos = blhx.cutEvery(im)
ks = []
for i, p in enumerate(pos):
k = keyboardsMap[p[0]][p[1]]
ks.append(k)
kl = ks.__len__()
if not kl == 18:
print("没匹配好")
return
time.sleep(1)
for x in range(9):
k0 = 2 * x
k1 = k0 + 1
kNode0 = ks[k0]
kNode1 = ks[k1]
win32api.keybd_event(VK_CODE[kNode0], 0, 0, 0)
time.sleep(0.04)
win32api.keybd_event(VK_CODE[kNode1], 0, 0, 0)
time.sleep(0.04)
win32api.keybd_event(VK_CODE[kNode0], 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.04)
win32api.keybd_event(VK_CODE[kNode1], 0, win32con.KEYEVENTF_KEYUP, 0)
print("tap")
time.sleep(0.375)


if __name__ == '__main__':
mian()