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

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

首页休闲益智连连看宝更新时间:2024-04-16

1、前言

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

2、基本环境配置

版本:Python3.6

系统:Windows

3、相关模块:

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

1importPIL.ImageGrabimportpyautoguiimportwin32apiimportwin32guiimportwin32conimporttimeimportrandom

1importPIL.ImageGrabimportpyautoguiimportwin32apiimportwin32guiimportwin32conimporttimeimportrandom

4、使用方法

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

效果图

5、代码实现

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

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

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

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved