Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. def move_slow(x, y, t):
  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. # Get a ratio for the time value,
  56. # Allowing us to loop through and move the mouse within that time.
  57. ratio_t = float(len(points)) / float(t)
  58. idx, count = 1, 0.0
  59.  
  60. for i in range(t):
  61. # Sleep for 1ms each iteration.
  62. ctypes.windll.kernel32.SleepEx(1, False)
  63.  
  64. count += ratio_t
  65.  
  66. # Move mouse while our count value is greater than 1, keeping us on track.
  67. while count >= 1.0:
  68. if idx == len(points): break
  69.  
  70. # Get the difference between the last values and current values.
  71. delta_x = points[idx][0] - points[idx - 1][0]
  72. delta_y = points[idx][1] - points[idx - 1][1]
  73.  
  74. idx += 1
  75.  
  76. # Move the mouse relative to current position based on the delta.
  77. Mouse.move_rel(delta_x, delta_y)
  78. count -= 1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement