Python Assistant for QQ LianLianKan Game – Environment Setup and Full Implementation
This article presents a Python-based assistant for the QQ LianLianKan game, detailing environment setup, required modules, and a full script that captures the game window, analyzes tile colors, determines removable pairs, and automates mouse clicks to solve the puzzle, intended for learning purposes only.
The article introduces a Python implementation of a QQ LianLianKan (连连看) helper, explicitly stating that it is for learning and should be used only in practice mode to avoid harming other players.
1. Basic Environment Configuration
Python version: 3.6 Operating system: Windows
2. Required Modules
<code>import PIL.ImageGrab</code>
<code>import pyautogui</code>
<code>import win32api</code>
<code>import win32gui</code>
<code>import win32con</code>
<code>import time</code>
<code>import random</code>3. Usage
Run the script after starting the game; the script will automatically locate matching tiles and click them. It is emphasized to run the program in practice mode to avoid being reported by other players.
4. Code Implementation
<code>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
# ... (rest of the function builds type‑id map)
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, y1 = point1[0], point1[1]
for index2 in range(index1 + 1, arr_len):
point2 = arr[index2]
x2, y2 = point2[0], 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
return None
def verifying_connectivity(x1, y1, x2, y2):
# Checks vertical and horizontal paths between two tiles
# Returns True if a path with at most two bends exists
# (implementation omitted for brevity)
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, [])
map[grid_id].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)</code>The main idea is to use pywin32 to obtain the game window handle, capture the game area as an image, split it into tiles, compare colors of several points on each tile, treat identical tiles as a pair, and then simulate mouse clicks to remove them. The final line of the script controls the click interval.
Disclaimer: This article is compiled from online sources; all rights belong to the original author. If any content infringes your rights, please contact us for removal or authorization.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.