Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. def move_slow(x, y, t, point_delta = 1):
  2. x1, y1, x2, y2 = 0, 0, int(x), int(y)
  3.  
  4. points = []
  5.  
  6. # Check if linear movement is steep
  7. is_steep = abs(y2 - y1) > abs(x2 - x1)
  8.  
  9. # If the linear movement is steep, reverse the values.
  10. if is_steep:
  11. x1, y1 = y1, x1
  12. x2, y2 = y2, x2
  13.  
  14. reverse = False
  15. if x1 > x2:
  16. x1, x2 = x2, x1
  17. y1, y2 = y2, y1
  18. reverse = True
  19.  
  20. # Get the delta (differernce) between each axial value
  21. delta_x = x2 - x1
  22. delta_y = abs(y2 - y1)
  23.  
  24. # Get margin for error
  25. error = int(delta_x / 2)
  26.  
  27. y = y1
  28. y_step = None
  29.  
  30. if y1 < y2:
  31. y_step = 1
  32. else:
  33. y_step = -1
  34.  
  35. # Calculate each point from 0,0 to x2, y2
  36. for x in range(x1, x2 + 1):
  37. if is_steep:
  38. points.append((y, x))
  39. else:
  40. points.append((x, y))
  41.  
  42. error -= delta_y
  43.  
  44. if error < 0:
  45. y += y_step
  46. error += delta_x
  47.  
  48. # Reverse the points if the values were reversed at the start of the function.
  49. if reverse:
  50. points.reverse()
  51.  
  52. # Add 0,0 to start of the list so no errors occur when using the generated points
  53. points.insert(0, (0, 0))
  54.  
  55. # Convert points to a more compact array generating less movements:
  56. new_points = points[0::point_delta]
  57. new_points[-1] = points[-1]
  58.  
  59. # Get a ratio for the time value,
  60. # Allowing us to loop through and move the mouse within that time.
  61. ratio_t = float(len(new_points)) / float(t)
  62. idx, count = 1, 0.0
  63.  
  64. for i in range(t):
  65. # Sleep for 1ms each iteration.
  66. ctypes.windll.kernel32.SleepEx(1, False)
  67.  
  68. count += ratio_t
  69.  
  70. # Move mouse while our count value is greater than 1, keeping us on track.
  71. while count >= 1.0:
  72. if idx == len(new_points): break
  73.  
  74. # Get the difference between the last values and current values.
  75. delta_x = new_points[idx][0] - new_points[idx - 1][0]
  76. delta_y = new_points[idx][1] - new_points[idx - 1][1]
  77.  
  78. idx += 1
  79.  
  80. # Move the mouse relative to current position based on the delta.
  81. Mouse.move_rel(delta_x, delta_y)
  82. count -= 1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement