1 import math
2 import os
3 import tempfile
4 import time
5 from functoole import reduce
6 from PIL import Image
7 BACKGROUND_POS = {40,500}
8 DISTANCE_TO_TIME_RATIO =1.35
9 SCREENSHOT_PATH = tempfile.gettempdir()+"/screenshot.png"
10 def calculate_jump_distance():
11 im =Image.open(SCREENSHOT_PATE)
12 baxkround_rgb = im.getpixel(BACKGROUND_POS)
13 role_pos_list = None
14 vertex1_pos = None
15 block_background_rgb = None
16 vertex2_pos = None
17 role_line_flag = True
18 for y in range(BACKGROUND_POS[1], im.height):
19 if role_pos_list and role_line_flag:
20 break
21 role_line_flag = True
22 vertex2_line_flag = True
23 for x in range(BACKGROUND_POS[0], im.width):
24 current_rgb = im.getpixel((x, y))
25 next_rgb = im.getpixel((x + 1, y)) if x + 1 < im.width else (0, 0, 0)
26 # 识别顶点1
27 if x > BACKGROUND_POS[0] and y > BACKGROUND_POS[1] and not vertex1_pos
28 and not is_similar(background_rgb, current_rgb) and is_similar(current_rgb, next_rgb):
29 vertex1_pos = (x, y)
30 block_background_rgb = current_rgb
31 # 识别顶点2
32 if block_background_rgb and vertex2_line_flag and is_similar(current_rgb, block_background_rgb, 5):
33 vertex2_line_flag = False
34 if vertex2_pos:
35 if x < vertex2_pos[0] and vertex2_pos[0] - x < 20 and y - vertex2_pos[1] < 20:
36 vertex2_pos = (x, y)
37 else:
38 vertex2_pos = (x, y)
39 # 识别小人
40 if is_part_of_role(current_rgb):
41 if role_line_flag:
42 role_pos_list = []
43 role_line_flag = False
44 role_pos_list.append((x, y))
45 if len(role_pos_list) == 0:
46 raise Exception('无法识别小人位置!!!')
47 pos_sum = reduce((lambda o1, o2: (o1[0] + o2[0], o1[1] + o2[1])), role_pos_list)
48 role_pos = (int(pos_sum[0] / len(role_pos_list)), int(pos_sum[1] / len(role_pos_list)))
49 destination_pos = (vertex1_pos[0], vertex2_pos[1])
50 return int(linear_distance(role_pos, destination_pos))
51def is_part_of_role(rgb):
52 return 53 < rgb[0] < 59 and 57 < rgb[1] < 61 and 95 < rgb[2] < 103
53def linear_distance(xy1, xy2):
54 return math.sqrt(pow(xy1[0] - xy2[0], 2) + pow(xy1[1] - xy2[1], 2))
55def is_similar(rgb1, rgb2, degree=10):
56 return abs(rgb1[0] - rgb2[0]) <= degree and abs(rgb1[1] - rgb2[1]) <= degree and abs(rgb1[2] - rgb2[2]) <= degree
57def screenshot():
58 os.system("adb shell screencap -p /mnt/sdcard/screencap.png")
59 os.system("adb pull /mnt/sdcard/screencap.png {} >> {}/jump.out".format(SCREENSHOT_PATH, tempfile.gettempdir()))
60def jump(touch_time):
61 os.system("adb shell input swipe 0 0 0 0 {}".format(touch_time))
62def distance2time(distance):
63 return int(distance * DISTANCE_TO_TIME_RATIO)
64if __name__ == '__main__':
65 count = 1
66 while True:
67 screenshot()
68 distance = calculate_jump_distance()
69 touch_time = distance2time(distance)
70 jump(touch_time)
71 print("#{}: distance={}, time={}".format(count, distance, touch_time))
72 count += 1
73 time.sleep(1)
跳一跳辅助
猜你喜欢
转载自blog.csdn.net/qq_40861561/article/details/80740377
今日推荐
周排行