-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
91 lines (79 loc) · 2.53 KB
/
main.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
import img_tool
import pickle
import adb_tool
import time
import util
from config import config
import os
import cv2
import shutil
'''创建及清空必要的文件夹'''
try:
shutil.rmtree('ScreenShot')
except Exception:
pass
os.mkdir('ScreenShot')
if not os.path.exists('SingleChar'):
os.mkdir('SingleChar')
if config['debug']:
print("开启了调试模式,请手动答题")
if not os.path.exists('ScreenShotForTrain'):
os.mkdir('ScreenShotForTrain')
'''
使用的截屏方法,0代表直接使用adb截屏,1代表使用跨平台的方法从PC截屏,2代表使用windows原生API进行截屏
0方法很慢,最多只能闯到第35关
1方法比较快,有一定几率通关
2方法很快,通关几率更大
'''
shot_type = config['type']
#加载逻辑回归模型
with open('lr.pickle', 'rb') as fr:
lr = pickle.load(fr)
preRes = '' #保存上一步的表达式,防止因截图过快导致的本次点击了上一张图的答案
'''
一次屏幕点击
'''
def one_tap(res):
print(eval(res))
if eval(res):
if shot_type == 0:
adb_tool.tapScreen(config['adb_tap_true_x'], config['adb_tap_y'])
else:
util.tapScreenFromPC(config['pc_tap_true_x'], config['pc_tap_y'])
else:
if shot_type == 0:
adb_tool.tapScreen(config['adb_tap_false_x'], config['adb_tap_y'])
else:
util.tapScreenFromPC(config['pc_tap_false_x'], config['pc_tap_y'])
count = 1 #迭代轮数
while True:
if shot_type == 0:
img = adb_tool.getScreenshot()
elif shot_type == 1:
img = util.shotFromComputer()
else:
img = util.shotByWinAPI('ScreenShot/%d.png' %count)
if config['debug']:
cv2.imwrite('ScreenShotForTrain/%d.png' %int(time.time()), img)
print("截图成功")
time.sleep(0.3)
continue
#t2= time.time()
#print('截图耗时%f' %(t2 - t1))
res = img_tool.get_result(lr, img, '%d.png' % count)
#t3 = time.time()
#print('获取结果耗时%f' % (t3 - t2))
if res == preRes or res == '':
'''如果表达式和之前的表达式相同,则代表截图重复,可能此时手机已经跳到了下一题,因此不进行点击'''
#print('截图重复')
#time.sleep(config['sleep_when_repeat'] / (count // 10 + 1))
continue
else:
print('第%d题: %s'%(count,res), end=' ')
preRes = res
try:
one_tap(res)
except SyntaxError:
print("游戏结束!")
exit(0)
count += 1