Skip to content

Commit cd1de42

Browse files
committed
格式调整
1 parent 49e0de1 commit cd1de42

File tree

3 files changed

+42
-45
lines changed

3 files changed

+42
-45
lines changed

WechatJump/Config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import re
99

10+
1011
def open_accordant_config():
1112
'''
1213
调用配置文件
@@ -32,7 +33,7 @@ def _get_screen_size():
3233
'''
3334
size_str = os.popen('adb shell wm size').read()
3435
if not size_str:
35-
print('请安装ADB及驱动并配置环境变量')
36+
print('请安装 ADB 及驱动并配置环境变量')
3637
sys.exit()
3738
m = re.search(r'(\d+)x(\d+)', size_str)
3839
if m:

WechatJump/Debug.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@
99

1010
screenshot_backup_dir = 'screenshot_backups/'
1111

12+
1213
def make_debug_dir(screenshot_backup_dir):
1314
'''
1415
创建备份文件夹
1516
'''
1617
if not os.path.isdir(screenshot_backup_dir):
1718
os.mkdir(screenshot_backup_dir)
1819

20+
1921
def backup_screenshot(ts):
2022
'''
2123
为了方便失败的时候 debug
2224
'''
2325
make_debug_dir(screenshot_backup_dir)
2426
shutil.copy('autojump.png', '{}{}.png'.format(screenshot_backup_dir, ts))
2527

28+
2629
def save_debug_screenshot(ts, im, piece_x, piece_y, board_x, board_y):
2730
'''
28-
对debug图片加上详细的注释
31+
对 debug 图片加上详细的注释
2932
'''
3033
make_debug_dir(screenshot_backup_dir)
3134
draw = ImageDraw.Draw(im)
@@ -39,23 +42,25 @@ def save_debug_screenshot(ts, im, piece_x, piece_y, board_x, board_y):
3942
del draw
4043
im.save('{}{}_d.png'.format(screenshot_backup_dir, ts))
4144

45+
4246
def dump_device_info():
4347
'''
4448
显示设备信息
4549
'''
4650
size_str = os.popen('adb shell wm size').read()
4751
device_str = os.popen('adb shell getprop ro.product.model').read()
4852
density_str = os.popen('adb shell wm density').read()
49-
print("如果你的脚本无法工作,上报issue时请copy如下信息:\n**********\
50-
\nScreen: {size}\
51-
\nDensity: {dpi}\
52-
\nDeviceType: {type}\
53-
\nOS: {os}\
54-
\nPython: {python}\n**********".format(
55-
size=size_str.strip(),
56-
type=device_str.strip(),
57-
dpi=density_str.strip(),
58-
os=sys.platform,
59-
python=sys.version
60-
)
61-
)
53+
print("""如果你的脚本无法工作,上报 issue 时请 copy 如下信息:
54+
**********
55+
Screen: {size}
56+
Density: {dpi}
57+
DeviceType: {type}
58+
OS: {os}
59+
Python: {python}
60+
**********""".format(
61+
size=size_str.strip(),
62+
type=device_str.strip(),
63+
dpi=density_str.strip(),
64+
os=sys.platform,
65+
python=sys.version
66+
))

