Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

先装一下

思路:这个脚本全部围绕着找到两个点的坐标(棋子中心点,目标中心点),找到这两个坐标,在计算距离d,在除以速度v,得到需要跳跃的时间t,在用python控制鼠标按下时间。整个过程其实是偏向纯数学的东西是不是觉得很简单。接着看,找到两个点的坐标我想了3种方法,鄙人取名为(老实憨厚)(精准又优雅)(投机取巧)

进去正题。

准备东西

1,用python写的,主要是C++的opencv太难写了,但是如果追求速度,害得是C++,python有点慢

2,安利两个实用工具,也是要用到的工具

Snipaste,截图贴图工具,截的图可以贴在旁边,非常方便。

OneQuick,窗口置顶工具,想让某个窗口置顶就鼠标移上去按Win+G,默认的好像是Win+G。

都是微软的。

3,微信电脑版,要用跳一跳。

over。

1,老实憨厚版

找棋子坐标

看下面的图,分析特点。

棋子桌标就是棋子的脚底下的中心点,找到他其实很简单,用opencv库的模板匹配。

我们首先要准备一张棋子的模板,如图

距离把它包裹住并且贴紧,然后用它来匹配原图像,

代码:

1
2
3
4
5
6
7
8
9
10
11
import cv2
img = cv2.imread("f:\Snipaste_2022.png")
template = cv2.imread('f:\Snipaste_2022_0.png')
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
h, w = template.shape[:2]
top_left = max_loc
center_loc = ((int)(max_loc[0] + w ), int(max_loc[1] + h ) )
cv2.rectangle(img, max_loc, center_loc, (0, 0, 255), 1, 8)
cv2.imshow("sad",img)
cv2.waitKey(0

结果像这样:

这个矩形的坐标是知道的,找到脚底下的坐标不言而喻了吧。

找目标点坐标

首先我们要知道,为了找到目标点坐标,哪些东西有用哪些东西没用。

看图:

棋子一下的部分不需要吧,记分数字上面的东西不需要吧,当然棋子的右半身也不需要吧,需要的部分只有棋子的右上部分,所以我们需要把一整张图剪切成那样,

接下来的工作就是用到opencv的边缘检测了

可以发现,这一次目标点是在右边的,所以我们只要找到最上方的点和最右方的点,如果目标点是在左边,那么我们只要找到最左边点和最上方的点,就可以简单得出目标点中心的坐标。

这样两个点的坐标就找到了,在计算两点的位置,大致就完成了。

完整代码:

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import math
from PIL import ImageGrab
from pynput.mouse import Button, Controller
import time
import cv2
import numpy as np

pt1 = (0, 0)
pt2 = (291, 0)
pt3 = (291, 428)
pt4 = (140, 516)
pt5 = (0, 516)


def get_center(img_rgb, template):
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
h, w = template.shape[:2]
top_left = max_loc
center_loc = ((int)(max_loc[0] + w / 2), int(max_loc[1] + h / 2) + 36)
a = cv2.circle(img_rgb, center_loc, 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)

return max_loc, h, w, center_loc


def math_point(p, center_loc, k):
# y=-0.58*x+a*0.58+b
y1 = k * center_loc[0] + p[0] * (-k) + p[1]
y1 = int(y1)
x2 = (p[0] * (-k) + p[1] - center_loc[1]) / (-k)
x2 = int(x2)

p1 = (center_loc[0], y1)
p2 = (x2, center_loc[1])
return p1, p2


def get_img1(img, max_loc, center_loc, k):
pt_1 = ()
pt_2 = ()
pt_3 = ()
pt_4 = ()
pt_5 = ()
if k < 0:
pt_1 = (0, 0)
pt_2 = (center_loc[0], 0)
pt_3, pt_4 = math_point(max_loc, center_loc, k)
pt_5 = (0, center_loc[1])
else:
pt_1 = (0, 0)
pt_2 = (img.shape[1], 0)
pt_3 = (img.shape[1], img.shape[0])
p = (max_loc[0] + 40, max_loc[1])
pt_5, pt_4 = math_point(p, center_loc, k)

blank = np.zeros(img.shape[:2], dtype='uint8')
cv2.circle(blank, pt_1, 1, (255, 255, 255), -1)
cv2.circle(blank, pt_2, 1, (255, 255, 255), -1)
cv2.circle(blank, pt_3, 1, (255, 255, 255), -1)
cv2.circle(blank, pt_4, 1, (255, 255, 255), -1)
cv2.circle(blank, pt_5, 1, (255, 255, 255), -1)
triangle_cnt = np.array([pt_1, pt_2, pt_3, pt_4, pt_5])

mask = cv2.drawContours(blank, [triangle_cnt], 0, (255, 255, 255), -1)
m = cv2.bitwise_and(img, img, mask=mask)
return m


lowThreshold = 0
max_lowThreshold = 255
ratio = 3
kernel_size = 3


def find_left(masked, gray):
# detected_edges = cv2.GaussianBlur(gray, (3, 3), 0)
detected_edges = cv2.Canny(masked, 30, 30 * ratio, apertureSize=kernel_size)
dst = cv2.bitwise_and(masked, masked, mask=detected_edges) # just add some colours to edges from original image.

lap = masked.copy()
contours, hierarchy = cv2.findContours(image=detected_edges, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
max = 50000
min = 50000
a = []
b = []

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] < min:
min = contours[i][j][k][1]
if contours[i][j][k][0] < max:
max = contours[i][j][k][0]
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] == min:
a = contours[i][j][k]
if contours[i][j][k][0] == max:
b.append(contours[i][j][k])

