1、前言

Python实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要拿去伤害玩家们…

2、基本环境配置

版本:Python3.6

系统:Windows

3、相关模块:

私信小编001即可获取大量Python编程学习资料

1import PIL.ImageGrabimport pyautoguiimport win32apiimport win32guiimport win32conimport timeimport random
1import PIL.ImageGrabimport pyautoguiimport win32apiimport win32guiimport win32conimport timeimport random

4、使用方法

开始游戏后运行就行了, 再次提示, 请在练习模式中使用, 否则可能会被其他玩家举报。

效果图

5、代码实现

1import PIL.ImageGrab
2import pyautogui
3import win32api
4import win32gui
5import win32con
6import time
7import random
8  9def color_hash(color):
10    value = ""
11    for i in range(5):
12        value += "%d,%d,%d," % (color[0], color[1], color[2])
13    return hash(value)
14
15 16def image_hash(img):
17    value = ""
18    for i in range(5):
19        c = img.getpixel((i * 3, i * 3))
20        value += "%d,%d,%d," % (c[0], c[1], c[2])
21    return hash(value)
22
23
24def game_area_image_to_matrix():
25    pos_to_image = {}
26
27    for row in range(ROW_NUM):
28        pos_to_image[row] = {}
29        for col in range(COL_NUM):
30            grid_left = col * grid_width
31            grid_top = row * grid_height
32            grid_right = grid_left + grid_width
33            grid_bottom = grid_top + grid_height
34
35            grid_image = game_area_image.crop((grid_left, grid_top, grid_right, grid_bottom))
36
37            pos_to_image[row][col] = grid_image
38
39    pos_to_type_id = {}
40    image_map = {}
41
42    empty_hash = color_hash((48, 76, 112))
43
44    for row in range(ROW_NUM):
45        pos_to_type_id[row] = {}
46        for col in range(COL_NUM):
47            this_image = pos_to_image[row][col]
48            this_image_hash = image_hash(this_image)
49            if this_image_hash == empty_hash:
50                pos_to_type_id[row][col] = 0
51                continue
52            image_map.setdefault(this_image_hash, len(image_map) + 1)
53            pos_to_type_id[row][col] = image_map.get(this_image_hash)
54
55    return pos_to_type_id
56
57 58def solve_matrix_one_step():
59    for key in map:
60        arr = map[key]
61        arr_len = len(arr)
62        for index1 in range(arr_len - 1):
63            point1 = arr[index1]
64            x1 = point1[0]
65            y1 = point1[1]
66            for index2 in range(index1 + 1, arr_len):
67                point2 = arr[index2]
68                x2 = point2[0]
69                y2 = point2[1]
70                if verifying_connectivity(x1, y1, x2, y2):
71                    arr.remove(point1)
72                    arr.remove(point2)
73                    matrix[y1][x1] = 0
74                    matrix[y2][x2] = 0
75                    if arr_len == 2:
76                        map.pop(key)
77                    return y1, x1, y2, x2
78
79
80def verifying_connectivity(x1, y1, x2, y2):
81    max_y1 = y1
82    while max_y1 + 1 < ROW_NUM and matrix[max_y1 + 1][x1] == 0:
83        max_y1 += 1
84    min_y1 = y1
85    while min_y1 - 1 >= 0 and matrix[min_y1 - 1][x1] == 0:
86        min_y1 -= 1
87
88    max_y2 = y2
89    while max_y2 + 1 < ROW_NUM and matrix[max_y2 + 1][x2] == 0:
90        max_y2 += 1
91    min_y2 = y2
92    while min_y2 - 1 >= 0 and matrix[min_y2 - 1][x2] == 0:
93        min_y2 -= 1
94
95    rg_min_y = max(min_y1, min_y2)
96    rg_max_y = min(max_y1, max_y2)
97    if rg_max_y >= rg_min_y:
98        for index_y in range(rg_min_y, rg_max_y + 1):
99            min_x = min(x1, x2)
100            max_x = max(x1, x2)
101            flag = True
102            for index_x in range(min_x + 1, max_x):
103                if matrix[index_y][index_x] != 0:
104                    flag = False
105                    break
106            if flag:
107                return True
108
109    max_x1 = x1110    while max_x1 + 1 < COL_NUM and matrix[y1][max_x1 + 1] == 0:
111        max_x1 += 1
112    min_x1 = x1
113    while min_x1 - 1 >= 0 and matrix[y1][min_x1 - 1] == 0:
114        min_x1 -= 1
115
116    max_x2 = x2117    while max_x2 + 1 < COL_NUM and matrix[y2][max_x2 + 1] == 0:
118        max_x2 += 1
119    min_x2 = x2
120    while min_x2 - 1 >= 0 and matrix[y2][min_x2 - 1] == 0:
121        min_x2 -= 1
122
123    rg_min_x = max(min_x1, min_x2)
124    rg_max_x = min(max_x1, max_x2)
125    if rg_max_x >= rg_min_x:
126        for index_x in range(rg_min_x, rg_max_x + 1):
127            min_y = min(y1, y2)
128            max_y = max(y1, y2)
129            flag = True
130            for index_y in range(min_y + 1, max_y):
131                if matrix[index_y][index_x] != 0:
132                    flag = False133                    break
134            if flag:
135                return True
136
137    return False
138
139
140def execute_one_step(one_step):
141    from_row, from_col, to_row, to_col = one_step
142
143    from_x = game_area_left + (from_col + 0.5) * grid_width
144    from_y = game_area_top + (from_row + 0.5) * grid_height
145
146    to_x = game_area_left + (to_col + 0.5) * grid_width
147    to_y = game_area_top + (to_row + 0.5) * grid_height
148
149    pyautogui.moveTo(from_x, from_y)
150    pyautogui.click()
151
152    pyautogui.moveTo(to_x, to_y)
153    pyautogui.click()
154
155
156if __name__ == '__main__':
157
158    COL_NUM = 19
159    ROW_NUM = 11
160
161    screen_width = win32api.GetSystemMetrics(0)
162    screen_height = win32api.GetSystemMetrics(1)
163
164    hwnd = win32gui.FindWindow(win32con.NULL, 'QQ游戏 - 连连看角色版')
165    if hwnd == 0:
166        exit(-1)
167
168    win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
169    win32gui.SetForegroundWindow(hwnd)
170    window_left, window_top, window_right, window_bottom = win32gui.GetWindowRect(hwnd)
171    if min(window_left, window_top) < 0 or window_right > screen_width or window_bottom > screen_height:
172        exit(-1)
173    window_width = window_right - window_left
174    window_height = window_bottom - window_top
175
176    game_area_left = window_left + 14.0 / 800.0 * window_width
177    game_area_top = window_top + 181.0 / 600.0 * window_height
178    game_area_right = window_left + 603 / 800.0 * window_width
179    game_area_bottom = window_top + 566 / 600.0 * window_height
180
181    game_area_width = game_area_right - game_area_left
182    game_area_height = game_area_bottom - game_area_top
183    grid_width = game_area_width / COL_NUM
184    grid_height = game_area_height / ROW_NUM
185
186    game_area_image = PIL.ImageGrab.grab((game_area_left, game_area_top, game_area_right, game_area_bottom))
187
188    matrix = game_area_image_to_matrix()189
190    map = {}
191
192    for y in range(ROW_NUM):
193        for x in range(COL_NUM):
194            grid_id = matrix[y][x]
195            if grid_id == 0:
196                continue
197            map.setdefault(grid_id, [])
198            arr = map[grid_id]
199            arr.append([x, y])
200
201    pyautogui.PAUSE = 0
202
203    while True:
204        one_step = solve_matrix_one_step()
205        if not one_step:
206            exit(0)
207        execute_one_step(one_step)
208        time.sleep(random.randint(0,0)/1000)

