How to move aim in games by using Python

4.1k views Asked by At

I'm trying to create an AI that plays CS:GO.

But I could not move aim.

I tried pyautogui, win32api, pynput libraries, all of them worked on desktop or anywhere that have an cursor.

But in game there is no cursor and as I followed position of mouse is stay at middle ((1920/2, 1080/2), for me) when move mouse to turn it increases for a short time then backs to that position.

How can I move aim in CS:GO or GTAV or anygame by python. what is the difference between python code and real mouse?

I do not think this is because of anti-cheat because it did not worked on GTAV

I looked same topics but they did not solve my problem

Codes where similar for all libraries so the code that I used is this:

import pyautogui, sys
import _thread
import time

time.sleep(2)

def kaydir(miktarX, miktarY):
    pyautogui.moveRel(miktarX, miktarY)

print('Press Ctrl-C to quit.')
try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
        _thread.start_new_thread(kaydir, (1, 1))
        time.sleep(0.08)
except KeyboardInterrupt:
    print('\n')
1

There are 1 answers

2
Hexception On

So first thing is: What do you mean with that question:

what is the difference between python code and real mouse?

But if you want to create an bot for any game you have 2 options:

  • Memory Hacking: Get some memory pointers to change input flags etc (via CheatEngine etc)

  • Simulating user interaction (your way): If you want to use PyAutoGUI for interaction i think this is a good point to start: https://www.youtube.com/watch?v=NaZTtUmE990

    Another way, I would prefer, is (as you have mentioned) the WinAPI: C++ implementation for mouse moving (since i dont know how to do it in python):

    int main() {
        HWND tWin= FindWindow("TargetWindow", "Target Window");
        if (tWin) {
            RECT rect = {0};
            GetWindowRect(tWin, &rect);
            SetCursorPos(rect.right - 180 /*x offset*/, rect.bottom - 300 /*y offset*/);
        }
    
        return 0;
    }
    

    Also make sure that if your program is 64-bit use the 64-bit WinAPI.

    If your game is running under higher privilegies as you bot you need to run your bot as Administrator or as SYSTEM

    But I think to create a bot you also need memory hacking (just reading) for getting the player position etc, Since how do you extract pos items etc from the gamescreen-imgage from the programmers point of view?. You may also can also use Machine Learning but for a game it needs a huge training process.