200行Python 實現的qq連連看輔助, 用於學習, 請不要拿去傷害玩家們
Linux編程
點擊右側關注,免費入門到精通!
作者丨Laziji
https://laboo.top/2018/11/07/lianliankan/
前言
Python 實現的qq連連看輔助, 僅用於學習, 請在練習模式下使用, 請不要拿去傷害玩家們...
基本環境配置
版本:Python3.6
系統:Windows
相關模塊:
import import
pyautogui
import
win32apiimport
win32guiimport
win32conimport
timeimport
random
使用方法
開始遊戲後運行就行了, 再次提示, 請在練習模式中使用, 否則可能會被其他玩家舉報
效果圖
代碼實現
import PIL.ImageGrab
import pyautogui
import win32api
import win32gui
import win32con
import time
import random
"""
想要學習Python?Python學習交流群:452739833滿足你的需求,資料都已經上傳群文件流,可以自行下載!
"""
def color_hash(color):
value = ""
for i in range(5):
value += "%d,%d,%d," % (color[0], color[1], color[2])
return hash(value)
def image_hash(img):
value = ""
for i in range(5):
c = img.getpixel((i
* 3, i *
3))value += "%d,%d,%d," % (c[0], c[1], c[2])
return hash(value)
def game
_area_
image_to_
matrix():pos
_to_
image = {} for row in range(ROW_NUM):
pos
_to_
image[row] = {}for col in range(COL_NUM):
grid
_left = col * grid_
widthgrid
_top = row * grid_
height
grid
_right = grid_
left + grid_widthgrid
_bottom = grid_
top + grid_heightgrid
_image = game_
area_image.crop((grid_
left, grid_top, grid_
right, grid_bottom))pos
_to_
image[row
][col
] = grid_imagepos
_to_
type_id = {}image_map = {}
empty
_hash = color_
hash((48, 76, 112))
for row in range(ROW_NUM):
pos
_to_
type_id[row] = {}for col in range(COL_NUM):
this
_image = pos_
to_image[row
][col
]this
_image_
hash = image_hash(this_
image)if this
_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)return pos
_to_
type_iddef solve
_matrix_
one_step():for key in map:
arr = map[key]
arr_len = len(arr)
for index1 in range(arr_len - 1):
point1 = arr[index1]
x1 = point1[0]
y1 = point1[1]
for index2 in range(index1 + 1, arr_len):
point2 = arr[index2]
x2 = point2[0]
y2 = point2[1]
if verifying_connectivity(x1, y1, x2, y2):
arr.remove(point1)
arr.remove(point2)
matrix[
y1
][x1
] = 0matrix[
y2
][x2
] = 0if arr_len == 2:
map.pop(key)
return y1, x1, y2, x2
def verifying_connectivity(x1, y1, x2, y2):
max_y1 = y1
while max
_y1 + 1 < ROW_
NUM and matrix[max_y1 + 1
][x1
] == 0:max_y1 += 1
min_y1 = y1
while min
_y1 - 1 >= 0 and matrix[min_
y1 - 1][x1] == 0:min_y1 -= 1
max_y2 = y2
while max
_y2 + 1 < ROW_
NUM and matrix[max_y2 + 1
][x2
] == 0:max_y2 += 1
min_y2 = y2
while min
_y2 - 1 >= 0 and matrix[min_
y2 - 1][x2] == 0:min_y2 -= 1
rg
_min_
y = max(min_y1, min_
y2)rg
_max_
y = min(max_y1, max_
y2)if rg
_max_
y >= rg_min_
y:for index
_y in range(rg_
min_y, rg_
max_y + 1):min_x = min(x1, x2)
max_x = max(x1, x2)
flag = True
for index
_x in range(min_
x + 1, max_x):if matrix[
index_y
][index_x
] != 0:flag = False
break
if flag:
return True
max_x1 = x1
while max
_x1 + 1 < COL_
NUM and matrix[y1
][max_x1 + 1
] == 0:max_x1 += 1
min_x1 = x1
while min
_x1 - 1 >= 0 and matrix[y1][min_
x1 - 1] == 0:min_x1 -= 1
max_x2 = x2
while max
_x2 + 1 < COL_
NUM and matrix[y2
][max_x2 + 1
] == 0:max_x2 += 1
min_x2 = x2
while min
_x2 - 1 >= 0 and matrix[y2][min_
x2 - 1] == 0:min_x2 -= 1
rg
_min_
x = max(min_x1, min_
x2)rg
_max_
x = min(max_x1, max_
x2)if rg
_max_
x >= rg_min_
x:for index
_x in range(rg_
min_x, rg_
max_x + 1):min_y = min(y1, y2)
max_y = max(y1, y2)
flag = True
for index
_y in range(min_
y + 1, max_y):if matrix[
index_y
][index_x
] != 0:flag = False
break
if flag:
return True
return False
def execute
_one_
step(one_step):from
_row, from_
col, to_row, to_
col = one_stepfrom
_x = game_
area_left + (from_
col + 0.5) * grid_widthfrom
_y = game_
area_top + (from_
row + 0.5) * grid_heightto
_x = game_
area_left + (to_
col + 0.5) * grid_widthto
_y = game_
area_top + (to_
row + 0.5) * grid_heightpyautogui.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遊戲 - 連連看角色版")
if hwnd == 0:
exit(-1)
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
win32gui.SetForegroundWindow(hwnd)
window
_left, window_
top, window_right, window_
bottom = win32gui.GetWindowRect(hwnd)if min(window
_left, window_
top)<
0
or
window_right
> screen_width or window_
bottom > screen_height:exit(-1)
window
_width = window_
right - window_leftwindow
_height = window_
bottom - window_topgame
_area_
left = window_left + 14.0 / 800.0 * window_
widthgame
_area_
top = window_top + 181.0 / 600.0 * window_
heightgame
_area_
right = window_left + 603 / 800.0 * window_
widthgame
_area_
bottom = window_top + 566 / 600.0 * window_
heightgame
_area_
width = game_area_
right - game_area_
leftgame
_area_
height = game_area_
bottom - game_area_
topgrid
_width = game_
area_width / COL_
NUMgrid
_height = game_
area_height / ROW_
NUMgame
_area_
image = PIL.ImageGrab.grab((game_area_
left, game_area_
top, game_area_
right, game_area_
bottom))matrix = game
_area_
image_to_
matrix()map = {}
for y in range(ROW_NUM):
for x in range(COL_NUM):
grid_id = matrix[
y
][x
]if grid_id == 0:
continue
map.setdefault(grid_id, [])
arr = map[grid_id]
arr.append([x, y])
pyautogui.PAUSE = 0
while True:
one
_step = solve_
matrix_one_
step()if not one_step:
exit(0)
execute
_one_
step(one_step)time.sleep(random.randint(0,0)/1000)
(左右滑動可查看完整代碼)
主要思路就是利用pywin32獲取連連看遊戲句柄, 獲取遊戲界面的圖片, 對方塊進行切割, 對每個方塊取幾個點的顏色進行比對, 均相同則認為是同一個方塊
然後模擬滑鼠去消就行了, 代碼的最後一行是每次點擊的間隔
推薦↓↓↓
長
按
關
注
??
【
16個技術公眾號
】都在這裡!
涵蓋:程序員大咖、源碼共讀、程序員共讀、數據結構與演算法、黑客技術和網路安全、大數據科技、編程前端、Java、Python、Web編程開發、Android、iOS開發、Linux、資料庫研發、幽默程序員等。
萬水千山總是情,點個 「
好看
」 行不行

TAG:Python開發 |