當前位置:
首頁 > 知識 > 200行Python 實現的qq連連看輔助, 用於學習, 請不要拿去傷害玩家們

200行Python 實現的qq連連看輔助, 用於學習, 請不要拿去傷害玩家們



Linux編程

點擊右側關注,免費入門到精通!

作者丨Laziji


https://laboo.top/2018/11/07/lianliankan/

前言



Python 實現的qq連連看輔助, 僅用於學習, 請在練習模式下使用, 請不要拿去傷害玩家們...



基本環境配置



版本:Python3.6



系統:Windows



相關模塊:


import

 PIL.ImageGrab

import

 pyautogui

import

 win32api

import

 win32gui

import

 win32con

import

 time

import

 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_

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))

    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_id

def 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

] = 0
                    matrix[

y2

][

x2

] = 0
                    if 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_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遊戲 - 連連看角色版")
    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_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 = {}

    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、資料庫研發、幽默程序員等。

萬水千山總是情,點個 「

好看

」 行不行

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Python開發 的精彩文章:

這個事故告訴我們,該放手時就放手!

TAG:Python開發 |