python下的自瞄算法鼠标移动算法
前提背景:已经计算出目的坐标,已有移动鼠标的函数,那么如何模拟手动移动呢
(此处我的移动函数为dd_dll.DD_movR)
第一种bezier移动
特点:坐标是随机的,移动轨迹往往不定,但是由于每次移动的到目的坐标移动的点数是固定的,这时候还是容易被判断为机器,也可每次运行时添加步数为随机数
参考python代码:
def sleep_exact(seconds):
start_time = time.perf_counter()
while time.perf_counter() - start_time < seconds:
pass
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def combination(n, k):
if k == 0 or k == n:
return 1.0
elif k == 1:
return float(n)
else:
result = 1.0
for i in range(1, k+1):
result *= float(n - i + 1) / i
return result
def bezier(pts, t):
n = len(pts) - 1
x = 0.0
y = 0.0
for i in range(n+1):
b = combination(n, i) * pow(t, i) * pow(1.0 - t, n - i)
x += b * pts[i].x
y += b * pts[i].y
return Point(int(x), int(y))
def beziermove(dx,dy):
# 设置控制点,可以根据需要自行调整
controlPoints = [
Point(0, 0),
Point(dx // 2, dy),
Point(dx, dy // 2),
Point(dx, dy)
]
# 计算移动轨迹
numPoints = 50 # 移动轨迹上点的数量
path = []
for i in range(numPoints + 1):
t = i / float(numPoints)
p = bezier(controlPoints, t)
path.append(p)
# 执行移动操作
for i in range(1, len(path)):
x = path[i].x - path[i - 1].x
y = path[i].y - path[i - 1].y
dd_dll.DD_movR(x, y)
random_float = random.uniform(0.0015, 0.002)
sleep_exact(random_float)
第二种bresenham算法
特点:每次移动的次数不一样,具体视坐标而定,但移动轨迹是直线,容易被判断机器
参考python代码:
def bresenham(x1, y1):
x0=0
y0=0
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
increment_x = sx
increment_y = sy
err = dx - dy
while x0 != x1 or y0 != y1:
dd_dll.DD_movR(increment_x, increment_y) #鼠标移动
random_float = random.uniform(0.0015, 0.002)
sleep_exact(random_float) # 设置随机0.0015-0.002 random_number = random.randint(start, end)
e2 = 2 * err
if e2 > -dy:
err -= dy
x0 += sx
increment_x = sx
else:
increment_x = 0
if e2 < dx:
err += dx
y0 += sy
increment_y = sy
else:
increment_y = 0