-
Notifications
You must be signed in to change notification settings - Fork 0
/
4399.py
287 lines (218 loc) · 8.48 KB
/
4399.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
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
# -*- coding: utf-8 -*-
'''
@Time : 2021/1/7 21:58
@Author : Wanhao Zhang
@Contact : [email protected]
@FileName: 4399.py
@Software: PyCharm
'''
import operator
import time
import win32gui
from PIL import ImageGrab
import numpy as np
from pymouse import PyMouse
class GameAssist:
def __init__(self, wdname):
self.num_matrix = [] # 数字矩阵,每种数字代表一种动物,1-12
self.map_matrix = [] # 在self.num_matrix 周围加了一圈0
self.width = 71 # 每个图标的宽度,71像素
self.base_x = 399 # (self.base_x, slef.base_y) 整个连连看可点击位置的左上角
self.base_y = 305 # 可以截图后通过windows自带的画图工具找到某点的坐标
self.click_time = 0.2 # 模拟鼠标每次点击的间隔
self.mouse = PyMouse()
# 获取窗口的句柄
self.hwnd = win32gui.FindWindow(0, wdname)
if self.hwnd == 0:
print('no such hwnd')
exit(1)
# 将该窗口显示在最前面
win32gui.SetForegroundWindow(self.hwnd)
'''
获取屏幕截图,并将图标分割
'''
def screen_grab(self):
# 获取整个屏幕截图
image_grab = ImageGrab.grab()
# 获取游戏中间动物图标截图
box = (399, 305, 1247, 873)
animals_iamge = image_grab.crop(box)
# 将每个动物图像分割,得到图像矩阵
images_list = []
offset = 71
x0 = 0
y0 = 0
for i in range(8):
images_row = []
for j in range(12):
# 小图标左上角的坐标
x1 = x0 + j*offset
y1 = y0 + i*offset
# 小图标右下角的坐标
x2 = x1 + offset
y2 = y1 + offset
# 5px的偏移是为了去掉小图标周围,只保留中间,这样区分不同的图片更容易
images_row.append(animals_iamge.crop((x1+5, y1+5, x2-5, y2-5)))
images_list.append(images_row)
return images_list
def get_index(self, str01, threshold, str01_list):
for i in range(len(str01_list)):
diff = sum(map(operator.ne, str01, str01_list[i]))
if diff < threshold:
return i
return -1
'''
将每个图片转换成一个数字,相同的图标数字相同
'''
def image2num(self, animal_images):
num_str01_matrix = []
for i in range(8):
num_row = []
for j in range(12):
im = animal_images[i][j]
im_L = im.convert("L")
# im_L.show()
pixels = list(im_L.getdata()) # 每个点的像素值
avg_pixel = sum(pixels) / len(pixels)
str01 = "".join(map(lambda x: "1" if x > avg_pixel else "0", pixels))
num_row.append(str01)
num_str01_matrix.append(num_row)
for i in range(12):
print("(0,"+ str(i) +")", end='')
print(sum(map(operator.ne, num_str01_matrix[0][0], num_str01_matrix[0][i])))
threshold = 800 # 低于这个阈值认为是同一种图片
image_type_list = [] # 所有图标的类型
num_matrix = np.zeros((8,12), dtype=np.uint32) #创建一个全0矩阵
# 将每个点的数字串转换成一个特定数字
for i in range(8):
for j in range(12):
index = self.get_index(num_str01_matrix[i][j], threshold, image_type_list)
if index < 0:
image_type_list.append(num_str01_matrix[i][j])
num_matrix[i][j] = len(image_type_list)
else:
num_matrix[i][j] = index+1
return num_matrix
def is_row_connected(self, x, y1, y2):
if y1 > y2:
tmp = y1
y1 = y2
y2 = tmp
if y2 - y1 == 1:
return True
for i in range(y1+1, y2):
if self.map_matrix[x][i] != 0:
return False
return True
def is_col_connected(self, x1, x2, y):
if x1 > x2:
tmp = x1
x1 = x2
x2 = tmp
if x2 - x1 == 1:
return True
for i in range(x1+1, x2):
if self.map_matrix[i][y] != 0:
return False
return True
def get_direct_connected(self, x, y):
# 同一行直接相连的点
ans_list = []
row = x - 1
while row >= 0:
if self.map_matrix[row][y] == 0:
ans_list.append([row, y])
else:
break
row = row - 1
row = x + 1
while row < self.map_matrix.shape[0]:
if self.map_matrix[row][y] == 0:
ans_list.append([row, y])
else:
break
row = row + 1
col = y - 1
while col >= 0:
if self.map_matrix[x][col] == 0:
ans_list.append([x, col])
else:
break
col = col - 1
col = y + 1
while col < self.map_matrix.shape[1]:
if self.map_matrix[x][col] == 0:
ans_list.append([x, col])
else:
break
col = col + 1
return ans_list
def is_reachable(self, x1, y1, x2, y2):
# 如果数字不相同,直接返回不可到达
if self.map_matrix[x1][y1] != self.map_matrix[x2][y2]:
return False
list1 = self.get_direct_connected(x1, y1)
list2 = self.get_direct_connected(x2, y2)
for x1,y1 in list1:
for x2,y2 in list2:
if x1 == x2:
if self.is_row_connected(x1, y1, y2):
return True
elif y1 == y2:
if self.is_col_connected(x1, x2, y1):
return True
return False
'''
依次点击(x1, y1) (x2, y2), 并且将这两个位置的数字变成0
'''
def click_and_set0(self, x1, y1, x2, y2):
# 确定需要点击的坐标的中心位置
c_x1 = int(self.base_x + (y1 - 1)*self.width + self.width/2)
c_y1 = int(self.base_y + (x1 - 1)*self.width + self.width/2)
c_x2 = int(self.base_x + (y2 - 1)*self.width + self.width/2)
c_y2 = int(self.base_y + (x2 - 1)*self.width + self.width/2)
time.sleep(self.click_time)
self.mouse.click(c_x1, c_y1)
time.sleep(self.click_time)
self.mouse.click(c_x2, c_y2)
# 矩阵中设为0
self.map_matrix[x1][y1] = 0
self.map_matrix[x2][y2] = 0
'''
扫描整个矩阵,并点击相消
'''
def scan_game(self):
row_num = self.map_matrix.shape[0]
col_num = self.map_matrix.shape[1]
# self.click_and_set0(1,5,1,7)
print(row_num)
print(col_num)
for i in range(1, row_num):
for j in range(1, col_num):
if self.map_matrix[i][j] == 0:
continue
for l in range(1, row_num):
for k in range(1, col_num):
if i == l and j == k:
continue
if self.map_matrix[l][k] == 0:
continue
if self.is_reachable(i, j, l, k):
self.click_and_set0(i, j, l, k)
def start(self):
# 获取图像矩阵
animal_images = self.screen_grab()
# 获取图标的数字矩阵
self.num_matrix = self.image2num(animal_images)
# 四周添加上0,做成地图矩阵
self.map_matrix = np.zeros((self.num_matrix.shape[0] + 2, self.num_matrix.shape[1] + 2), dtype=np.uint32)
# print(map_matrix.shape)
self.map_matrix[1:9, 1:13] = self.num_matrix
print(self.map_matrix)
self.scan_game()
self.scan_game() # 很不优雅地扫描两遍,不过数据量小,没有关系
if __name__ == "__main__":
# 窗口句柄,使用windspy获取
wdname = "宠物连连看经典版2小游戏,在线玩,4399小游戏 - 360安全浏览器 10.0"
game_assist = GameAssist(wdname)
game_assist.start()