python move mouse smoothly at constant speed

import pyautogui, math
from random import randint

def moveTo(x,y,speed=1100 , rand_x=0, rand_y=0, rand_time=0):
    """speed --> Pixels/sec 
    Check current cursor position and move relative to the distance to x,y input"""
    x_now, y_now = pyautogui.position()
    length = math.sqrt(pow(abs(x - x_now),2) + pow(abs(y - y_now),2))
    speed =  speed *0.6 if length < 500 else speed  # move slower when the cursor is close
    speed = 100 if speed <100 else speed    # limit minimum speed
    time = length  / speed + randint(-rand_time,rand_time)/1000
    print("distance: {}px  &  time: {}s".format(int(length), round(time,3) ) )
    pyautogui.moveTo(x+ randint(-rand_x,rand_x), 
                     y+ randint(-rand_y,rand_y), 
                     time, tween=pyautogui.easeOutQuad)

if __name__ == "__main__":
    moveTo(50,50)

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source