主要思路就是利用pywin32获取连连看游戏句柄, 获取游戏界面的图片, 对方块进行切割, 对每个方块取几个点的颜色进行比对, 均相同则认为是同一个方块,

然后模拟鼠标取消就行了, 代码的最后一行是每次点击的间隔。

自动识图进行点击,用Python玩连连看是什么效果?相关推荐

  1. c++连连看游戏_用Python玩连连看是什么效果?

    1.前言Python实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要拿去伤害玩家们...2.基本环境配置版本:Python3.6系统:Windows3.相关模块: 1import P ...

  2. 用Python玩连连看是什么效果?

    1.前言 Python实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要拿去伤害玩家们... 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知 ...

  3. 用Python 玩连连看 是什么效果?别霍霍别人了

    作者:Laziji 源自:https://laboo.top/2018/11/07/lianliankan/ 1.前言 Python 实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要 ...

  4. python Opencv和pyautogui实现自动识图点击

    python Opencv和pyautogui实现自动识图点击 1.导入python及其他模块 匹配类是上一章博客内容,pyautogui自带的图片匹配效果不是很理想.就使用Opencv的图片匹配来实 ...

  5. 易景空间地图建筑CAD自动识图转换为室内三维地图技术2021年突破

    目前市场上仅有的几家三维地图编辑器都是通过上传建筑CAD截图图片,然后上传到地图平台,在图片上进行描绘,这个过程时间花费如果房间量少的时候还能忍受,但是在针对复杂.房间比较多的地图场景时,需要重复的绘 ...

  6. python黑科技自动p图_不用ps让Python教你P图

    程序猿的哭泣 最近项目忙的有点疯狂到996ICU的节奏,差一点就一周无休了-还好周六晚上总算是硬气的决定休一天,不然真的如下图般:来自30岁程序猿的哭泣了. 今天西安的天气很不错,早起带着俩小兔崽子出 ...

  7. python黑科技自动p图_大神级Python工程师是怎么P图的,带你用Python玩转P图

    群内不定时分享干货,包括最新的python企业案例学习资料和零基础入门教程,欢迎初学和进阶中的小伙伴入群学习交流 1.PIL:Python影像库 PIL或者Python Imaging Library ...

  8. html在背景上自动打字,Js制作点击输入框时默认文字消失的效果

    为了提高用户体验和易用度,一些设计师会对网页中用户经常用的东西进行优化,比如输入框.一般的输入框是怎样优化的呢?从用户体验的角度出发,简化用户使用步骤,让用户用得更方便就是提高了易用性,例如当鼠标悬浮 ...

  9. autojs非按键精灵怎么做游戏脚本,来,给你游戏识图点击实例

    用autojs做游戏而非按键精灵做怎么做,来,给你游戏识图点击实例 代码如下,脚本采取的是农药的爬塔脚本示范 **@更多基础加autojs交流群698307198喽; 一键加群:点击加群 和更多作者同 ...

