-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhackjump.py
More file actions
96 lines (77 loc) · 2.42 KB
/
hackjump.py
File metadata and controls
96 lines (77 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
__author__ = "bythew3i"
from math import sqrt
import wda
from PIL import Image, ImageDraw
import time
MAGICNUM = 0.0020 # coeffcient = time/physicalDistance
PLAYERCOLOR = (56, 59, 102) # player color reference
PCOLORAPPR = 5 # player color approximation (tryin to find the pixel whose color distance with PLAYERCOLR is less than this value)
BCOLORDIFF = 30 # board color difference (calculate the color distance of a pixel and background color, if greater than this value, we consider this pixel is from board)
BCOLORAPPR = 5 # board color approximation (After finding topmost board pixel, use it to find the rightmost pixel by calling "isCloseColor" function with this value)
def colorDistance(c1, c2):
return sqrt((c1[0]-c2[0])**2+(c1[1]-c2[1])**2+(c1[2]-c2[2])**2)
def phyDistance(x1, y1, x2, y2):
return sqrt((x1-x2)**2 + (y1-y2)**2)
def isCloseColor(c1, c2, ref):
return -ref < c1[0]-c2[0] < ref \
and -ref < c1[1]-c2[1] < ref \
and -ref < c1[2]-c2[2] < ref
def findPlayer(im):
offsetX = 5
offsetY = -10
imw = im.size[0]
imh = im.size[1]
for y in range(imh*2//3, imh//2, -1):
for x in range(imw):
cur = im.getpixel((x, y))
if colorDistance(cur, PLAYERCOLOR) <= PCOLORAPPR:
return x+offsetX, y+offsetY
return None
def findBoard(im, right=True): # only search left half
offsetY = 3
boardColor = None
topX = None
imw = im.size[0]
imh = im.size[1]
xfrm, xto = imw//2, 0
if right:
xfrm, xto = imw-1, imw//2
# find the top and board color
for y in range(int(imh/3), int(imh/2)):
preColor = im.getpixel((xto, y))
for x in range(xfrm, xto, -1):
curColor = im.getpixel((x, y))
if colorDistance(curColor, preColor) > BCOLORDIFF:
# return x, y
topX = x
boardColor = im.getpixel((x, y+offsetY))
break
if boardColor!=None:
break
# find left/right edge
for x in range(imw-1, 0, -1):
for y in range(int(imh/3), int(imh/2)):
curColor = im.getpixel((x, y))
if isCloseColor(curColor, boardColor, BCOLORAPPR):
return topX, y
return None
def getDistance(path):
im = Image.open(path)
# 750 x 1334
tx, ty = findPlayer(im)
bx, by = findBoard(im, tx < im.size[0]//2) ## check the next board location
return phyDistance(tx, ty, bx, by)
def main():
tapX = 50
tapY = 50
path = 'buffer/resource.png'
wait = 2
c = wda.Client()
s = c.session()
while True:
c.screenshot(path)
t = getDistance(path)*MAGICNUM
# print(t)
s.tap_hold(tapX, tapY, t) # tap element
time.sleep(wait)
main()