天天看點

跳一跳輔助程式(拿來主義)

感謝部落客提供了手動跳。

github大佬們提供了自動跳

手動跳很累,而且有時候會掉下來。還是自動跳好。

首先你需要一個python開發環境,需要numpy PIL matplotlib這三個包的支援。如果沒有pip,可以到這裡手動下載下傳

然後是一個adb,這個是android開發包的一個程式,可以到這裡下載下傳,(需要免費的可以給我留言,或者自己下載下傳SDK)

然後就是代碼了。直接複制運作。我慢慢會補充代碼的說明(畢竟伸手黨懶)

跳一跳輔助程式(拿來主義)
跳一跳輔助程式(拿來主義)

#coding:utf-8
import os
import PIL,numpy
import  matplotlib.pylab as plt
from matplotlib.animation import FuncAnimation
import time
need_update = True
press_coefficient=2.099
piece_base_height_1_2=13
piece_body_width=47

def get_screen_image():
    os.system('adb shell screencap -p /sdcard/screen.png')#擷取目前界面的手機截圖
    os.system('adb pull /sdcard/screen.png')#下載下傳目前截圖到電腦目前檔案夾
    return numpy.array(PIL.Image.open('screen.png')) #打開目前檔案下的圖檔

def jump_to_next(point1,point2):#計算弦的長度
    x1, y1 = point1; x2, y2 = point2
    distance = ((x2-x1)**2 + (y2-y1)**2)**0.5
    x1=int(x1)
    y1=int(y1)
    x2=int(x2)
    y2=int(y2)
    os.system('adb shell input swipe {} {} {} {} {}'.format(x1,y1,x2,y2,int(distance*2)))
    #手機的單擊位置,0 0 0 0 0 ,x軸y軸滑動後的xy軸單擊時間


def on_calck(event, coor=[]):#綁定滑鼠的單擊事件[(x,y)(x2,y2)],
    global need_update
    coor.append((event.xdata, event.ydata))
    if len(coor) == 2:
        jump_to_next(coor.pop(), coor.pop())
    need_update = True

def update_screen(frame):#更新圖檔/從畫圖檔
    global need_update
    if need_update:
        time.sleep(1)
        axes_image.set_array(get_screen_image())
        need_update = False
    return axes_image,
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(int(h / 3), int(h*2 / 3), 50):
        last_pixel = im_pixel[0, i]
        for j in range(1, w):
            pixel = im_pixel[j, i]
            # 不是純色的線,則記錄 scan_start_y 的值,準備跳出循環
            if pixel != last_pixel:
                scan_start_y = i - 50
                break
        if scan_start_y:
            break
    print('scan_start_y: {}'.format(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 = int(piece_x_sum / piece_x_c)
    piece_y = piece_y_max - piece_base_height_1_2  # 上移棋子底盤高度的一半

    # 限制棋盤掃描的橫坐标,避免音符 bug
    if piece_x < w/2:
        board_x_start = piece_x
        board_x_end = w
    else:
        board_x_start = 0
        board_x_end = piece_x

    for i in range(int(h / 3), int(h * 2 / 3)):
        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(int(board_x_start), int(board_x_end)):
            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
    last_pixel = im_pixel[board_x, i]

    # 從上頂點往下 +274 的位置開始向上找顔色與上頂點一樣的點,為下頂點
    # 該方法對所有純色平面和部分非純色平面有效,對高爾夫草坪面、木紋桌面、
    # 藥瓶和非菱形的碟機(好像是)會判斷錯誤
    for k in range(i+274, i, -1):  # 274 取開局時最大的方塊的上下頂點距離
        pixel = im_pixel[board_x, k]
        if abs(pixel[0] - last_pixel[0]) \
                + abs(pixel[1] - last_pixel[1]) \
                + abs(pixel[2] - last_pixel[2]) < 10:
            break
    board_y = int((i+k) / 2)

    # 如果上一跳命中中間,則下個目标中心會出現 r245 g245 b245 的點,利用這個
    # 屬性彌補上一段代碼可能存在的判斷錯誤
    # 若上一跳由于某種原因沒有跳到正中間,而下一跳恰好有無法正确識别花紋,則有
    # 可能遊戲失敗,由于花紋面積通常比較大,失敗機率較低
    for j in range(i, i+200):
        pixel = im_pixel[board_x, j]
        if abs(pixel[0] - 245) + abs(pixel[1] - 245) + abs(pixel[2] - 245) == 0:
            board_y = j + 10
            break

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

def auto_jump(frame):
    x1,y1,x2,y2=find_piece_and_board(PIL.Image.open('screen.png'))
    coor=[]
    global need_update
    coor.append((x1,y1))
    coor.append((x2,y2))
    jump_to_next(coor.pop(), coor.pop())
    need_update = True
    global need_update
    if need_update:
        time.sleep(1)
        axes_image.set_array(get_screen_image())
        need_update = False
    return axes_image,
figure = plt.figure(u'跳一跳輔助') #建立一個空白的圖檔對象、建立一張圖檔
axes_image = plt.imshow(get_screen_image(), animated=True)#把擷取的圖檔畫在坐标軸上

#figure.canvas.mpl_connect('button_press_event', on_calck)
#figure.canvas.mpl_connect('button_press_event', auto_jump)
#ani = FuncAnimation(figure, update_screen, interval=50, blit=True)
ani = FuncAnimation(figure, auto_jump, interval=50, blit=True)
plt.show()      

跳一跳輔助源碼

自動跳有時候也會掉下來,不知道是怎麼回事。github上好像還有一個用圖像識别做的。有時間可以看一下。應該沒有時間了。

繼續閱讀