天天看點

python 微信跳一跳腳本

config.json

{
    "under_game_score_y": 300,
    "press_coefficient": 1.393,
    "piece_base_height_1_2": 20,
    "piece_body_width": 73,
    "swipe" : {
      "x1": 320,
      "y1": 410,
      "x2": 320,
      "y2": 410
    }
}           

複制

a.py 

# coding: utf-8
import os
import shutil
import time
import math
from PIL import Image, ImageDraw
import random
import json


# === 思路 ===
# 核心:每次落穩之後截圖,根據截圖算出棋子的坐标和下一個塊頂面的中點坐标,
#      根據兩個點的距離乘以一個時間系數獲得長按的時間
# 識别棋子:靠棋子的顔色來識别位置,通過截圖發現最下面一行大概是一條直線,就從上往下一行一行周遊,
#         比較顔色(顔色用了一個區間來比較)找到最下面的那一行的所有點,然後求個中點,
#         求好之後再讓 Y 軸坐标減小棋子底盤的一半高度進而得到中心點的坐标
# 識别棋盤:靠底色和方塊的色差來做,從分數之下的位置開始,一行一行掃描,由于圓形的塊最頂上是一條線,
#          方形的上面大概是一個點,是以就用類似識别棋子的做法多識别了幾個點求中點,
#          這時候得到了塊中點的 X 軸坐标,這時候假設現在棋子在目前塊的中心,
#          根據一個通過截圖擷取的固定的角度來推出中點的 Y 坐标
# 最後:根據兩點的坐标算距離乘以系數來擷取長按時間(似乎可以直接用 X 軸距離)


# TODO: 解決定位偏移的問題
# TODO: 看看兩個塊中心到中軸距離是否相同,如果是的話靠這個來判斷一下目前超前還是落後,便于矯正
# TODO: 一些固定值根據截圖的具體大小計算
# TODO: 直接用 X 軸距離簡化邏輯

with open('config.json','r') as f:
    config = json.load(f)

# Magic Number,不設定可能無法正常執行,請根據具體截圖從上到下按需設定
under_game_score_y = config['under_game_score_y']     # 截圖中剛好低于分數顯示區域的 Y 坐标,300 是 1920x1080 的值,2K 屏、全面屏請根據實際情況修改
press_coefficient = config['press_coefficient']       # 長按的時間系數,請自己根據實際情況調節
piece_base_height_1_2 = config['piece_base_height_1_2']   # 二分之一的棋子底座高度,可能要調節
piece_body_width = config['piece_body_width']             # 棋子的寬度,比截圖中量到的稍微大一點比較安全,可能要調節

#swipe_x1, swipe_y1, swipe_x2, swipe_y2 = 320, 410, 320, 410     # 模拟按壓的起始點坐标,需要自動重複遊戲請設定成“再來一局”的坐标

#piece_base_height_1_2 = 23   # 二分之一的棋子底座高度,可能要調節
#piece_body_width = 77       # 棋子的寬度,比截圖中量到的稍微大一點比較安全,可能要調節

# 下面的 (353, 859) 和 (772, 1100) 是遊戲截圖裡的兩個台子的中點坐标,主要用來算角度,可能要調節
sample_board_x1, sample_board_y1, sample_board_x2, sample_board_y2 = 353, 859, 772, 1100


screenshot_backup_dir = 'screenshot_backups/'
if not os.path.isdir(screenshot_backup_dir):
        os.mkdir(screenshot_backup_dir)


def pull_screenshot():
    os.system('adb shell screencap -p /sdcard/1.png')
    os.system('adb pull /sdcard/1.png .')


def backup_screenshot(ts):
    # 為了友善失敗的時候 debug
    if not os.path.isdir(screenshot_backup_dir):
        os.mkdir(screenshot_backup_dir)
    shutil.copy('1.png', '{}{}.png'.format(screenshot_backup_dir, ts))

def save_debug_creenshot(ts, im, piece_x, piece_y, board_x, board_y):
    draw = ImageDraw.Draw(im)
    draw.line((piece_x, piece_y) + (board_x, board_y), fill=2, width=3)
    del draw
    im.save("{}{}_d.png".format(screenshot_backup_dir, ts))

