├── Game links.txt ├── requirements.txt ├── README.md ├── New.py └── .gitignore /Game links.txt: -------------------------------------------------------------------------------- 1 | https://m.plonga.com/adventure/Temple-Run-2-Online-Tablet 2 | https://m.plonga.com/adventure/Subway-Surfers-Online-Game -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2020.12.5 2 | MouseInfo==0.1.3 3 | numpy==1.19.5 4 | opencv-contrib-python==3.4.5.20 5 | opencv-python==4.5.1.48 6 | Pillow==8.1.0 7 | PyAutoGUI==0.9.52 8 | PyGetWindow==0.0.9 9 | PyMsgBox==1.0.9 10 | pyperclip==1.8.2 11 | PyRect==0.1.4 12 | PyScreeze==0.1.26 13 | PyTweening==1.0.3 14 | wincertstore==0.2 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Motion-Control-Game 2 | 3 | #### A few months ago I saw something similar to this on Linkedin that inspired me to work on the project. This project can be used to play any game in your PC or conrol any application the way you like by selecting the tracking object and then moving it around the boxes on the screen 4 | 5 | ![Screenshot (18)](https://user-images.githubusercontent.com/59216663/114544687-95e8c280-9c78-11eb-9e29-e43f64c2e1a9.png) 6 | 7 | 8 | ## Getting Started 9 | 10 | #### In order to start with the Motion Control Game just clone this repository. Open the main file in your Python IDE, create a virtual environment with Python version 3.6 and replace the names of each directory with your pc directories respectively . 11 | 12 | #### Now install the packages provided in the requirements.txt by running pip install -r requirements.txt to install them all. 13 | 14 | ## How does it work? 15 | 16 | #### Open the game with the link provided in the .txt file. Run the code in your IDE. A picture will be clicked. Keep an object you wish to track in the frame. Now select an area of the object. Start the game now you are ready to go. 17 | 18 | ## Working Principal 19 | 20 | #### Basically the idea is based on tracking down movements in real time and implementing a keypress as an output. The project is made using opencv and pyautogui as the main modules.Just after running the python script a picture will be clicked from the webcam.Then we mark down or select the portion of the object or the person in the frame that we wish to track.After clicking enter,a screen appears that is divide into different parts or lines and a blue circle pointing on the object that was selected for tracking the object where the middle portion resembles the nuetral or null. Now, whenever our tracking object moves towards any direction the key of that direction is pressed using PYAUTOGUI. 21 | 22 | ## Contribution 23 | 24 | #### Different laptops provide different fps and have different image quality as well. There are different trackers provided in the main code as well. Use them and experiment and see which one works better for you. Please feel free to contribute to the project and Pull Requests are open for everyone willing to improve upon the project. 25 | -------------------------------------------------------------------------------- /New.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import pyautogui 3 | import time 4 | count=0 5 | count1=0 6 | cap=cv2.VideoCapture(0) 7 | tracker= cv2.TrackerCSRT_create() #suitable for smaller objects 8 | #tracker=cv2.TrackerKCF_create() #better for bigger objects (recommended) 9 | #tracker=cv2.TrackerMOSSE_create() ##not good 10 | #tracker=cv2.TrackerGOTURN_create() 11 | #CSRT_create() 12 | success,img= cap.read() 13 | img = cv2.flip(img, 1) 14 | bbox= cv2.selectROI("tracking",img,False) 15 | tracker.init(img,bbox) 16 | def drawbox(img,bbox): 17 | x,y,w,h= int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]) 18 | global count,count1 19 | if(x>285 and y>160 and x<355 and y <320): 20 | count=0 21 | if(y>205 and y<275 and x>300 and x<340): 22 | count1=0 23 | if(count1<1): 24 | if(x>0 and y<205): 25 | print("up") 26 | pyautogui.keyDown('up') 27 | pyautogui.keyUp('up') 28 | count1+=1 29 | elif(x>0 and y>275): 30 | print("down") 31 | pyautogui.keyDown('down') 32 | pyautogui.keyUp('down') 33 | count1+=1 34 | if(count<1): 35 | if(x>355 and y>160): 36 | print("right") 37 | pyautogui.keyDown('right') 38 | pyautogui.keyUp('right') 39 | count+=1 40 | elif(x<285 and y>160): 41 | print("left") 42 | pyautogui.keyDown('left') 43 | pyautogui.keyUp('left') 44 | count+=1 45 | time.sleep(0.001) 46 | 47 | 48 | cv2.circle(img,(x,y), 10,(255, 255, 0),3) 49 | 50 | while True: 51 | timer= cv2.getTickCount() 52 | success,img= cap.read() 53 | img = cv2.flip(img, 1) 54 | cv2.line(img,(0,205),(640,205),(0,224,19),2) #horizontal up 55 | cv2.line(img,(0,275),(640,275),(0,224,19),2) #horizontal down 56 | cv2.line(img,(285,0),(285,480),(0,224,19),2) #vertical left 57 | cv2.line(img,(355,0),(355,480),(0,224,19),2) #vertical right 58 | success,bbox= tracker.update(img) 59 | if success: 60 | drawbox(img,bbox) 61 | else: 62 | cv2.putText(img,"lost",(75,50),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,223,0),2) 63 | fps=cv2.getTickFrequency()/(cv2.getTickCount()-timer) 64 | cv2.putText(img,str(int(fps)),(7,50),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,223,0),2) 65 | cv2.imshow("video",img) 66 | if cv2.waitKey(1) == ord('q'): 67 | break -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | --------------------------------------------------------------------------------