Python實(shí)現(xiàn)自動玩連連看的腳本分享
序言
最近女朋友在玩連連看,玩了一個星期了還沒通關(guān),真的是菜。
我實(shí)在是看不過去了,直接用python寫了個腳本代碼,一分鐘一把游戲。
快是快,就是聯(lián)網(wǎng)玩容易被罵,嘿嘿~
實(shí)現(xiàn)步驟
模塊導(dǎo)入
import cv2 import numpy as np import win32api import win32gui import win32con from PIL import ImageGrab import time import random
窗體標(biāo)題 用于定位游戲窗體
WINDOW_TITLE = "連連看"
時間間隔隨機(jī)生成 [MIN,MAX]
TIME_INTERVAL_MAX = 0.06 TIME_INTERVAL_MIN = 0.1
游戲區(qū)域距離頂點(diǎn)的x偏移
MARGIN_LEFT = 10
游戲區(qū)域距離頂點(diǎn)的y偏移
MARGIN_HEIGHT = 180
橫向的方塊數(shù)量
H_NUM = 19
縱向的方塊數(shù)量
V_NUM = 11
方塊寬度
POINT_WIDTH = 31
方塊高度
POINT_HEIGHT = 35
空圖像編號
EMPTY_ID = 0
切片處理時候的左上、右下坐標(biāo):
SUB_LT_X = 8 SUB_LT_Y = 8 SUB_RB_X = 27 SUB_RB_Y = 27
游戲的最多消除次數(shù)
MAX_ROUND = 200
獲取窗體坐標(biāo)位置
def getGameWindow(): ? ? # FindWindow(lpClassName=None, lpWindowName=None) ?窗口類名 窗口標(biāo)題名 ? ? window = win32gui.FindWindow(None, WINDOW_TITLE) ? ? # 沒有定位到游戲窗體 ? ? while not window: ? ? ? ? print('Failed to locate the game window , reposition the game window after 10 seconds...') ? ? ? ? time.sleep(10) ? ? ? ? window = win32gui.FindWindow(None, WINDOW_TITLE) ? ? # 定位到游戲窗體 ? ? # 置頂游戲窗口 ? ? win32gui.SetForegroundWindow(window) ? ? pos = win32gui.GetWindowRect(window) ? ? print("Game windows at " + str(pos)) ? ? return (pos[0], pos[1])
獲取屏幕截圖
def getScreenImage(): print('Shot screen...') # 獲取屏幕截圖 Image類型對象 scim = ImageGrab.grab() scim.save('screen.png') # 用opencv讀取屏幕截圖 # 獲取ndarray return cv2.imread("screen.png")
從截圖中分辨圖片 處理成地圖
def getAllSquare(screen_image, game_pos): ? ? print('Processing pictures...') ? ? # 通過游戲窗體定位 ? ? # 加上偏移量獲取游戲區(qū)域 ? ? game_x = game_pos[0] + MARGIN_LEFT ? ? game_y = game_pos[1] + MARGIN_HEIGHT ? ? # 從游戲區(qū)域左上開始 ? ? # 把圖像按照具體大小切割成相同的小塊 ? ? # 切割標(biāo)準(zhǔn)是按照小塊的橫縱坐標(biāo) ? ? all_square = [] ? ? for x in range(0, H_NUM): ? ? ? ? for y in range(0, V_NUM): ? ? ? ? ? ? # ndarray的切片方法 : [縱坐標(biāo)起始位置:縱坐標(biāo)結(jié)束為止,橫坐標(biāo)起始位置:橫坐標(biāo)結(jié)束位置] ? ? ? ? ? ? square = screen_image[game_y + y * POINT_HEIGHT:game_y + (y + 1) * POINT_HEIGHT, ? ? ? ? ? ? ? ? ? ? ?game_x + x * POINT_WIDTH:game_x + (x + 1) * POINT_WIDTH] ? ? ? ? ? ? all_square.append(square) ? ? # 因為有些圖片的邊緣會造成干擾,所以統(tǒng)一把圖片往內(nèi)縮小一圈 ? ? # 對所有的方塊進(jìn)行處理 ,去掉邊緣一圈后返回 ? ? finalresult = [] ? ? for square in all_square: ? ? ? ? s = square[SUB_LT_Y:SUB_RB_Y, SUB_LT_X:SUB_RB_X] ? ? ? ? finalresult.append(s) ? ? return finalresult
判斷列表中是否存在相同圖形
存在返回進(jìn)行判斷圖片所在的id
否則返回-1
def isImageExist(img, img_list): i = 0 for existed_img in img_list: # 兩個圖片進(jìn)行比較 返回的是兩個圖片的標(biāo)準(zhǔn)差 b = np.subtract(existed_img, img) # 若標(biāo)準(zhǔn)差全為0 即兩張圖片沒有區(qū)別 if not np.any(b): return i i = i + 1 return -1
獲取所有的方塊類型
def getAllSquareTypes(all_square): print("Init pictures types...") types = [] # number列表用來記錄每個id的出現(xiàn)次數(shù) number = [] # 當(dāng)前出現(xiàn)次數(shù)最多的方塊 # 這里我們默認(rèn)出現(xiàn)最多的方塊應(yīng)該是空白塊 nowid = 0; for square in all_square: nid = isImageExist(square, types) # 如果這個圖像不存在則插入列表 if nid == -1: types.append(square) number.append(1); else: # 若這個圖像存在則給計數(shù)器 + 1 number[nid] = number[nid] + 1 if (number[nid] > number[nowid]): nowid = nid # 更新EMPTY_ID # 即判斷在當(dāng)前這張圖中的空白塊id global EMPTY_ID EMPTY_ID = nowid print('EMPTY_ID = ' + str(EMPTY_ID)) return types
將二維圖片矩陣轉(zhuǎn)換為二維數(shù)字矩陣
注意因為在上面對截屏切片時是以列為優(yōu)先切片的
所以生成的record二維矩陣每行存放的其實(shí)是游戲屏幕中每列的編號
換個說法就是record其實(shí)是游戲屏幕中心對稱后的列表
def getAllSquareRecord(all_square_list, types): print("Change map...") record = [] line = [] for square in all_square_list: num = 0 for type in types: res = cv2.subtract(square, type) if not np.any(res): line.append(num) break num += 1 # 每列的數(shù)量為V_NUM # 那么當(dāng)當(dāng)前的line列表中存在V_NUM個方塊時我們認(rèn)為本列處理完畢 if len(line) == V_NUM: print(line); record.append(line) line = [] return record
判斷給出的兩個圖像能否消除
def canConnect(x1, y1, x2, y2, r): ? ? result = r[:] ? ? # 如果兩個圖像中有一個為0 直接返回False ? ? if result[x1][y1] == EMPTY_ID or result[x2][y2] == EMPTY_ID: ? ? ? ? return False ? ? if x1 == x2 and y1 == y2: ? ? ? ? return False ? ? if result[x1][y1] != result[x2][y2]: ? ? ? ? return False ? ? # 判斷橫向連通 ? ? if horizontalCheck(x1, y1, x2, y2, result): ? ? ? ? return True ? ? # 判斷縱向連通 ? ? if verticalCheck(x1, y1, x2, y2, result): ? ? ? ? return True ? ? # 判斷一個拐點(diǎn)可連通 ? ? if turnOnceCheck(x1, y1, x2, y2, result): ? ? ? ? return True ? ? # 判斷兩個拐點(diǎn)可連通 ? ? if turnTwiceCheck(x1, y1, x2, y2, result): ? ? ? ? return True ? ? # 不可聯(lián)通返回False ? ? return False
判斷橫向聯(lián)通
def horizontalCheck(x1, y1, x2, y2, result): if x1 == x2 and y1 == y2: return False if x1 != x2: return False startY = min(y1, y2) endY = max(y1, y2) # 判斷兩個方塊是否相鄰 if (endY - startY) == 1: return True # 判斷兩個方塊通路上是否都是0,有一個不是,就說明不能聯(lián)通,返回false for i in range(startY + 1, endY): if result[x1][i] != EMPTY_ID: return False return True
判斷縱向聯(lián)通
def verticalCheck(x1, y1, x2, y2, result): ? ? if x1 == x2 and y1 == y2: ? ? ? ? return False ? ? if y1 != y2: ? ? ? ? return False ? ? startX = min(x1, x2) ? ? endX = max(x1, x2) ? ? # 判斷兩個方塊是否相鄰 ? ? if (endX - startX) == 1: ? ? ? ? return True ? ? # 判斷兩方塊兒通路上是否可連。 ? ? for i in range(startX + 1, endX): ? ? ? ? if result[i][y1] != EMPTY_ID: ? ? ? ? ? ? return False ? ? return True
判斷一個拐點(diǎn)可聯(lián)通
def turnOnceCheck(x1, y1, x2, y2, result): ? ? if x1 == x2 or y1 == y2: ? ? ? ? return False ? ? cx = x1 ? ? cy = y2 ? ? dx = x2 ? ? dy = y1 ? ? # 拐點(diǎn)為空,從第一個點(diǎn)到拐點(diǎn)并且從拐點(diǎn)到第二個點(diǎn)可通,則整條路可通。 ? ? if result[cx][cy] == EMPTY_ID: ? ? ? ? if horizontalCheck(x1, y1, cx, cy, result) and verticalCheck(cx, cy, x2, y2, result): ? ? ? ? ? ? return True ? ? if result[dx][dy] == EMPTY_ID: ? ? ? ? if verticalCheck(x1, y1, dx, dy, result) and horizontalCheck(dx, dy, x2, y2, result): ? ? ? ? ? ? return True ? ? return False
判斷兩個拐點(diǎn)可聯(lián)通
def turnTwiceCheck(x1, y1, x2, y2, result): ? ? if x1 == x2 and y1 == y2: ? ? ? ? return False ? ? # 遍歷整個數(shù)組找合適的拐點(diǎn) ? ? for i in range(0, len(result)): ? ? ? ? for j in range(0, len(result[1])): ? ? ? ? ? ? # 不為空不能作為拐點(diǎn) ? ? ? ? ? ? if result[i][j] != EMPTY_ID: ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? # 不和被選方塊在同一行列的不能作為拐點(diǎn) ? ? ? ? ? ? if i != x1 and i != x2 and j != y1 and j != y2: ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? # 作為交點(diǎn)的方塊不能作為拐點(diǎn) ? ? ? ? ? ? if (i == x1 and j == y2) or (i == x2 and j == y1): ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? if turnOnceCheck(x1, y1, i, j, result) and ( ? ? ? ? ? ? ? ? ? ? horizontalCheck(i, j, x2, y2, result) or verticalCheck(i, j, x2, y2, result)): ? ? ? ? ? ? ? ? return True ? ? ? ? ? ? if turnOnceCheck(i, j, x2, y2, result) and ( ? ? ? ? ? ? ? ? ? ? horizontalCheck(x1, y1, i, j, result) or verticalCheck(x1, y1, i, j, result)): ? ? ? ? ? ? ? ? return True ? ? return False
自動消除
def autoRelease(result, game_x, game_y): ? ? # 遍歷地圖 ? ? for i in range(0, len(result)): ? ? ? ? for j in range(0, len(result[0])): ? ? ? ? ? ? # 當(dāng)前位置非空 ? ? ? ? ? ? if result[i][j] != EMPTY_ID: ? ? ? ? ? ? ? ? # 再次遍歷地圖 尋找另一個滿足條件的圖片 ? ? ? ? ? ? ? ? for m in range(0, len(result)): ? ? ? ? ? ? ? ? ? ? for n in range(0, len(result[0])): ? ? ? ? ? ? ? ? ? ? ? ? if result[m][n] != EMPTY_ID: ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 若可以執(zhí)行消除 ? ? ? ? ? ? ? ? ? ? ? ? ? ? if canConnect(i, j, m, n, result): ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 消除的兩個位置設(shè)置為空 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? result[i][j] = EMPTY_ID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? result[m][n] = EMPTY_ID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print('Remove :' + str(i + 1) + ',' + str(j + 1) + ' and ' + str(m + 1) + ',' + str( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? n + 1)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 計算當(dāng)前兩個位置的圖片在游戲中應(yīng)該存在的位置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x1 = game_x + j * POINT_WIDTH ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? y1 = game_y + i * POINT_HEIGHT ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x2 = game_x + n * POINT_WIDTH ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? y2 = game_y + m * POINT_HEIGHT ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 模擬鼠標(biāo)點(diǎn)擊第一個圖片所在的位置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? win32api.SetCursorPos((x1 + 15, y1 + 18)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x1 + 15, y1 + 18, 0, 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x1 + 15, y1 + 18, 0, 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 等待隨機(jī)時間 ,防止檢測 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 模擬鼠標(biāo)點(diǎn)擊第二個圖片所在的位置 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? win32api.SetCursorPos((x2 + 15, y2 + 18)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x2 + 15, y2 + 18, 0, 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x2 + 15, y2 + 18, 0, 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 執(zhí)行消除后返回True ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return True ? ? return False
效果的話得上傳視頻,截圖展現(xiàn)不出來效果,大家可以自行試試。
全部代碼
# -*- coding:utf-8 -*- import cv2 import numpy as np import win32api import win32gui import win32con from PIL import ImageGrab import time import random # 窗體標(biāo)題 用于定位游戲窗體 WINDOW_TITLE = "連連看" # 時間間隔隨機(jī)生成 [MIN,MAX] TIME_INTERVAL_MAX = 0.06 TIME_INTERVAL_MIN = 0.1 # 游戲區(qū)域距離頂點(diǎn)的x偏移 MARGIN_LEFT = 10 # 游戲區(qū)域距離頂點(diǎn)的y偏移 MARGIN_HEIGHT = 180 # 橫向的方塊數(shù)量 H_NUM = 19 # 縱向的方塊數(shù)量 V_NUM = 11 # 方塊寬度 POINT_WIDTH = 31 # 方塊高度 POINT_HEIGHT = 35 # 空圖像編號 EMPTY_ID = 0 # 切片處理時候的左上、右下坐標(biāo): SUB_LT_X = 8 SUB_LT_Y = 8 SUB_RB_X = 27 SUB_RB_Y = 27 # 游戲的最多消除次數(shù) MAX_ROUND = 200 def getGameWindow(): # FindWindow(lpClassName=None, lpWindowName=None) 窗口類名 窗口標(biāo)題名 window = win32gui.FindWindow(None, WINDOW_TITLE) # 沒有定位到游戲窗體 while not window: print('Failed to locate the game window , reposition the game window after 10 seconds...') time.sleep(10) window = win32gui.FindWindow(None, WINDOW_TITLE) # 定位到游戲窗體 # 置頂游戲窗口 win32gui.SetForegroundWindow(window) pos = win32gui.GetWindowRect(window) print("Game windows at " + str(pos)) return (pos[0], pos[1]) def getScreenImage(): print('Shot screen...') # 獲取屏幕截圖 Image類型對象 scim = ImageGrab.grab() scim.save('screen.png') # 用opencv讀取屏幕截圖 # 獲取ndarray return cv2.imread("screen.png") def getAllSquare(screen_image, game_pos): print('Processing pictures...') # 通過游戲窗體定位 # 加上偏移量獲取游戲區(qū)域 game_x = game_pos[0] + MARGIN_LEFT game_y = game_pos[1] + MARGIN_HEIGHT # 從游戲區(qū)域左上開始 # 把圖像按照具體大小切割成相同的小塊 # 切割標(biāo)準(zhǔn)是按照小塊的橫縱坐標(biāo) all_square = [] for x in range(0, H_NUM): for y in range(0, V_NUM): # ndarray的切片方法 : [縱坐標(biāo)起始位置:縱坐標(biāo)結(jié)束為止,橫坐標(biāo)起始位置:橫坐標(biāo)結(jié)束位置] square = screen_image[game_y + y * POINT_HEIGHT:game_y + (y + 1) * POINT_HEIGHT, game_x + x * POINT_WIDTH:game_x + (x + 1) * POINT_WIDTH] all_square.append(square) # 因為有些圖片的邊緣會造成干擾,所以統(tǒng)一把圖片往內(nèi)縮小一圈 # 對所有的方塊進(jìn)行處理 ,去掉邊緣一圈后返回 finalresult = [] for square in all_square: s = square[SUB_LT_Y:SUB_RB_Y, SUB_LT_X:SUB_RB_X] finalresult.append(s) return finalresult # 判斷列表中是否存在相同圖形 # 存在返回進(jìn)行判斷圖片所在的id # 否則返回-1 def isImageExist(img, img_list): i = 0 for existed_img in img_list: # 兩個圖片進(jìn)行比較 返回的是兩個圖片的標(biāo)準(zhǔn)差 b = np.subtract(existed_img, img) # 若標(biāo)準(zhǔn)差全為0 即兩張圖片沒有區(qū)別 if not np.any(b): return i i = i + 1 return -1 def getAllSquareTypes(all_square): print("Init pictures types...") types = [] # number列表用來記錄每個id的出現(xiàn)次數(shù) number = [] # 當(dāng)前出現(xiàn)次數(shù)最多的方塊 # 這里我們默認(rèn)出現(xiàn)最多的方塊應(yīng)該是空白塊 nowid = 0; for square in all_square: nid = isImageExist(square, types) # 如果這個圖像不存在則插入列表 if nid == -1: types.append(square) number.append(1); else: # 若這個圖像存在則給計數(shù)器 + 1 number[nid] = number[nid] + 1 if (number[nid] > number[nowid]): nowid = nid # 更新EMPTY_ID # 即判斷在當(dāng)前這張圖中的空白塊id global EMPTY_ID EMPTY_ID = nowid print('EMPTY_ID = ' + str(EMPTY_ID)) return types # 將二維圖片矩陣轉(zhuǎn)換為二維數(shù)字矩陣 # 注意因為在上面對截屏切片時是以列為優(yōu)先切片的 # 所以生成的record二維矩陣每行存放的其實(shí)是游戲屏幕中每列的編號 # 換個說法就是record其實(shí)是游戲屏幕中心對稱后的列表 def getAllSquareRecord(all_square_list, types): print("Change map...") record = [] line = [] for square in all_square_list: num = 0 for type in types: res = cv2.subtract(square, type) if not np.any(res): line.append(num) break num += 1 # 每列的數(shù)量為V_NUM # 那么當(dāng)當(dāng)前的line列表中存在V_NUM個方塊時我們認(rèn)為本列處理完畢 if len(line) == V_NUM: print(line); record.append(line) line = [] return record def canConnect(x1, y1, x2, y2, r): result = r[:] # 如果兩個圖像中有一個為0 直接返回False if result[x1][y1] == EMPTY_ID or result[x2][y2] == EMPTY_ID: return False if x1 == x2 and y1 == y2: return False if result[x1][y1] != result[x2][y2]: return False # 判斷橫向連通 if horizontalCheck(x1, y1, x2, y2, result): return True # 判斷縱向連通 if verticalCheck(x1, y1, x2, y2, result): return True # 判斷一個拐點(diǎn)可連通 if turnOnceCheck(x1, y1, x2, y2, result): return True # 判斷兩個拐點(diǎn)可連通 if turnTwiceCheck(x1, y1, x2, y2, result): return True # 不可聯(lián)通返回False return False def horizontalCheck(x1, y1, x2, y2, result): if x1 == x2 and y1 == y2: return False if x1 != x2: return False startY = min(y1, y2) endY = max(y1, y2) # 判斷兩個方塊是否相鄰 if (endY - startY) == 1: return True # 判斷兩個方塊通路上是否都是0,有一個不是,就說明不能聯(lián)通,返回false for i in range(startY + 1, endY): if result[x1][i] != EMPTY_ID: return False return True def verticalCheck(x1, y1, x2, y2, result): if x1 == x2 and y1 == y2: return False if y1 != y2: return False startX = min(x1, x2) endX = max(x1, x2) # 判斷兩個方塊是否相鄰 if (endX - startX) == 1: return True # 判斷兩方塊兒通路上是否可連。 for i in range(startX + 1, endX): if result[i][y1] != EMPTY_ID: return False return True def turnOnceCheck(x1, y1, x2, y2, result): if x1 == x2 or y1 == y2: return False cx = x1 cy = y2 dx = x2 dy = y1 # 拐點(diǎn)為空,從第一個點(diǎn)到拐點(diǎn)并且從拐點(diǎn)到第二個點(diǎn)可通,則整條路可通。 if result[cx][cy] == EMPTY_ID: if horizontalCheck(x1, y1, cx, cy, result) and verticalCheck(cx, cy, x2, y2, result): return True if result[dx][dy] == EMPTY_ID: if verticalCheck(x1, y1, dx, dy, result) and horizontalCheck(dx, dy, x2, y2, result): return True return False def turnTwiceCheck(x1, y1, x2, y2, result): if x1 == x2 and y1 == y2: return False # 遍歷整個數(shù)組找合適的拐點(diǎn) for i in range(0, len(result)): for j in range(0, len(result[1])): # 不為空不能作為拐點(diǎn) if result[i][j] != EMPTY_ID: continue # 不和被選方塊在同一行列的不能作為拐點(diǎn) if i != x1 and i != x2 and j != y1 and j != y2: continue # 作為交點(diǎn)的方塊不能作為拐點(diǎn) if (i == x1 and j == y2) or (i == x2 and j == y1): continue if turnOnceCheck(x1, y1, i, j, result) and ( horizontalCheck(i, j, x2, y2, result) or verticalCheck(i, j, x2, y2, result)): return True if turnOnceCheck(i, j, x2, y2, result) and ( horizontalCheck(x1, y1, i, j, result) or verticalCheck(x1, y1, i, j, result)): return True return False def autoRelease(result, game_x, game_y): # 遍歷地圖 for i in range(0, len(result)): for j in range(0, len(result[0])): # 當(dāng)前位置非空 if result[i][j] != EMPTY_ID: # 再次遍歷地圖 尋找另一個滿足條件的圖片 for m in range(0, len(result)): for n in range(0, len(result[0])): if result[m][n] != EMPTY_ID: # 若可以執(zhí)行消除 if canConnect(i, j, m, n, result): # 消除的兩個位置設(shè)置為空 result[i][j] = EMPTY_ID result[m][n] = EMPTY_ID print('Remove :' + str(i + 1) + ',' + str(j + 1) + ' and ' + str(m + 1) + ',' + str( n + 1)) # 計算當(dāng)前兩個位置的圖片在游戲中應(yīng)該存在的位置 x1 = game_x + j * POINT_WIDTH y1 = game_y + i * POINT_HEIGHT x2 = game_x + n * POINT_WIDTH y2 = game_y + m * POINT_HEIGHT # 模擬鼠標(biāo)點(diǎn)擊第一個圖片所在的位置 win32api.SetCursorPos((x1 + 15, y1 + 18)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x1 + 15, y1 + 18, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x1 + 15, y1 + 18, 0, 0) # 等待隨機(jī)時間 ,防止檢測 time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX)) # 模擬鼠標(biāo)點(diǎn)擊第二個圖片所在的位置 win32api.SetCursorPos((x2 + 15, y2 + 18)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x2 + 15, y2 + 18, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x2 + 15, y2 + 18, 0, 0) time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX)) # 執(zhí)行消除后返回True return True return False def autoRemove(squares, game_pos): game_x = game_pos[0] + MARGIN_LEFT game_y = game_pos[1] + MARGIN_HEIGHT # 重復(fù)一次消除直到到達(dá)最多消除次數(shù) while True: if not autoRelease(squares, game_x, game_y): # 當(dāng)不再有可消除的方塊時結(jié)束 , 返回消除數(shù)量 return if __name__ == '__main__': random.seed() # i. 定位游戲窗體 game_pos = getGameWindow() time.sleep(1) # ii. 獲取屏幕截圖 screen_image = getScreenImage() # iii. 對截圖切片,形成一張二維地圖 all_square_list = getAllSquare(screen_image, game_pos) # iv. 獲取所有類型的圖形,并編號 types = getAllSquareTypes(all_square_list) # v. 講獲取的圖片地圖轉(zhuǎn)換成數(shù)字矩陣 result = np.transpose(getAllSquareRecord(all_square_list, types)) # vi. 執(zhí)行消除 , 并輸出消除數(shù)量 print('The total elimination amount is ' + str(autoRemove(result, game_pos)))
兄弟們快去試試吧
以上就是Python實(shí)現(xiàn)自動玩連連看的腳本分享的詳細(xì)內(nèi)容,更多關(guān)于Python連連看的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解
今天小編就為大家分享一篇基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python控制自己的手機(jī)攝像頭拍照并自動發(fā)送到郵箱案例講解
這篇文章主要介紹了Python控制自己的手機(jī)攝像頭拍照,并把照片自動發(fā)送到郵箱,大概思路是通過opencv調(diào)用攝像頭拍照保存圖像本地用email庫構(gòu)造郵件內(nèi)容,保存的圖像以附件形式插入郵件內(nèi)容用smtplib庫發(fā)送郵件到指定郵箱,需要的朋友可以參考下2022-04-04Python loguru日志庫之高效輸出控制臺日志和日志記錄
這篇文章主要介紹了python loguru日志庫之高效輸出控制臺日志和日志記錄的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03win7 下搭建sublime的python開發(fā)環(huán)境的配置方法
Sublime Text具有漂亮的用戶界面和強(qiáng)大的功能,例如代碼縮略圖,Python的插件,代碼段等。還可自定義鍵綁定,菜單和工具欄。Sublime Text的主要功能包括:拼寫檢查,書簽,完整的 Python API,Goto功能,即時項目切換,多選擇,多窗口等等。2014-06-06