前言
Python 實現的qq連連看輔助, 僅用于學習, 請在練習模式下使用, 請不要拿去傷害玩家們...
作者:Laziji
源自:
https://laboo.top/2018/11/07/lianliankan/
基本環境配置
版本:Python3.6
系統:Windows
相關模塊:
importPIL.ImageGrab
importpyautogui
importwin32api
importwin32gui
importwin32con
importtime
importrandom
使用方法
開始遊戲後運行就行了, 再次提示, 請在練習模式中使用, 否則可能會被其他玩家舉報
效果圖
代碼實現
importPIL.ImageGrab
importpyautogui
importwin32api
importwin32gui
importwin32con
importtime
importrandom
'''
想要學習Python?Python學習交流群:452739833滿足你的需求,資料都已經上傳群文件流,可以自行下載!
'''
defcolor_hash(color):
value=""
foriinrange(5):
value ="%d,%d,%d,"%(color[0],color[1],color[2])
returnhash(value)
defimage_hash(img):
value=""
foriinrange(5):
c=img.getpixel((i*3,i*3))
value ="%d,%d,%d,"%(c[0],c[1],c[2])
returnhash(value)
defgame_area_image_to_matrix():
pos_to_image={}
forrowinrange(ROW_NUM):
pos_to_image[row]={}
forcolinrange(COL_NUM):
grid_left=col*grid_width
grid_top=row*grid_height
grid_right=grid_left grid_width
grid_bottom=grid_top grid_height
grid_image=game_area_image.crop((grid_left,grid_top,grid_right,grid_bottom))
pos_to_image[row][col]=grid_image
pos_to_type_id={}
image_map={}
empty_hash=color_hash((48,76,112))
forrowinrange(ROW_NUM):
pos_to_type_id[row]={}
forcolinrange(COL_NUM):
this_image=pos_to_image[row][col]
this_image_hash=image_hash(this_image)
ifthis_image_hash==empty_hash:
pos_to_type_id[row][col]=0
continue
image_map.setdefault(this_image_hash,len(image_map) 1)
pos_to_type_id[row][col]=image_map.get(this_image_hash)
returnpos_to_type_id
defsolve_matrix_one_step():
forkeyinmap:
arr=map[key]
arr_len=len(arr)
forindex1inrange(arr_len-1):
point1=arr[index1]
x1=point1[0]
y1=point1[1]
forindex2inrange(index1 1,arr_len):
point2=arr[index2]
x2=point2[0]
y2=point2[1]
ifverifying_connectivity(x1,y1,x2,y2):
arr.remove(point1)
arr.remove(point2)
matrix[y1][x1]=0
matrix[y2][x2]=0
ifarr_len==2:
map.pop(key)
returny1,x1,y2,x2
defverifying_connectivity(x1,y1,x2,y2):
max_y1=y1
whilemax_y1 1<ROW_NUMandmatrix[max_y1 1][x1]==0:
max_y1 =1
min_y1=y1
whilemin_y1-1>=0andmatrix[min_y1-1][x1]==0:
min_y1-=1
max_y2=y2
whilemax_y2 1<ROW_NUMandmatrix[max_y2 1][x2]==0:
max_y2 =1
min_y2=y2
whilemin_y2-1>=0andmatrix[min_y2-1][x2]==0:
min_y2-=1
rg_min_y=max(min_y1,min_y2)
rg_max_y=min(max_y1,max_y2)
ifrg_max_y>=rg_min_y:
forindex_yinrange(rg_min_y,rg_max_y 1):
min_x=min(x1,x2)
max_x=max(x1,x2)
flag=True
forindex_xinrange(min_x 1,max_x):
ifmatrix[index_y][index_x]!=0:
flag=False
break
ifflag:
returnTrue
max_x1=x1
whilemax_x1 1<COL_NUMandmatrix[y1][max_x1 1]==0:
max_x1 =1
min_x1=x1
whilemin_x1-1>=0andmatrix[y1][min_x1-1]==0:
min_x1-=1
max_x2=x2
whilemax_x2 1<COL_NUMandmatrix[y2][max_x2 1]==0:
max_x2 =1
min_x2=x2
whilemin_x2-1>=0andmatrix[y2][min_x2-1]==0:
min_x2-=1
rg_min_x=max(min_x1,min_x2)
rg_max_x=min(max_x1,max_x2)
ifrg_max_x>=rg_min_x:
forindex_xinrange(rg_min_x,rg_max_x 1):
min_y=min(y1,y2)
max_y=max(y1,y2)
flag=True
forindex_yinrange(min_y 1,max_y):
ifmatrix[index_y][index_x]!=0:
flag=False
break
ifflag:
returnTrue
returnFalse
defexecute_one_step(one_step):
from_row,from_col,to_row,to_col=one_step
from_x=game_area_left (from_col 0.5)*grid_width
from_y=game_area_top (from_row 0.5)*grid_height
to_x=game_area_left (to_col 0.5)*grid_width
to_y=game_area_top (to_row 0.5)*grid_height
pyautogui.moveTo(from_x,from_y)
pyautogui.click()
pyautogui.moveTo(to_x,to_y)
pyautogui.click()
if__name__=='__main__':
COL_NUM=19
ROW_NUM=11
screen_width=win32api.GetSystemMetrics(0)
screen_height=win32api.GetSystemMetrics(1)
hwnd=win32gui.FindWindow(win32con.NULL,'QQ遊戲-連連看角色版')
ifhwnd==0:
exit(-1)
win32gui.ShowWindow(hwnd,win32con.SW_RESTORE)
win32gui.SetForegroundWindow(hwnd)
window_left,window_top,window_right,window_bottom=win32gui.GetWindowRect(hwnd)
ifmin(window_left,window_top)<0orwindow_right>screen_widthorwindow_bottom>screen_height:
exit(-1)
window_width=window_right-window_left
window_height=window_bottom-window_top
game_area_left=window_left 14.0/800.0*window_width
game_area_top=window_top 181.0/600.0*window_height
game_area_right=window_left 603/800.0*window_width
game_area_bottom=window_top 566/600.0*window_height
game_area_width=game_area_right-game_area_left
game_area_height=game_area_bottom-game_area_top
grid_width=game_area_width/COL_NUM
grid_height=game_area_height/ROW_NUM
game_area_image=PIL.ImageGrab.grab((game_area_left,game_area_top,game_area_right,game_area_bottom))
matrix=game_area_image_to_matrix()
map={}
foryinrange(ROW_NUM):
forxinrange(COL_NUM):
grid_id=matrix[y][x]
ifgrid_id==0:
continue
map.setdefault(grid_id,[])
arr=map[grid_id]
arr.append([x,y])
pyautogui.PAUSE=0
whileTrue:
one_step=solve_matrix_one_step()
ifnotone_step:
exit(0)
execute_one_step(one_step)
time.sleep(random.randint(0,0)/1000)
(左右滑動可查看完整代碼)
主要思路就是利用pywin32獲取連連看遊戲句柄, 獲取遊戲界面的圖片, 對方塊進行切割, 對每個方塊取幾個點的顔色進行比對, 均相同則認為是同一個方塊,
然後模拟鼠标去消就行了, 代碼的最後一行是每次點擊的間隔
這裡小編是一個有着5年工作經驗的Python工程師,關于Python有一個完整學習Python的路線,學習材料和工具。需要的夥伴可以私信我,發送“Python”就可以獲取領取地址,免費送給大家。對于學習Python有任何問題(學習方法,學習效率,如何就業)都可以問我。希望你也能憑自己的努力,成為下一個優秀的程序員!
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!