(如想轉載,請聯系博主或貼上本博地址)
僅供學習python之用,勿用做商業用途。運行環境為1920*1080屏幕,python3.7,win7,谷歌瀏覽器版本 75.0.3770.100。
參考https://www.cnblogs.com/reader/p/10111777.html及https://baijiahao.baidu.com/s?id=1618385402903335091&wfr=spider&for=pc。
感謝先輩提供的優秀思路。
連連看游戲鏈接:http://www.4399.com/flash/80972.htm#search3
下面直接貼上代碼。
# -*- coding:utf-8 -*- from win32 import win32gui import time from PIL import ImageGrab, Image import numpy as np import operator from pymouse import PyMouse import win32gui class GameAssist: def __init__(self, wdname): """初始化""" # 取得窗口句柄 self.hwnd = win32gui.FindWindow(0, wdname) if not self.hwnd: print("窗口找不到,請確認窗口句柄名稱:【%s】" % wdname ) exit() # 窗口顯示最前面 win32gui.SetForegroundWindow(self.hwnd) # 小圖標編號矩陣 self.im2num_arr = [] # 主截圖的左上角坐標和右下角坐標 self.scree_left_and_right_point = (577, 289, 1043, 599) # 小圖標寬高 self.im_width = 39 # PyMouse對象,鼠標點擊 self.mouse = PyMouse() def screenshot(self): """屏幕截圖""" # 1、用grab函數截圖,參數為左上角和右下角左標 # image = ImageGrab.grab((417, 257, 885, 569)) image = ImageGrab.grab(self.scree_left_and_right_point) # 2、分切小圖 # exit() image_list = {} offset = self.im_width # 39 # 8行12列 for x in range(8): image_list[x] = {} for y in range(12): # print("show",x, y) # exit() top = x * offset left = y * offset right = (y + 1) * offset bottom = (x + 1) * offset # 用crop函數切割成小圖標,參數為圖標的左上角和右下角左邊 im = image.crop((left, top, right, bottom)) # 將切割好的圖標存入對應的位置 image_list[x][y] = im return image_list def image2num(self, image_list): """將圖標矩陣轉換成數字矩陣""" # 1、創建全零矩陣和空的一維數組 arr = np.zeros((10, 14), dtype=np.int32) # 以數字代替圖片 image_type_list = [] # 2、識別出不同的圖片,將圖片矩陣轉換成數字矩陣 for i in range(len(image_list)): for j in range(len(image_list[0])): im = image_list[i][j] # 驗證當前圖標是否已存入 index = self.getIndex(im, image_type_list) # 不存在image_type_list if index < 0: image_type_list.append(im) arr[i + 1][j + 1] = len(image_type_list) else: arr[i + 1][j + 1] = index + 1 print("圖標數:", len(image_type_list)) self.im2num_arr = arr return arr # 檢查數組中是否有圖標,如果有則返回索引下表 def getIndex(self,im, im_list): for i in range(len(im_list)): if self.isMatch(im, im_list[i]): return i return -1 # 漢明距離判斷兩個圖標是否一樣 def isMatch(self, im1, im2): # 縮小圖標,轉成灰度 image1 = im1.resize((20, 20), Image.ANTIALIAS).convert("L") image2 = im2.resize((20, 20), Image.ANTIALIAS).convert("L") # 將灰度圖標轉成01串,即系二進制數據 pixels1 = list(image1.getdata()) pixels2 = list(image2.getdata()) avg1 = sum(pixels1) / len(pixels1) avg2 = sum(pixels2) / len(pixels2) hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pixels1)) hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pixels2)) # 統計兩個01串不同數字的個數 match = sum(map(operator.ne, hash1, hash2)) # 原作者郭靖愕然閥值設為10,實測400位數字,50合適 return match < 50 # 判斷矩陣是否全為0 def isAllZero(self, arr): for i in range(1, 9): for j in range(1, 13): if arr[i][j] != 0: return False return True # 是否為同行或同列且可連 def isReachable(self, x1, y1, x2, y2): # 1、先判斷值是否相同 if self.im2num_arr[x1][y1] != self.im2num_arr[x2][y2]: return False # 1、分別獲取兩個坐標同行或同列可連的坐標數組 list1 = self.getDirectConnectList(x1, y1) list2 = self.getDirectConnectList(x2, y2) # print(x1, y1, list1) # print(x2, y2, list2) # exit() # 2、比較坐標數組中是否可連 for x1, y1 in list1: for x2, y2 in list2: if self.isDirectConnect(x1, y1, x2, y2): return True return False # 獲取同行或同列可連的坐標數組 def getDirectConnectList(self, x, y): plist = [] for px in range(0, 10): for py in range(0, 14): # 獲取同行或同列且為0的坐標 if self.im2num_arr[px][py] == 0 and self.isDirectConnect(x, y, px, py): plist.append([px, py]) return plist # 是否為同行或同列且可連 def isDirectConnect(self, x1, y1, x2, y2): # 1、位置完全相同 if x1 == x2 and y1 == y2: return False # 2、行列都不同的 if x1 != x2 and y1 != y2: return False # 3、同行 if x1 == x2 and self.isRowConnect(x1, y1, y2): return True # 4、同列 if y1 == y2 and self.isColConnect(y1, x1, x2): return True return False # 判斷同行是否可連 def isRowConnect(self, x, y1, y2): minY = min(y1, y2) maxY = max(y1, y2) # 相鄰直接可連 if maxY - minY == 1: return True # 判斷兩個坐標之間是否全為0 for y0 in range(minY + 1, maxY): if self.im2num_arr[x][y0] != 0: return False return True # 判斷同列是否可連 def isColConnect(self, y, x1, x2): minX = min(x1, x2) maxX = max(x1, x2) # 相鄰直接可連 if maxX - minX == 1: return True # 判斷兩個坐標之間是否全為0 for x0 in range(minX + 1, maxX): if self.im2num_arr[x0][y] != 0: return False return True # 點擊事件並設置數組為0 def clickAndSetZero(self, x1, y1, x2, y2): # print("click", x1, y1, x2, y2) # (299, 251, 768, 564) # 原理:左上角圖標中點 + 偏移量 p1_x = int(self.scree_left_and_right_point[0] + (y1 - 1)*self.im_width + (self.im_width / 2)) p1_y = int(self.scree_left_and_right_point[1] + (x1 - 1)*self.im_width + (self.im_width / 2)) p2_x = int(self.scree_left_and_right_point[0] + (y2 - 1)*self.im_width + (self.im_width / 2)) p2_y = int(self.scree_left_and_right_point[1] + (x2 - 1)*self.im_width + (self.im_width / 2)) time.sleep(0.1) #原作者郭靖愕然用的click方法,實測click為雙擊,press單擊 self.mouse.press(p1_x, p1_y) time.sleep(0.1) self.mouse.press(p2_x, p2_y) # 設置矩陣值為0 #while self.im2num_arr[x1][y1-1]!=0 #上面都是0沒有圖標,就置矩陣值為0 self.im2num_arr[x1][y1] = 0 self.im2num_arr[x2][y2] = 0 print("消除:(%d, %d) (%d, %d)" % (x1, y1, x2, y2)) # exit() # 程序入口、控制中心 def start(self): # 1、先截取游戲區域大圖,然后分切每個小圖 image_list = self.screenshot() # 2、識別小圖標,收集編號 self.image2num(image_list) print(self.im2num_arr) # 3、遍歷查找可以相連的坐標 while not self.isAllZero(self.im2num_arr): for x1 in range(1, 9): for y1 in range(1, 13): if self.im2num_arr[x1][y1] == 0: continue for x2 in range(1, 9): for y2 in range(1, 13): # 跳過為0 或者同一個 if self.im2num_arr[x2][y2] == 0 or (x1 == x2 and y1 == y2): continue if self.isReachable(x1, y1, x2, y2): self.clickAndSetZero(x1, y1, x2, y2) if __name__ == "__main__": # wdname 為連連看窗口的名稱,必須寫完整 wdname = '寵物連連看經典版2,寵物連連看經典版2小游戲,4399小游戲 www.4399.com - Google Chrome' demo = GameAssist(wdname) demo.start()