wechat_jump_auto.py

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
# 这时候得到了块中点的 X 轴坐标,这时候假设现在棋子在当前块的中心,
1212
# 根据一个通过截图获取的固定的角度来推出中点的 Y 坐标
1313
# 最后:根据两点的坐标算距离乘以系数来获取长按时间(似乎可以直接用 X 轴距离)
14-
15-
TODO:
16-
解决定位偏移的问题
17-
看看两个块中心到中轴距离是否相同,如果是的话靠这个来判断一下当前超前还是落后,便于矫正
18-
一些固定值根据截图的具体大小计算
19-
直接用 X 轴距离简化逻辑
2014
'''
2115
import os
2216
import sys
@@ -27,10 +21,10 @@
2721
import random
2822
from WechatJump import Debug, Config
2923

30-
debug_switch = False # debug开关,需要调试的时候请改为:True
24+
debug_switch = False # debug 开关,需要调试的时候请改为:True
3125
config = Config.open_accordant_config()
3226

33-
# Magic Number,不设置可能无法正常执行,请根据具体截图从上到下按需设置
27+
# Magic Number,不设置可能无法正常执行,请根据具体截图从上到下按需设置,设置保存在 config 文件夹中
3428
under_game_score_y = config['under_game_score_y']
3529
press_coefficient = config['press_coefficient'] # 长按的时间系数,请自己根据实际情况调节
3630
piece_base_height_1_2 = config['piece_base_height_1_2'] # 二分之一的棋子底座高度,可能要调节
@@ -41,8 +35,6 @@
4135
swipe = config['swipe']
4236
else:
4337
swipe = {}
44-
#设置模拟按压各项参数,经过多台手机测试,其中2160x1080建议调整参数为320,1210,720,910
45-
#使用vivox20,夏普全面屏和小米mix2测试过,均可达到2000+分数(记得在开发者设置打开usb安全验证)
4638
swipe['x1'], swipe['y1'], swipe['x2'], swipe['y2'] = 320, 410, 320, 410
4739

4840

@@ -71,7 +63,7 @@ def pull_screenshot():
7163

7264
def set_button_position(im):
7365
'''
74-
将swipe设置为 `再来一局` 按钮的位置
66+
将 swipe 设置为 `再来一局` 按钮的位置
7567
'''
7668
global swipe_x1, swipe_y1, swipe_x2, swipe_y2
7769
w, h = im.size
@@ -85,7 +77,7 @@ def jump(distance):
8577
跳跃一定的距离
8678
'''
8779
press_time = distance * press_coefficient
88-
press_time = max(press_time, 200) # 设置 200 ms 是最小的按压时间
80+
press_time = max(press_time, 200) # 设置 200ms 是最小的按压时间
8981
press_time = int(press_time)
9082
cmd = 'adb shell input swipe {x1} {y1} {x2} {y2} {duration}'.format(
9183
x1=swipe_x1,
@@ -98,6 +90,7 @@ def jump(distance):
9890
os.system(cmd)
9991
return press_time
10092

93+
10194
def find_piece_and_board(im):
10295
'''
10396
寻找关键坐标
@@ -110,22 +103,22 @@ def find_piece_and_board(im):
110103
board_x = 0
111104
board_y = 0
112105
scan_x_border = int(w / 8) # 扫描棋子时的左右边界
113-
scan_start_y = 0 # 扫描的起始y坐标
106+
scan_start_y = 0 # 扫描的起始 y 坐标
114107
im_pixel = im.load()
115-
# 以50px步长,尝试探测scan_start_y
108+
# 以 50px 步长,尝试探测 scan_start_y
116109
for i in range(int(h / 3), int(h*2 / 3), 50):
117110
last_pixel = im_pixel[0, i]
118111
for j in range(1, w):
119112
pixel = im_pixel[j, i]
120-
# 不是纯色的线,则记录scan_start_y的值,准备跳出循环
113+
# 不是纯色的线,则记录 scan_start_y 的值,准备跳出循环
121114
if pixel[0] != last_pixel[0] or pixel[1] != last_pixel[1] or pixel[2] != last_pixel[2]:
122115
scan_start_y = i - 50
123116
break
124117
if scan_start_y:
125118
break
126-
print('scan_start_y: ', scan_start_y)
119+
print('scan_start_y: {}'.format(scan_start_y))
127120

128-
# 从scan_start_y开始往下扫描,棋子应位于屏幕上半部分,这里暂定不超过2/3
121+
# 从 scan_start_y 开始往下扫描,棋子应位于屏幕上半部分,这里暂定不超过 2/3
129122
for i in range(scan_start_y, int(h * 2 / 3)):
130123
for j in range(scan_x_border, w - scan_x_border): # 横坐标方面也减少了一部分扫描开销
131124
pixel = im_pixel[j, i]
@@ -137,10 +130,10 @@ def find_piece_and_board(im):
137130

138131
if not all((piece_x_sum, piece_x_c)):
139132
return 0, 0, 0, 0
140-
piece_x = int(piece_x_sum / piece_x_c);
133+
piece_x = int(piece_x_sum / piece_x_c)
141134
piece_y = piece_y_max - piece_base_height_1_2 # 上移棋子底盘高度的一半
142135

143-
#限制棋盘扫描的横坐标,避免音符bug
136+
# 限制棋盘扫描的横坐标,避免音符 bug
144137
if piece_x < w/2:
145138
board_x_start = piece_x
146139
board_x_end = w
@@ -169,31 +162,28 @@ def find_piece_and_board(im):
169162
board_x = board_x_sum / board_x_c
170163
last_pixel = im_pixel[board_x, i]
171164

172-
#从上顶点往下+274的位置开始向上找颜色与上顶点一样的点,为下顶点
173-
#该方法对所有纯色平面和部分非纯色平面有效,对高尔夫草坪面、木纹桌面、药瓶和非菱形的碟机(好像是)会判断错误
174-
for k in range(i+274, i, -1): #274取开局时最大的方块的上下顶点距离
165+
# 从上顶点往下 +274 的位置开始向上找颜色与上顶点一样的点,为下顶点
166+
# 该方法对所有纯色平面和部分非纯色平面有效,对高尔夫草坪面、木纹桌面、药瓶和非菱形的碟机(好像是)会判断错误
167+
for k in range(i+274, i, -1): # 274 取开局时最大的方块的上下顶点距离
175168
pixel = im_pixel[board_x, k]
176169
if abs(pixel[0] - last_pixel[0]) + abs(pixel[1] - last_pixel[1]) + abs(pixel[2] - last_pixel[2]) < 10:
177170
break
178171
board_y = int((i+k) / 2)
179172

180-
#如果上一跳命中中间,则下个目标中心会出现r245 g245 b245的点,利用这个属性弥补上一段代码可能存在的判断错误
181-
#若上一跳由于某种原因没有跳到正中间,而下一跳恰好有无法正确识别花纹,则有可能游戏失败,由于花纹面积通常比较大,失败概率较低
173+
# 如果上一跳命中中间,则下个目标中心会出现 r245 g245 b245 的点,利用这个属性弥补上一段代码可能存在的判断错误
174+
# 若上一跳由于某种原因没有跳到正中间,而下一跳恰好有无法正确识别花纹,则有可能游戏失败,由于花纹面积通常比较大,失败概率较低
182175
for l in range(i, i+200):
183176
pixel = im_pixel[board_x, l]
184177
if abs(pixel[0] - 245) + abs(pixel[1] - 245) + abs(pixel[2] - 245) == 0:
185178
board_y = l+10
186179
break
187180

188-
189-
190181
if not all((board_x, board_y)):
191182
return 0, 0, 0, 0
192183

193184
return piece_x, piece_y, board_x, board_y
194185

195186

196-
197187
def check_screenshot():
198188
'''
199189
检查获取截图的方式
@@ -207,11 +197,12 @@ def check_screenshot():
207197
pull_screenshot()
208198
try:
209199
Image.open('./autojump.png').load()
210-
print('采用方式{}获取截图'.format(screenshot_way))
211-
except:
200+
print('采用方式 {} 获取截图'.format(screenshot_way))
201+
except Exception:
212202
screenshot_way -= 1
213203
check_screenshot()
214204

205+
215206
def main():
216207
'''
217208
主函数
@@ -231,7 +222,7 @@ def main():
231222
if debug_switch:
232223
Debug.save_debug_screenshot(ts, im, piece_x, piece_y, board_x, board_y)
233224
Debug.backup_screenshot(ts)
234-
time.sleep(1) # 为了保证截图的时候应落稳了,多延迟一会儿
225+
time.sleep(random.uniform(1, 1.2)) # 为了保证截图的时候应落稳了,多延迟一会儿,随机值防 ban
235226

236227

237228
if __name__ == '__main__':

0 commit comments

Comments
 (0)