c++连连看游戏_用Python玩连连看是什么效果?
1import PIL.ImageGrab2import pyautogui3import win32api4import win32gui5import win32con6import time7import 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 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 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 = True102 for index_x in range(min_x + 1, max_x):103 if matrix[index_y][index_x] != 0:104 flag = False105 break106 if flag:107 return True108109 max_x1 = x1110 while max_x1 + 1 and matrix[y1][max_x1 + 1] == 0:111 max_x1 += 1112 min_x1 = x1113 while min_x1 - 1 >= 0 and matrix[y1][min_x1 - 1] == 0:114 min_x1 -= 1115116 max_x2 = x2117 while max_x2 + 1 and matrix[y2][max_x2 + 1] == 0:118 max_x2 += 1119 min_x2 = x2120 while min_x2 - 1 >= 0 and matrix[y2][min_x2 - 1] == 0:121 min_x2 -= 1122123 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 = True130 for index_y in range(min_y + 1, max_y):131 if matrix[index_y][index_x] != 0:132 flag = False133 break134 if flag:135 return True136137 return False138139140def execute_one_step(one_step):141 from_row, from_col, to_row, to_col = one_step142143 from_x = game_area_left + (from_col + 0.5) * grid_width144 from_y = game_area_top + (from_row + 0.5) * grid_height145146 to_x = game_area_left + (to_col + 0.5) * grid_width147 to_y = game_area_top + (to_row + 0.5) * grid_height148149 pyautogui.moveTo(from_x, from_y)150 pyautogui.click()151152 pyautogui.moveTo(to_x, to_y)153 pyautogui.click()154155156if __name__ == '__main__':157158 COL_NUM = 19159 ROW_NUM = 11160161 screen_width = win32api.GetSystemMetrics(0)162 screen_height = win32api.GetSystemMetrics(1)163164 hwnd = win32gui.FindWindow(win32con.NULL, 'QQ游戏 - 连连看角色版')165 if hwnd == 0:166 exit(-1)167168 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_left174 window_height = window_bottom - window_top175176 game_area_left = window_left + 14.0 / 800.0 * window_width177 game_area_top = window_top + 181.0 / 600.0 * window_height178 game_area_right = window_left + 603 / 800.0 * window_width179 game_area_bottom = window_top + 566 / 600.0 * window_height180181 game_area_width = game_area_right - game_area_left182 game_area_height = game_area_bottom - game_area_top183 grid_width = game_area_width / COL_NUM184 grid_height = game_area_height / ROW_NUM185186 game_area_image = PIL.ImageGrab.grab((game_area_left, game_area_top, game_area_right, game_area_bottom))187188 matrix = game_area_image_to_matrix()189190 map = {}191192 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 continue197 map.setdefault(grid_id, [])198 arr = map[grid_id]199 arr.append([x, y])200201 pyautogui.PAUSE = 0202203 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获取连连看游戏句柄, 获取游戏界面的图片, 对方块进行切割, 对每个方块取几个点的颜色进行比对, 均相同则认为是同一个方块,然后模拟鼠标取消就行了, 代码的最后一行是每次点击的间隔。如果本文对你有帮助,"转发、点赞、三连"就是对作者最大的鼓励!
来源:https://urlify.cn/7vEjie
人生苦短,和我一起学Python!
c++连连看游戏_用Python玩连连看是什么效果?相关推荐
- 用Python玩连连看是什么效果?
1.前言 Python实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要拿去伤害玩家们... 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知 ...
- java设计连连看心得_基于Java的连连看游戏的设计与实现
ComputerKnowledgeand Technology 电脑知识与技术 软件设计开发本栏目责任编辑:谢媛媛 第7卷第35期 (2011年12月) 9 基于Java的连连看游戏的设计与实现 陈珊 ...
- 自动识图进行点击,用Python玩连连看是什么效果?
1.前言 Python实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要拿去伤害玩家们- 2.基本环境配置 版本:Python3.6 系统:Windows 3.相关模块: 私信小编00 ...
- python生成一笔画_用Python玩烧脑小游戏《一笔画完》,瞬间闯到100关
原标题:用Python玩烧脑小游戏<一笔画完>,瞬间闯到100关 " 昨天和朋友出去外面吃饭,吃完饭后朋友打开了一个小程序玩了起来...... 游戏长这样 大概玩法是:从地图中猫 ...
- 用Python 玩连连看 是什么效果?别霍霍别人了
作者:Laziji 源自:https://laboo.top/2018/11/07/lianliankan/ 1.前言 Python 实现的qq连连看辅助, 仅用于学习, 请在练习模式下使用, 请不要 ...
- 跳一跳python开挂_用Python玩跳一跳小游戏,我能开挂
原标题:用Python玩跳一跳小游戏,我能开挂 对很多人来说,可能是已经过时的游戏,对于Python刚入门来说,却是一个非常值得学习的项目. 我们收集了很多有关python入门的项目案例,包含了相应的 ...
- python pygame模块怎么写游戏_使用 Python 和 Pygame 模块构建一个游戏框架
这系列的第一篇通过创建一个简单的骰子游戏来探究 Python.现在是来从零制作你自己的游戏的时间. 在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏 ...
- 我的世界python写游戏_用python写游戏之 Give it up
<永不言弃 Give It Up>,这是一款极具虐心色彩的音乐题材闯关游戏. 这篇文章就来分析这款游戏原理,并用python写出来一个简易版.废话不多说,直接开始分析. 游戏元素,暂且把主 ...
- 用python玩转数据第一周答案_用Python玩转数据_答案
用Python玩转数据_答案 答案: 更多相关问题 求由参数方程所确定的函数y=y(x)的二阶导数 已知数列的通项公式,则取最小值时=,此时=. (本小题满分10分)已知是等差数列,其中](1)求的通 ...
最新文章
- 电脑怎样限制装软件 怎么限制软件运行
- android gridview item 点击,Android-取消GridView/ListView item被点击时的效果
- 网站建设都需要哪些步骤?
- 点击页面空白处就关闭某个层是怎么做到的
- 部署负载均衡 Keepalived DR群集优化版(减少部署完成后的BUG)
- java主类型_Java主类结构:基本数据类型
- 数据结构与算法--6.二分查找
- StringUtils工具类说明
- 【线程】——volatile关键字
- 多数据点拟合曲线,最小二乘法,矩阵
- accumulate
- 说三件可能你不知道的小事
- 为ui中的面板添加枚举类型脚本与json来保存信息
- 已知高维高斯联合概率分布求边缘概率分布以及条件概率分布
- java+nanomsg(jnanomsg)
- Volatile关键字~转载自博客园的“海子”
- OSChina 周一乱弹 —— 今天下班带你去放松咧
- 人这一辈子,都在为选择买单
- 《孩子,你慢慢来》的读书笔记与读后感2600字
- 最新 955 不加班的公司名单
热门文章
- 从业务到平台的思维转变
- Spring Boot中使用LDAP来统一管理用户信息
- MVC-Razor(3)
- mysql数据库性别男用1存储那性别女用什么呢?
- No module named ‘prompt_toolkit.enums‘
- pytorch 计算相似度,相关系数
- qthread destroyed while thread is still running
- pytorch attention 注意力
- opencv中traincascade训练分类器
- python 简单图像处理(13) 二值图腐蚀和膨胀,开运算、闭运算