forked from hanmin0822/RaphaelScriptHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaphaelScriptHelper.py
161 lines (143 loc) · 5.8 KB
/
RaphaelScriptHelper.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import ImageProc, ADBHelper, random, time, cv2
import settings as st
# support PC
from PIL import ImageGrab
import pyautogui
# 0为PC环境,1为安卓设备
deviceType = 1
deviceID = ""
def random_delay():
t = random.uniform(st.randomDelayMin, st.randomDelayMax)
print("【随机延时】将随机延时 {0} 秒".format(t))
time.sleep(t)
def delay(t):
print("【主动延时】延时 {0} 秒".format(t))
time.sleep(t)
def random_pos(pos):
x, y = pos
rand = random.randint(1, 10000)
if rand % 2 == 0:
x = x + random.randint(0, st.touchPosRange)
else:
x = x - random.randint(0, st.touchPosRange)
rand = random.randint(1, 10000)
if rand % 2 == 0:
y = y + random.randint(0, st.touchPosRange)
else:
y = y - random.randint(0, st.touchPosRange)
return (x, y)
# 智能模拟点击某个点,将会随机点击以这个点为中心一定范围内的某个点,并随机按下时长
def touch(pos):
randTime = random.randint(0, st.touchDelayRange)
_pos = random_pos(pos)
print("【模拟点击】点击坐标 {0} {1} 毫秒".format(_pos, randTime))
if randTime < 10:
ADBHelper.touch(deviceID, _pos)
else:
ADBHelper.longTouch(deviceID, _pos, randTime)
def touch_pc(x, y):
# 获取当前屏幕分辨率
screen_width, screen_height = pyautogui.size()
# 将鼠标移动到(x, y)处并点击左键
print("【模拟点击】点击坐标 ({0},{1})".format(x, y))
pyautogui.moveTo(x, y)
pyautogui.click()
# 智能模拟滑屏,给定起始点和终点的二元组,模拟一次随机智能滑屏
def slide(vector):
startPos, stopPos = vector
_startPos = random_pos(startPos)
_stopPos = random_pos(stopPos)
randTime = random.randint(st.slideMinVer, st.slideMaxVer)
print("【模拟滑屏】使用 {0} 毫秒从坐标 {1} 滑动到坐标 {2}".format(randTime, _startPos, _stopPos))
ADBHelper.slide(deviceID, _startPos, _stopPos, randTime)
# 截屏,识图,返回坐标
def find_pic(target, returnCenter = False):
ADBHelper.screenCapture(deviceID, st.cache_path + "screenCap.png")
time.sleep(0.1)
if returnCenter == True:
leftTopPos = ImageProc.locate(st.cache_path + "screenCap.png", target, st.accuracy)
img = cv2.imread(target)
centerPos = ImageProc.centerOfTouchArea(img.shape, leftTopPos)
return centerPos
else:
leftTopPos = ImageProc.locate(st.cache_path + "screenCap.png", target, st.accuracy)
return leftTopPos
# 截屏,识图,返回坐标
def find_pic_pc(target, returnCenter = False):
screenshot = ImageGrab.grab()
full_path = st.cache_path + "screenCap.png"
screenshot.save(full_path)
print("【截图】已保存截图至", full_path)
time.sleep(0.1)
if returnCenter == True:
leftTopPos = ImageProc.locate(st.cache_path + "screenCap.png", target, st.accuracy)
img = cv2.imread(target)
centerPos = ImageProc.centerOfTouchArea(img.shape, leftTopPos)
return centerPos
else:
leftTopPos = ImageProc.locate(st.cache_path + "screenCap.png", target, st.accuracy)
return leftTopPos
def find_target_pic_pc(source, target, returnCenter = False):
if returnCenter == True:
leftTopPos = ImageProc.locate(source, target, st.accuracy)
img = cv2.imread(target)
centerPos = ImageProc.centerOfTouchArea(img.shape, leftTopPos)
return centerPos
else:
leftTopPos = ImageProc.locate(source, target, st.accuracy)
return leftTopPos
# 截屏,识图,返回所有坐标
def find_pic_all(target):
ADBHelper.screenCapture(deviceID, st.cache_path + "screenCap.png")
time.sleep(0.1)
leftTopPos = ImageProc.locate_all(st.cache_path + "screenCap.png", target, st.accuracy)
return leftTopPos
def find_pic_all_pc(target):
screenshot = ImageGrab.grab()
full_path = st.cache_path + "screenCap.png"
screenshot.save(full_path)
print("【截图】已保存截图至", full_path)
time.sleep(0.1)
leftTopPos = ImageProc.locate_all(st.cache_path + "screenCap.png", target, st.accuracy)
return leftTopPos
# 寻找目标区块并在其范围内随机点击
def find_pic_touch(target):
leftTopPos = find_pic(target)
if leftTopPos is None:
print("【识图】识别 {0} 失败".format(target))
return False
print("【识图】识别 {0} 成功,图块左上角坐标 {1}".format(target, leftTopPos))
img = cv2.imread(target)
tlx, tly = leftTopPos
h_src, w_src, tongdao = img.shape
x = random.randint(tlx, tlx + w_src)
y = random.randint(tly, tly + h_src)
touch((x, y))
return True
def find_pic_touch_pc(target, touchCenter = False):
leftTopPos = find_pic_pc(target, touchCenter)
if leftTopPos is None:
print("【识图】识别 {0} 失败".format(target))
return False
print("【识图】识别 {0} 成功,图块左上角坐标 {1}".format(target, leftTopPos))
img = cv2.imread(target)
tlx, tly = leftTopPos
if touchCenter:
touch_pc(tlx, tly)
else:
h_src, w_src, tongdao = img.shape
x = random.randint(tlx, tlx + w_src)
y = random.randint(tly, tly + h_src)
touch_pc(x, y)
return True
# 寻找目标区块并将其拖动到某个位置
def find_pic_slide(target,pos):
leftTopPos = find_pic(target)
if leftTopPos is None:
print("【识图】识别 {0} 失败".format(target))
return False
print("【识图】识别 {0} 成功,图块左上角坐标 {1}".format(target, leftTopPos))
img = cv2.imread(target)
centerPos = ImageProc.centerOfTouchArea(img.shape,leftTopPos)
slide((centerPos, pos))
return True