def set_button_position(im):
    # 将swipe設定為 `再來一局` 按鈕的位置
    global swipe_x1, swipe_y1, swipe_x2, swipe_y2
    w, h = im.size
    left = w / 2
    top = 1003 * (h / 1280.0) + 10
    swipe_x1, swipe_y1, swipe_x2, swipe_y2 = left, top, left, top

def jump(distance):
    press_time = distance * press_coefficient
    press_time = max(press_time, 200)   # 設定 200 ms 是最小的按壓時間
    press_time = int(press_time)
    cmd = 'adb shell input swipe {} {} {} {} {}'.format(swipe_x1, swipe_y1, swipe_x2, swipe_y2, press_time)
    print(cmd)
    os.system(cmd)


def find_piece_and_board(im):
    w, h = im.size

    piece_x_sum = 0
    piece_x_c = 0
    piece_y_max = 0
    board_x = 0
    board_y = 0
    scan_x_border = int(w / 8)  # 掃描棋子時的左右邊界
    scan_start_y = 0  # 掃描的起始y坐标
    im_pixel=im.load()
    # 以50px步長,嘗試探測scan_start_y
    for i in range(under_game_score_y, h, 50):
        last_pixel = im_pixel[0,i]
        for j in range(1, w):
            pixel=im_pixel[j,i]
            # 不是純色的線,則記錄scan_start_y的值,準備跳出循環
            if pixel[0] != last_pixel[0] or pixel[1] != last_pixel[1] or pixel[2] != last_pixel[2]:
                scan_start_y = i - 50
                break
        if scan_start_y:
            break
    print("scan_start_y: ", scan_start_y)

    # 從scan_start_y開始往下掃描,棋子應位于螢幕上半部分,這裡暫定不超過2/3
    for i in range(scan_start_y, int(h * 2 / 3)):
        for j in range(scan_x_border, w - scan_x_border):  # 橫坐标方面也減少了一部分掃描開銷
            pixel = im_pixel[j,i]
            # 根據棋子的最低行的顔色判斷,找最後一行那些點的平均值,這個顔色這樣應該 OK,暫時不提出來
            if (50 < pixel[0] < 60) and (53 < pixel[1] < 63) and (95 < pixel[2] < 110):
                piece_x_sum += j
                piece_x_c += 1
                piece_y_max = max(i, piece_y_max)

    if not all((piece_x_sum, piece_x_c)):
        return 0, 0, 0, 0
    piece_x = piece_x_sum / piece_x_c
    piece_y = piece_y_max - piece_base_height_1_2  # 上移棋子底盤高度的一半

    for i in range(scan_start_y, h):
        last_pixel = im_pixel[0, i]
        if board_x or board_y:
            break
        board_x_sum = 0
        board_x_c = 0

        for j in range(w):
            pixel = im_pixel[j,i]
            # 修掉腦袋比下一個小格子還高的情況的 bug
            if abs(j - piece_x) < piece_body_width:
                continue

            # 修掉圓頂的時候一條線導緻的小 bug,這個顔色判斷應該 OK,暫時不提出來
            if abs(pixel[0] - last_pixel[0]) + abs(pixel[1] - last_pixel[1]) + abs(pixel[2] - last_pixel[2]) > 10:
                board_x_sum += j
                board_x_c += 1
        if board_x_sum:
            board_x = board_x_sum / board_x_c
    # 按實際的角度來算,找到接近下一個 board 中心的坐标
    board_y = piece_y - abs(board_x - piece_x) * abs(sample_board_y1 - sample_board_y2) / abs(sample_board_x1 - sample_board_x2)

    if not all((board_x, board_y)):
        return 0, 0, 0, 0

    return piece_x, piece_y, board_x, board_y


def main():
    while True:
        pull_screenshot()
        im = Image.open("./1.png")
        # 擷取棋子和 board 的位置
        piece_x, piece_y, board_x, board_y = find_piece_and_board(im)
        ts = int(time.time())
        print(ts, piece_x, piece_y, board_x, board_y)
        set_button_position(im)
        jump(math.sqrt((board_x - piece_x) ** 2 + (board_y - piece_y) ** 2))
        save_debug_creenshot(ts, im, piece_x, piece_y, board_x, board_y)
        backup_screenshot(ts)
        time.sleep(random.uniform(1, 1.1))   # 為了保證截圖的時候應落穩了,多延遲一會兒


if __name__ == '__main__':
    main()           

複制