<pre>#euraxluo 5.15
#obj_1
#跳一跳的外掛
from PIL import Image
import subprocess
import time
import random
import os
def jump(distance):
press_time = distance*2.353
press_time = max(press_time,200)
press_time = int(press_time)
point = (random.randint(500,600),random.randint(1000,1200))
cmd = 'adb shell input swipe {x1} {y1} {x2} {y2} {time}'.format(
x1=point[0],
y1=point[1],
x2=point[0]+random.randint(1,5),
y2=point[1]+random.randint(1,5),
time = press_time
)
os.system(cmd)
return press_time
def get_screenshot():
#jietu,baocun jump.png
#呼叫os模組
process=subprocess.Popen('adb shell screencap -p',shell=True,stdout=subprocess.PIPE)
screenshot = process.stdout.read()#讀取圖片資訊
screenshot = screenshot.replace(b'\r\n',b'\n')#去除干擾資訊
with open('jump.png','wb') as f:#儲存為圖片
f.write(screenshot)
def find_a_and_b(img_path):#尋找起點和終點
img = Image.open(img_path)
w,h = img.size #圖片尺寸
img_pixel = img.load() #圖片的畫素矩陣
start_p = None
for i in range(int(h/5),int(h*2/3),50): #以50為步長掃描
first_pixel = img_pixel[0,i]
for j in range(1,w):#迴圈查詢,如果遇到非純色。為棋盤
pixel = img_pixel[j,i]
if pixel[0] != first_pixel[0] or pixel[1] !=first_pixel[1] or pixel[2] !=first_pixel[2]:#確定棋盤
start_p = i - 50
break
if start_p:
break
#找到棋子
left = 0
right = 0
a_point_y_max = 0
bz = True
for i in range(start_p,int(h*2/3)):#限制棋子的掃描範圍
for j in range(int(w/9),int(w*8/9),10):#限制棋子的掃描範圍 去掉邊界1/9
pixel = img_pixel[j,i]
if (50<pixel[0]<60)and(53<pixel[1]<63)and(95<pixel[2]<110):
print(pixel[0],pixel[1],pixel[2])
if bz:
left = j
bz = False
right = j
a_point_y_max = max(i,a_point_y_max)#a_point 的縱座標
a_point_x = (left + right) // 2 #a_point 的橫座標
a_point_y = a_point_y_max - 10 #根據不同分辨路確定
#找棋盤
#限制棋盤的掃描範圍
if a_point_x < w/2: #棋子在左邊
b_start_x = a_point_x + 50
b_end_x = 690
else:
b_start_x = 30
b_end_x = a_point_x - 50
#找棋盤的頂點
for i in range(start_p,start_p+200):#y軸大致位置迴圈至下200px
first_pixel = img_pixel[0,i]#背景色的rgb
for j in range(b_start_x,b_end_x,5):#棋子的邊界座標迴圈至邊界
pixel = img_pixel[j,i]
if abs(pixel[0] - first_pixel[0])+abs(pixel[1] - first_pixel[1])+abs(pixel[2] - first_pixel[2])>15:
if bz:
left = j
right = j
bz = False
else:
right = j
if not bz:
break
b_x = (720-(right+left)//2)
b_top = img_pixel[b_x,i+30]
#從定點往下找
for k in range(i+250,i,-1):
pixel = img_pixel[b_x,k]
if abs(pixel[0] - first_pixel[0]) + abs(pixel[1] - first_pixel[1]) + abs(pixel[2] - first_pixel[2]) < 12:
break
b_y=(i+k)//2+80
return (a_point_x,a_point_y),(b_x+20,b_y)
def run():
oper = input('請連線手機,確定開始?y/n')
if oper != 'y':
exit('結束')
while True:
# screenshout
get_screenshot() # 獲取截圖
a,b=find_a_and_b('jump.png')
distance = ((a[0]-b[0])**2+(a[1]-b[1])**2)**0.5#計算距離
jump(distance)#按壓
time.sleep(random.randrange(1,2))#隨機休眠
if __name__ == '__main__':
run()
# find_a_and_b('jump.png')</pre>