Python實(shí)現(xiàn)自動(dòng)玩連連看的腳本分享
序言
最近女朋友在玩連連看,玩了一個(gè)星期了還沒(méi)通關(guān),真的是菜。
我實(shí)在是看不過(guò)去了,直接用python寫了個(gè)腳本代碼,一分鐘一把游戲。
快是快,就是聯(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 = "連連看"
時(shí)間間隔隨機(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
空?qǐng)D像編號(hào)
EMPTY_ID = 0
切片處理時(shí)候的左上、右下坐標(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)
? ? # 沒(méi)有定位到游戲窗體
? ? 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類型對(duì)象
scim = ImageGrab.grab()
scim.save('screen.png')
# 用opencv讀取屏幕截圖
# 獲取ndarray
return cv2.imread("screen.png")
從截圖中分辨圖片 處理成地圖
def getAllSquare(screen_image, game_pos):
? ? print('Processing pictures...')
? ? # 通過(guò)游戲窗體定位
? ? # 加上偏移量獲取游戲區(qū)域
? ? game_x = game_pos[0] + MARGIN_LEFT
? ? game_y = game_pos[1] + MARGIN_HEIGHT
? ? # 從游戲區(qū)域左上開(kāi)始
? ? # 把圖像按照具體大小切割成相同的小塊
? ? # 切割標(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)
? ? # 因?yàn)橛行﹫D片的邊緣會(huì)造成干擾,所以統(tǒng)一把圖片往內(nèi)縮小一圈
? ? # 對(duì)所有的方塊進(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:
# 兩個(gè)圖片進(jìn)行比較 返回的是兩個(gè)圖片的標(biāo)準(zhǔn)差
b = np.subtract(existed_img, img)
# 若標(biāo)準(zhǔn)差全為0 即兩張圖片沒(méi)有區(qū)別
if not np.any(b):
return i
i = i + 1
return -1
獲取所有的方塊類型
def getAllSquareTypes(all_square):
print("Init pictures types...")
types = []
# number列表用來(lái)記錄每個(gè)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)
# 如果這個(gè)圖像不存在則插入列表
if nid == -1:
types.append(square)
number.append(1);
else:
# 若這個(gè)圖像存在則給計(jì)數(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àn)樵谏厦鎸?duì)截屏切片時(shí)是以列為優(yōu)先切片的
所以生成的record二維矩陣每行存放的其實(shí)是游戲屏幕中每列的編號(hào)
換個(gè)說(shuō)法就是record其實(shí)是游戲屏幕中心對(duì)稱后的列表
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個(gè)方塊時(shí)我們認(rèn)為本列處理完畢
if len(line) == V_NUM:
print(line);
record.append(line)
line = []
return record判斷給出的兩個(gè)圖像能否消除
def canConnect(x1, y1, x2, y2, r): ? ? result = r[:] ? ? # 如果兩個(gè)圖像中有一個(gè)為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 ? ? # 判斷一個(gè)拐點(diǎn)可連通 ? ? if turnOnceCheck(x1, y1, x2, y2, result): ? ? ? ? return True ? ? # 判斷兩個(gè)拐點(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)
# 判斷兩個(gè)方塊是否相鄰
if (endY - startY) == 1:
return True
# 判斷兩個(gè)方塊通路上是否都是0,有一個(gè)不是,就說(shuō)明不能聯(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) ? ? # 判斷兩個(gè)方塊是否相鄰 ? ? if (endX - startX) == 1: ? ? ? ? return True ? ? # 判斷兩方塊兒通路上是否可連。 ? ? for i in range(startX + 1, endX): ? ? ? ? if result[i][y1] != EMPTY_ID: ? ? ? ? ? ? return False ? ? return True
判斷一個(gè)拐點(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)為空,從第一個(gè)點(diǎn)到拐點(diǎn)并且從拐點(diǎn)到第二個(gè)點(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
判斷兩個(gè)拐點(diǎn)可聯(lián)通
def turnTwiceCheck(x1, y1, x2, y2, result): ? ? if x1 == x2 and y1 == y2: ? ? ? ? return False ? ? # 遍歷整個(gè)數(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
自動(dòng)消除
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:
? ? ? ? ? ? ? ? # 再次遍歷地圖 尋找另一個(gè)滿足條件的圖片
? ? ? ? ? ? ? ? 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):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 消除的兩個(gè)位置設(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))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 計(jì)算當(dāng)前兩個(gè)位置的圖片在游戲中應(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)擊第一個(gè)圖片所在的位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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ī)時(shí)間 ,防止檢測(cè)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 模擬鼠標(biāo)點(diǎn)擊第二個(gè)圖片所在的位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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)不出來(lái)效果,大家可以自行試試。
全部代碼
# -*- 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 = "連連看"
# 時(shí)間間隔隨機(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
# 空?qǐng)D像編號(hào)
EMPTY_ID = 0
# 切片處理時(shí)候的左上、右下坐標(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)
# 沒(méi)有定位到游戲窗體
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類型對(duì)象
scim = ImageGrab.grab()
scim.save('screen.png')
# 用opencv讀取屏幕截圖
# 獲取ndarray
return cv2.imread("screen.png")
def getAllSquare(screen_image, game_pos):
print('Processing pictures...')
# 通過(guò)游戲窗體定位
# 加上偏移量獲取游戲區(qū)域
game_x = game_pos[0] + MARGIN_LEFT
game_y = game_pos[1] + MARGIN_HEIGHT
# 從游戲區(qū)域左上開(kāi)始
# 把圖像按照具體大小切割成相同的小塊
# 切割標(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)
# 因?yàn)橛行﹫D片的邊緣會(huì)造成干擾,所以統(tǒng)一把圖片往內(nèi)縮小一圈
# 對(duì)所有的方塊進(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:
# 兩個(gè)圖片進(jìn)行比較 返回的是兩個(gè)圖片的標(biāo)準(zhǔn)差
b = np.subtract(existed_img, img)
# 若標(biāo)準(zhǔn)差全為0 即兩張圖片沒(méi)有區(qū)別
if not np.any(b):
return i
i = i + 1
return -1
def getAllSquareTypes(all_square):
print("Init pictures types...")
types = []
# number列表用來(lái)記錄每個(gè)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)
# 如果這個(gè)圖像不存在則插入列表
if nid == -1:
types.append(square)
number.append(1);
else:
# 若這個(gè)圖像存在則給計(jì)數(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àn)樵谏厦鎸?duì)截屏切片時(shí)是以列為優(yōu)先切片的
# 所以生成的record二維矩陣每行存放的其實(shí)是游戲屏幕中每列的編號(hào)
# 換個(gè)說(shuō)法就是record其實(shí)是游戲屏幕中心對(duì)稱后的列表
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個(gè)方塊時(shí)我們認(rèn)為本列處理完畢
if len(line) == V_NUM:
print(line);
record.append(line)
line = []
return record
def canConnect(x1, y1, x2, y2, r):
result = r[:]
# 如果兩個(gè)圖像中有一個(gè)為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
# 判斷一個(gè)拐點(diǎn)可連通
if turnOnceCheck(x1, y1, x2, y2, result):
return True
# 判斷兩個(gè)拐點(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)
# 判斷兩個(gè)方塊是否相鄰
if (endY - startY) == 1:
return True
# 判斷兩個(gè)方塊通路上是否都是0,有一個(gè)不是,就說(shuō)明不能聯(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)
# 判斷兩個(gè)方塊是否相鄰
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)為空,從第一個(gè)點(diǎn)到拐點(diǎn)并且從拐點(diǎn)到第二個(gè)點(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
# 遍歷整個(gè)數(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:
# 再次遍歷地圖 尋找另一個(gè)滿足條件的圖片
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):
# 消除的兩個(gè)位置設(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))
# 計(jì)算當(dāng)前兩個(gè)位置的圖片在游戲中應(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)擊第一個(gè)圖片所在的位置
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ī)時(shí)間 ,防止檢測(cè)
time.sleep(random.uniform(TIME_INTERVAL_MIN, TIME_INTERVAL_MAX))
# 模擬鼠標(biāo)點(diǎn)擊第二個(gè)圖片所在的位置
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)不再有可消除的方塊時(shí)結(jié)束 , 返回消除數(shù)量
return
if __name__ == '__main__':
random.seed()
# i. 定位游戲窗體
game_pos = getGameWindow()
time.sleep(1)
# ii. 獲取屏幕截圖
screen_image = getScreenImage()
# iii. 對(duì)截圖切片,形成一張二維地圖
all_square_list = getAllSquare(screen_image, game_pos)
# iv. 獲取所有類型的圖形,并編號(hào)
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)自動(dòng)玩連連看的腳本分享的詳細(xì)內(nèi)容,更多關(guān)于Python連連看的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解
今天小編就為大家分享一篇基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python scrapy腳本報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了python scrapy腳本報(bào)錯(cuò)問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
python在html中插入簡(jiǎn)單的代碼并加上時(shí)間戳的方法
今天小編就為大家分享一篇python在html中插入簡(jiǎn)單的代碼并加上時(shí)間戳的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python控制自己的手機(jī)攝像頭拍照并自動(dòng)發(fā)送到郵箱案例講解
這篇文章主要介紹了Python控制自己的手機(jī)攝像頭拍照,并把照片自動(dòng)發(fā)送到郵箱,大概思路是通過(guò)opencv調(diào)用攝像頭拍照保存圖像本地用email庫(kù)構(gòu)造郵件內(nèi)容,保存的圖像以附件形式插入郵件內(nèi)容用smtplib庫(kù)發(fā)送郵件到指定郵箱,需要的朋友可以參考下2022-04-04
Python loguru日志庫(kù)之高效輸出控制臺(tái)日志和日志記錄
這篇文章主要介紹了python loguru日志庫(kù)之高效輸出控制臺(tái)日志和日志記錄的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Java文件與類動(dòng)手動(dòng)腦實(shí)例詳解
在本篇文章里小編給大家整理的是關(guān)于Java文件與類動(dòng)手動(dòng)腦實(shí)例知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)參考下。2019-11-11
win7 下搭建sublime的python開(kāi)發(fā)環(huán)境的配置方法
Sublime Text具有漂亮的用戶界面和強(qiáng)大的功能,例如代碼縮略圖,Python的插件,代碼段等。還可自定義鍵綁定,菜單和工具欄。Sublime Text的主要功能包括:拼寫檢查,書(shū)簽,完整的 Python API,Goto功能,即時(shí)項(xiàng)目切換,多選擇,多窗口等等。2014-06-06

