code breakdown
The script uses numpy and pyautogui, and works on Windows, mac, and linux. The program is very simple, and it is endlessly fun to watch! The above screenshot shows the whole file. The lines beginning with a '#' denote the functionality basics, but a more in-depth breakdown is provided below.

The first two lines import necessary libraries. I set abbreviations for the libraries so that their full name does not need to be typed later on. Depending on how and where you are using python, an error may appear when this line is run. To fix the error, make sure both libraries are installed.

This line helps provide an easy escape to the program. If at any point you wish to stop the script from running, move your cursor to the top left corner of your screen. This will trigger the failsafe and end the program. Of course, you can press 'control' + 'C' on your keyboard to stop it as well.

The 'pyautogui.size' command determines the pixel dimensions of your primary monitor. The x and y pixel dimensions of your screen are saved in the variables 'x' and 'y'. No matter your screen size, the program will move the mouse appropriately.

This line sets up a 'while' loop for the program to cycle through. It will keep repeating itself as long as the above failsafe has not been triggered.
These two lines fall within the above 'while' loop. They use the numpy library to determine random integers. These integers must fall within the determined screen size. Basically, it picks a random pixel location on your screen.
Finally, the fun part! This command moves your cursor to the random pixel location we just determined. Over the course of three seconds, it dynamically moves the mouse into position. The mouse movement speed is not constant either, so that it more accurately mimics a human.
The last line simply puts the program on pause for five seconds. I included it to give the script a little rest between movements. Of course, This delay can be deleted or changed as needed.
That's it! Not much to it, but still a very fun program to make and run. Feel free to contact me with any questions or help needed. I hope you enjoy it! 

Check out these awesome projects too!

Back to Top