Min = 50000
c = []
for i in range(len(b)):
if b[i][1] < Min:
c = b[i]

c = [c[0], c[1] + 5]
d = [a[0], c[1]]

center = (a[0], int((a[1] + d[1]) / 2))
print(center)
cv2.circle(lap, (center[0], center[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (a[0], a[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (c[0], c[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (d[0], d[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.drawContours(image=lap, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=1,
lineType=cv2.LINE_AA)
cv2.imshow('canny demo', lap)
return center


def find_right(masked, gray):
# detected_edges = cv2.GaussianBlur(gray, (3, 3), 0)
detected_edges = cv2.Canny(masked, 45, 40 * ratio, apertureSize=kernel_size)
dst = cv2.bitwise_and(masked, masked, mask=detected_edges) # just add some colours to edges from original image.
lap = masked.copy()
contours, hierarchy = cv2.findContours(image=detected_edges, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
max = 0
min = 50000
a = []
b = []

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] < min:
min = contours[i][j][k][1]
if contours[i][j][k][0] > max:
max = contours[i][j][k][0]
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] == min:
a = contours[i][j][k]
if contours[i][j][k][0] == max:
b.append(contours[i][j][k])

Min = 50000
c = []
for i in range(len(b)):
if b[i][1] < Min:
c = b[i]

d = (a[0], c[1])

cv2.circle(lap, (a[0], a[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (c[0], c[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (d[0], d[1]), 1, (0, 255, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.drawContours(image=lap, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=1,
lineType=cv2.LINE_AA)
cv2.imshow('canny demo', lap)
return d


def distance(a, b):
p1 = np.array(a)
p2 = np.array(b)
p3 = p1 - p2
print(p3)
p4 = math.hypot(p3[0], p3[1])
return p4


def get_time(length):
time = length / 306.001158116
return time


def dian_ji(time1):
mouse = Controller()
mouse.press(Button.left)
time.sleep(time1)
mouse.release(Button.left)


a = 0
while 1:
#把你的跳一跳放在左上角位置,对齐。
ss_region = (1, 2, 450, 855) # 距离左上右下的像素
ss_img = ImageGrab.grab(ss_region)
#f盘自己创建一个img文件夹,可以换其他盘
p = "F:\\img\\"
a = a + 1
path = p + "PILImage" + str(a) + ".jpg"

ss_img.save(path)
img = cv2.imread(path)

img_rgb = img.copy()
template = cv2.imread('F:\Snipaste_2022-07-21_03-20-01.png')#这是棋子的图片
max_loc, h, w, center_loc = get_center(img, template)

img = cv2.resize(img, (450, 855), interpolation=cv2.INTER_CUBIC)#这个(450,855)是跳一跳图片的大小,把截图按住就可以显示大小

center = ()
length = 0
# 左
if center_loc[0] > img.shape[1] / 2:
masked = get_img1(img, max_loc, center_loc, -1)
masked = masked[200:center_loc[1], 0:center_loc[0] - 15]
gray = cv2.cvtColor(masked, cv2.COLOR_BGR2GRAY)
masked = cv2.GaussianBlur(masked, (3, 3), 0)
center = find_left(masked, gray)
length = distance((masked.shape[1] + 15, masked.shape[0]), center)
time1 = get_time(length)
print("时间", time1)
print("距离", length)
dian_ji(time1)
else:
masked = get_img1(img, max_loc, center_loc, 1)
masked = masked[200:center_loc[1], center_loc[0] + 15:img.shape[1]]
gray = cv2.cvtColor(masked, cv2.COLOR_BGR2GRAY)
masked = cv2.GaussianBlur(masked, (3, 3), 0)
center = find_right(masked, gray)
length = distance((0 - 15, masked.shape[0]), center)
time1 = get_time(length)
print("时间", time1)
print("距离", length)
dian_ji(time1)

cv2.imshow("mask", masked)
cv2.waitKey(500)
time.sleep(1)

具体怎么操作末尾再说,这个方法可以用,但不推荐,所以注释就没写,玩玩就彳亍。

2,精准又优雅版

找棋子坐标都是一样的,不用多说了。

重点是找目标点。

目标点坐标

随便拿个图:

仔细看目标方块,它的表面是有颜色的,这张图是绿色,表示为(R,G,B),比如黑色(0,0,0),白色(255,255,255),每个颜色都有个颜色值,所以从这里入手。

一张图片是有很多像素点组成的,一个像素点就代表一个颜色,一张大小为(50x50)的图片有50*50=2500个像素点,我们可以从图片的有效区域(经过裁剪后的区域),从上往下扫描,找到第一个颜色变化最大的点,那么这个点就是目标方块表面的颜色(比如这张图第一个颜色变化最大的点是直角那个点,是绿色(100,149,105)),然后我们 把整张图不是(100,149,105)的像素点全部设置为(0,0,0)也就是涂黑

涂黑后

当然,这个情况就属于特殊情况,因为棋子的地面也是相同的颜色,所以需要进行特殊处理,整篇代码有80%都是在处理特殊情况,因为跳一跳每一次都是不确定的,所以需要考虑全面。

我换一张正常的,

涂黑后

这张图就比较明显了,只有两个颜色区域,有的小伙伴就发动大脑了,找到中点坐标现在很简单啊,找到绿色的最左边和最后边,在求个中点不就行了?

可以,想法很好,但是如果是上面那种情况呢,左下角存在影响因素,这样最左边的坐标就是(0,y),显然算出来的是错的,

所以我们依然得用边缘检测,然后再用特殊处理把明显不是我们需要的信息过滤掉,最后边沿检测算出来的结果如下:

给大家看一种典型的特殊情况吧,

算出来依然是准确的,特殊情况不止这一种类型,你永远不知道下一步方块是什么tm奇葩形状,什么tm奇葩位置

也永远不知道下一步会出现什么tm奇葩特殊情况,我遇到过最奇葩的:

这俩大小完全粘在一起了。

处理这些情况需要大量的算法,所以我说有80%的代码都在处理这些。

完整代码:

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import math
import shutil
import os
import win32con
import win32gui
from PIL import ImageGrab
from pynput.mouse import Button, Controller
import time
import cv2
import pyscreenshot
import numpy as np
import pyautogui
from numpy import *


def math_point(p, center_loc, k):
# y=-0.58*x+a*0.58+b
y1 = k * center_loc[0] + p[0] * (-k) + p[1]
y1 = int(y1)
x2 = (p[0] * (-k) + p[1] - center_loc[1]) / (-k)
x2 = int(x2)

p1 = (center_loc[0], y1)
p2 = (x2, center_loc[1])
return p1, p2


def get_img1(img, k):
pt_1 = ()
pt_2 = ()
pt_3 = ()
pt_4 = ()
pt_5 = ()
if k < 0:
pt_1 = (img.shape[1], img.shape[0])
pt_2 = (img.shape[1], img.shape[0] - 30)
pt_3 = (img.shape[1] - 50, img.shape[0])
else:
pt_1 = (0, img.shape[0] - 30)
pt_2 = (0, img.shape[0])
pt_3 = (50, img.shape[0])

blank = np.ones(img.shape[:2], dtype='uint8')
cv2.circle(blank, pt_1, 1, (0, 0, 0), -1)
cv2.circle(blank, pt_2, 1, (0, 0, 0), -1)
cv2.circle(blank, pt_3, 1, (0, 0, 0), -1)

triangle_cnt = np.array([pt_1, pt_2, pt_3])

mask = cv2.drawContours(blank, [triangle_cnt], 0, (0, 0, 0), -1)
m = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("m", m)
hwnd = win32gui.FindWindow(None, "m") # 获取句柄,然后置顶
CVRECT = cv2.getWindowImageRect("m")
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 610, 125, m.shape[1] + 10, m.shape[0] + 40,
win32con.SWP_SHOWWINDOW)
return m


def distance(a, b):
p1 = np.array(a)
p2 = np.array(b)
p3 = p1 - p2
print(p3)
p4 = math.hypot(p3[0], p3[1])
return p4


def get_time(length):
time = length / 306.001158116
return time


def dian_ji(time1):
mouse = Controller()
mouse.press(Button.left)
time.sleep(time1)
mouse.release(Button.left)


def get_center(img_rgb, template):
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
h, w = template.shape[:2]
top_left = max_loc
center_loc = ((int)(max_loc[0] + w / 2), int(max_loc[1] + h / 2) + 36)
a = cv2.circle(img_rgb, center_loc, 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)

return max_loc, h, w, center_loc


def find_area(p1, p2):
return abs(p1[0] - p2[0]) * abs(p1[1] - p2[1])


def pd_is_max(contours):
Max = 0
Min = 50000
a = []
b = []
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][0] < Min:
Min = contours[i][j][k][0]
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][0] > Max:
Max = contours[i][j][k][0]

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][0] == Min:
a = contours[i][j][k]
if contours[i][j][k][0] == Max:
b.append(contours[i][j][k])
M_in = 50000
c = []
for i in range(len(b)):
if b[i][1] < M_in:
c = b[i]
return a, c


def find_left(masked):
# detected_edges = cv2.GaussianBlur(gray, (3, 3), 0)
detected_edges = cv2.Canny(masked, 30, 30 * 3, apertureSize=3)
dst = cv2.bitwise_and(masked, masked, mask=detected_edges) # just add some colours to edges from original image.

lap = masked.copy()
contours, hierarchy = cv2.findContours(image=detected_edges, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
max = 50000
min = 50000
a = []
b = []

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] < min:
min = contours[i][j][k][1]
if contours[i][j][k][0] < max:
max = contours[i][j][k][0]
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] == min:
a = contours[i][j][k]
if contours[i][j][k][0] == max:
b.append(contours[i][j][k])

Min = 50000
c = []
for i in range(len(b)):
if b[i][1] < Min:
c = b[i]

c = [c[0], c[1] + 5]
d = [a[0], c[1]]

center = (a[0], int((a[1] + d[1]) / 2))

cv2.circle(lap, (center[0], center[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (a[0], a[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (c[0], c[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (d[0], d[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.drawContours(image=lap, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=1,
lineType=cv2.LINE_AA)
cv2.imshow('canny demo', lap)
return center


def find_right(masked):
# detected_edges = cv2.GaussianBlur(gray, (3, 3), 0)
detected_edges = cv2.Canny(masked, 45, 40 * 3, apertureSize=3)
dst = cv2.bitwise_and(masked, masked, mask=detected_edges) # just add some colours to edges from original image.
lap = masked.copy()
contours, hierarchy = cv2.findContours(image=detected_edges, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
max = 0
min = 50000
a = []
b = []

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] < min:
min = contours[i][j][k][1]
if contours[i][j][k][0] > max:
max = contours[i][j][k][0]
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][1] == min:
a = contours[i][j][k]
if contours[i][j][k][0] == max:
b.append(contours[i][j][k])

Min = 50000
c = []
for i in range(len(b)):
if b[i][1] < Min:
c = b[i]

d = (a[0], c[1])

cv2.circle(lap, (a[0], a[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (c[0], c[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(lap, (d[0], d[1]), 1, (0, 255, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.drawContours(image=lap, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=1,
lineType=cv2.LINE_AA)
cv2.imshow('canny demo', lap)
return d


def RemoveDir(filepath):
'''
如果文件夹不存在就创建,如果文件存在就清空!

'''
if not os.path.exists(filepath):
os.mkdir(filepath)
else:
shutil.rmtree(filepath)
os.mkdir(filepath)


def get_tol(img):
img_copy = img.copy()
arr = []
A = int(img.shape[0])
B = int(img.shape[1])
for y in range(A):
for x in range(B):
if x != 0:
temp1 = int(img[y][x][0]) - int(img[y][x - 1][0])
temp2 = int(img[y][x][1]) - int(img[y][x - 1][1])
temp3 = int(img[y][x][2]) - int(img[y][x - 1][2])
if abs(temp1) > 5 or abs(temp2) > 5 or abs(temp3) > 5:
a = (x, y)
arr.append(a)

# cv2.circle(img, (x, y), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)

co = img[arr[0][1] + 3][arr[0][0] + 1]
# for i in arr:
# cv2.circle(img_copy, (i[0], i[1]), 1, (255, 0, 255), thickness=3, lineType=cv2.LINE_AA)

print(type(co))
arr1 = []
for y in range(A):
for x in range(B):
# not (img[y][x] == co).all():
if abs((int(img[y][x][0]) - int(co[0]))) >= 6 or abs(int(img[y][x][1]) - int(co[1])) >= 6 or abs(
int(img[y][x][2]) - int(co[2])) >= 6:
b = (x, y)
arr1.append(b)

for i in arr1:
cv2.circle(img_copy, (i[0], i[1]), 1, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)
arry = []
for y in range(A):
for x in range(B):
# not (img[y][x] == co).all():
if abs(int(img[y][x][0]) - 200) <= 2 and abs(int(img[y][x][1]) - 220) <= 2 and abs(
int(img[y][x][2]) - 240) <= 2:
b = (x, y)
arry.append(b)
if len(arry) > 8:
for i in arry:
cv2.circle(img_copy, (i[0], i[1]), 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
a = img_copy

return a


def get_c(img, place, img_fuzhi):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (3, 3), cv2.BORDER_DEFAULT)
img = cv2.Canny(img, 50, 50)
img = cv2.dilate(img, (10, 10), iterations=2)
img = cv2.erode(img, (10, 10), iterations=1)

contours1, hierarchy = cv2.findContours(image=img, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image=img_copy2, contours=contours1, contourIdx=-1, color=(255, 0, 0), thickness=1,
lineType=cv2.LINE_AA)
p1, p2 = pd_is_max(contours1)
# cv2.circle(img, (200, 157), 1, (255, 255, 255), thickness=5, lineType=cv2.LINE_AA)

area = []
contours = []

print("length", len(contours1))
for obj in contours1:
if cv2.contourArea(obj) != 0:
print(cv2.contourArea(obj))
area.append(cv2.contourArea(obj))
print("area", len(area))
print("Variance of array : ", np.std(area))
if len(area) != 0:

zui_da = max(area)
zui_xiao = min(area)

if len(area) == 2:
# contours.append(contours1[1])

for obj in contours1:
max_dimension1 = np.amax(obj, axis=0)
if img.shape[1] - max_dimension1[0][1] > 3:
contours.append(obj)

if 0 <= len(area) < 2 or 2 < len(area) <= 4:
if np.std(area) < 50:
for obj in contours1:
if cv2.contourArea(obj) != 0:
contours.append(obj)
else:
for obj in contours1:
if cv2.contourArea(obj) == max(area):
contours.append(obj)

else:
if np.std(area) < 160:
for obj in contours1:
if cv2.contourArea(obj) != 0:
contours.append(obj)
else:
for obj in contours1:
if cv2.contourArea(obj) == zui_da:
contours.append(obj)

print("len", len(contours))
Max = 0
Min = 50000
a = []
b = []

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][0] < Min:
Min = contours[i][j][k][0]
for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][0] > Max:
Max = contours[i][j][k][0]

for i in range(len(contours)):
for j in range(len(contours[i])):
for k in range(len(contours[i][j])):
if contours[i][j][k][0] == Min:
a = contours[i][j][k]
if contours[i][j][k][0] == Max:
b.append(contours[i][j][k])
M_in = 50000
c = []
for i in range(len(b)):
if b[i][1] < M_in:
c = b[i]
print(a)
print(c)
d1 = int((a[0] + c[0]) / 2)
d2 = int((a[1] + c[1]) / 2)
d = [d1, d2]

cv2.circle(img_copy2, (a[0], a[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(img_copy2, (c[0], c[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.circle(img_copy2, (d[0], d[1]), 1, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)

cv2.imshow("csa", img_copy2)
hwnd1 = win32gui.FindWindow(None, "csa") # 获取句柄,然后置顶
CVRECT = cv2.getWindowImageRect("csa")
win32gui.SetWindowPos(hwnd1, win32con.HWND_TOPMOST, 900, 120, img_copy2.shape[1] + 10, img_copy2.shape[0] + 40,
win32con.SWP_SHOWWINDOW)
cv2.imshow("img", img)
hwnd2 = win32gui.FindWindow(None, "img") # 获取句柄,然后置顶
CVRECT1 = cv2.getWindowImageRect("img")
win32gui.SetWindowPos(hwnd2, win32con.HWND_TOPMOST, 1200, 120, img.shape[1] + 10, img.shape[0] + 40,
win32con.SWP_SHOWWINDOW)
return d
else:
img_fuzhi = cv2.cvtColor(img_fuzhi, cv2.COLOR_BGR2GRAY)
img_fuzhi = cv2.GaussianBlur(img_fuzhi, (3, 3), cv2.BORDER_DEFAULT)
if place == "left":
d = find_left(img_fuzhi)
return d
else:
d = find_right(img_fuzhi)
return d


a = -1
p = "F:\\img\\"
RemoveDir(p)
image = pyscreenshot.grab(bbox=(1, 2, 450, 855))
image.save("f:\\img\\Snipaste_2022.png")
while 1:
pyautogui.press('F1')
pyautogui.hotkey('Ctrl', 'Shift', 's')
a = a + 1
path = p + "Snipaste_2022_" + str(a) + ".png"
img = cv2.imread(path)
img_rgb = img.copy()
template = cv2.imread('F:\Snipaste_2022_0.png')
max_loc, h, w, center_loc = get_center(img, template)
img = cv2.resize(img, (450, 855), interpolation=cv2.INTER_CUBIC)
center = ()
length = 0
if center_loc[0] > img.shape[1] / 2:
img = img[300:center_loc[1], 0:center_loc[0] - 15]
img_copy2 = img.copy()
img_fuzhi = img.copy()
place = "left"
getimg = get_tol(img)
get_img = get_img1(getimg, -1)

center1 = get_c(get_img, place, img_fuzhi)
center2 = [img.shape[1] + 15, img.shape[0]]
length = distance(center1, center2)
time1 = get_time(length)
print("时间", time1)
print("距离", length)
dian_ji(time1)

else:

img = img[300:center_loc[1], center_loc[0] + 15:img.shape[1]]
img_copy2 = img.copy()
img_fuzhi = img.copy()
place = "r"
getimg = get_tol(img)
get_img = get_img1(getimg, 1)
center1 = get_c(get_img, place, img_fuzhi)
center2 = [0 - 15, img.shape[0]]
length = distance(center1, center2)
time1 = get_time(length)
print("时间", time1)
print("距离", length)
dian_ji(time1)

cv2.waitKey(600)
time.sleep(0.8)

这个代码其实是最终版本,整个跳一跳有一个形状这个方法不能处理,但是老实憨厚版可以处理(目前我没截到那个形状,截到了我再补上。)所以我直接把这俩整合到一起了,一个跳一跳跑两个程序。

怎么操作最后再说

3,投机取巧版

这个方法我只说说思路

跳一跳有个规律,当你这次跳跃刚好命中中心,那么下一次跳跃的目标点中心就会显示有个白点

我们同样可以用扫描像素点的方法,找到第一个像素点是白色的位置,那么这个点的位置就直接是中心的位置,两个点直接就找出来了,But,如果你下次跳的不是正中心呢,直接G。所以我说这是投机取巧版。

得到速度

如果不是拯救者的电脑(因为我是):

我直接明说,跳一跳的开局两个方块的距离的固定的

打开你们的windows自带的画图工具,手动找到两个点的坐标,手动算距离,再用下面的代码测试

1
2
3
4
5
6
7
import time
from pynput.mouse import Button, Controller
mouse = Controller()
mouse.press(Button.left)#模拟按压
time.sleep()#模拟按下时间。从0.5开始测试,直到跳到正中心的位置
mouse.release(Button.left)#模拟松开

再用手动算出来的距离除以时间,得到速度,这个速度就是之后要用的速度。

拯救者的就不用了,直接copy

操作

右键之前提到的Snipaste的首选项,

,改成这样。

把跳一跳放在一个合适的位置,用之前提到的OneQuick工具,Win+G将跳一跳窗口置顶,防止点其他地方窗口没了。

把鼠标放在跳一跳窗口内,然后别动了,按shift+f10启动程序。开始跑。

几千上万分不成问题。

在没改源码的情况下这个外挂算是比较nb了。

评论