最新文章

  1. python 提取字符串中的中文字符
  2. CV之Hog+HamMingDistance:基于Hog提取和汉明距离对比的应用—图像相似度对比之for循环将多个成对图片依次对比并输出相似度
  3. Centos为什么比不过Ubuntu和Debian?
  4. 图文详解 Windows 2003服务器集群安装(1)
  5. JUnit5 @Tag注解示例
  6. asp 文本转时间_ASP.NET Core界面开发,DevExpress v19.2增强富文本编辑器功能
  7. Matlab快速傅里叶变换
  8. 2021年与 Linux 有关的几件大事
  9. CC(标准)版D碟收藏指南(三)
  10. centos7 安装最新rabbitmq,并设置开机自启
  11. 清华大学--代理服务器
  12. 多目标进化优化-SPEA/R
  13. Phaser 3入门
  14. JetBrains 学生认证教程(Pycharm,IDEA… 等学生认证教程)
  15. 「收藏级干货」蜡烛图交易形态全解
  16. 6阶群的非平凡子群_当|G|=8时,群lt;G,*gt;只能有?阶非平凡子群,不能有?阶子群,平凡子群为?...
  17. 泰拉瑞亚灾厄模组鸿蒙方舟,泰拉瑞亚灾厄Mod介绍大全 灾厄Mod物品装备BOSS图文介绍 BOSS:亵渎之神-游侠网...
  18. 如何写出高性能代码(四)优化数据访问
  19. 全球最神秘的高频交易巨头
  20. 南大计算机考研生源,高校解析:南京大学2020年推免数据分析

热门文章

  1. 2.4gwifi最高下载速度_宽带200m,光猫和无线一体的猫,5g和2.4g无线下速度多少是正常?...
  2. 论文中的参考文献序号自动链接到对应的参考文献
  3. 联想t450进入bios设置按哪个键_thinkpad笔记本怎么进bios设置|联想thinkpad开机进bios按哪个键-系统城...
  4. 使用 Entity Framework Power Tool 报错 0×80070057 解决方法
  5. javaweb实现支付宝扫码支付完整流程
  6. PRINCE2主题:商业论证,收益导向的管理艺术
  7. 国产网页在线编辑器kindeditor的使用
  8. 解决WPS高分辨率下因字体缩放导致字体发虚的问题
  9. 第四题:输入某年某月某日,判断这一天是这一年的第几天?
  10. Java 汉字转拼音(倒叙显示,过滤字符,字母,有数字添加到末尾)