Best Python code snippet using fMBT_python
ai_input.py
Source:ai_input.py
1from time import sleep2import ctypes34# controls5# left mouse button down: stop shooting (should it be given this option? can be useful for killing green squares)6# left mouse button up: resume shooting7# W: move player up8# A: move player left9# S: move player down10# D: move player right11# space: bomb1213# dont allow inputs outside of the PLAYING game state14# dont allow for the AI to pause the game (ESC key)1516# instead of clicking in the bounds of the window:17 # get the x and y coordinate of where the mouse cursor is in-game18 # using the target mouse control scheme there should exist a location for the HUD19 # use these coordinates as a more accurate representation of mouse position in-game20 # because the position will be relative to the game, rather than the window2122 # problem: still have to move the mouse to coordinates in screen space2324 # solution:25 # instead of using win32gui screen coordinates,26 # write to program memory the new coordinates to shoot to27 # possible problem of the location constantly reseting to mouse position2829# useful reference for properly using ctypes for this application:30# https://stackoverflow.com/questions/11906925/python-simulate-keydown3132# macro ctypes data types for readability33LONG = ctypes.c_long34DWORD = ctypes.c_ulong35ULONG_PTR = ctypes.POINTER(DWORD)36WORD = ctypes.c_ushort37UINT = ctypes.c_uint3839# tagMOUSEINPUT40# https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-tagmouseinput41class MOUSEINPUT(ctypes.Structure):42 # tuple containing the structure data43 _fields_ = [('dx', LONG), # x pos of the mouse44 ('dy', LONG), # y pos of the mouse45 ('mouseData', DWORD),46 ('flags', DWORD),47 ('time', DWORD),48 ('dwExtraInfo', ULONG_PTR)]4950# tagKEYBDINPUT51# https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-tagkeybdinput52class KEYBDINPUT(ctypes.Structure):53 _fields_ = [('wVk', WORD),54 ('wScan', WORD),55 ('flags', DWORD), # flags include KEYEVENTF_KEYUP (key being released), no flags specified: the key is being pressed56 ("time", DWORD), # timestamp for the event, if 0: the system will provide its own timestamp57 ("dwExtraInfo", ULONG_PTR)]5859# tagHARDWAREINPUT60# https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-taghardwareinput61 # not directly used, but needed for INPUTu62class HARDWAREINPUT(ctypes.Structure):63 _fields_ = [('uMsg', DWORD),64 ('wParamL', WORD),65 ('wParamH', WORD)]6667# tagInput68# https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-taginput69 # union data type for INPUT70class INPUTu(ctypes.Union):71 _fields_ = [('mi', MOUSEINPUT),72 ('ki', KEYBDINPUT),73 ('hi', HARDWAREINPUT)]7475class INPUT(ctypes.Structure):76 _fields_ = [('type', DWORD),77 ('union', INPUTu)]7879# https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendinput80 # returns UINT representing the number of events that were successfully inserted into the KB or mouse input stream81 # argument cInputs obtained by taking the length of the inputs list82 # pInputs is the input list83 # cbSize is the size (in bytes) of the list84def SendInputs(*inputs): # * operator unpacks a list or tuple, allows sending all items in the tuple without specifying the length85 cInputs = len(inputs)86 LPINPUT = INPUT * cInputs87 pInputs = LPINPUT(*inputs) # cast as LPINPUT88 cbSize = ctypes.c_int(ctypes.sizeof(INPUT))89 return ctypes.windll.user32.SendInput(cInputs, pInputs, cbSize) # run the c function with parameters and return the result (UINT)9091# hex values for keys, need to use the DirectInput scan codes here:92# http://www.gamespp.com/directx/directInputKeyboardScanCodes.html93DIR_W = 0x1194DIR_A = 0x1E95DIR_S = 0x1F96DIR_D = 0x2097DIR_UP = 0xC898DIR_LEFT = 0xCB99DIR_RIGHT = 0xCD100DIR_DOWN = 0xD0101DIR_SPACE = 0x39102DIR_ESC = 0x01103DIR_RETURN = 0x1C104105# defines for input type106mi = ctypes.c_ulong(0)107ki = ctypes.c_ulong(1)108# hi = ctypes.c_ulong(2) # unused109110KEYEVENTF_SCANCODE = 0x0008 # use hardware scan code for the key to simulate key press, instead of virtual-key-code111# returns an INPUT constructed from a KEYBDINPUT defining an input with the given key code and that it is being pressed112def createKeyPress(keyCode):113 # create KEYBDINPUT114 key_in = INPUTu()115 key_in.ki = KEYBDINPUT(0, keyCode, KEYEVENTF_SCANCODE, 0, None) # 0 argument for virtual-key-code and timestamp, none for extra-info116 # give new KEYBDINPUT to an INPUT instance and return117 return INPUT(ki, key_in)118119KEYEVENTF_KEYUP = 0x0002120# returns an INPUT constructed from a KEYBDINPUT KEYBDINPUT defining an input with the given key code and that it is being released121def createKeyRelease(keyCode):122 # create KEYBDINPUT123 key_in = INPUTu()124 key_in.ki = KEYBDINPUT(0, keyCode, KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP, 0, None) # 0 argument for virtual-key-code and timestamp, none for extra-info125 # give new KEYBDINPUT to an INPUT instance and return126 return INPUT(ki, key_in)127128# duration that a key should be held down for129key_down_dur = 0.5130menu_dur = 0.2131132133#def stopShooting():134135136#def resumeShooting():137138def release_movement_keys():139 SendInputs(createKeyRelease(DIR_W))140 SendInputs(createKeyRelease(DIR_A))141 SendInputs(createKeyRelease(DIR_S))142 SendInputs(createKeyRelease(DIR_D))143144def release_aim_keys():145 SendInputs(createKeyRelease(DIR_UP))146 SendInputs(createKeyRelease(DIR_LEFT))147 SendInputs(createKeyRelease(DIR_DOWN))148 SendInputs(createKeyRelease(DIR_RIGHT))149150151def moveUp():152 SendInputs(createKeyPress(DIR_W))153154155def moveLeft():156 SendInputs(createKeyPress(DIR_A))157158159def moveDown():160 SendInputs(createKeyPress(DIR_S))161162163def moveRight():164 SendInputs(createKeyPress(DIR_D))165166167def arrowUp():168 SendInputs(createKeyPress(DIR_UP))169170171def arrowLeft():172 SendInputs(createKeyPress(DIR_LEFT))173174175def arrowDown():176 SendInputs(createKeyPress(DIR_DOWN))177178179def arrowRight():180 SendInputs(createKeyPress(DIR_RIGHT))181182183def pause():184 SendInputs(createKeyPress(DIR_ESC))185 sleep(key_down_dur - 0.3)186 SendInputs(createKeyRelease(DIR_ESC))187188def enter():189 SendInputs(createKeyPress(DIR_RETURN))190 sleep(key_down_dur)191 SendInputs(createKeyRelease(DIR_RETURN))192193def resetGame():194 SendInputs(createKeyPress(DIR_ESC))195 sleep(menu_dur)196 SendInputs(createKeyRelease(DIR_ESC))197 sleep(menu_dur)198 SendInputs(createKeyPress(DIR_DOWN))199 sleep(menu_dur)200 SendInputs(createKeyRelease(DIR_DOWN))201 sleep(menu_dur)202 SendInputs(createKeyPress(DIR_DOWN))203 sleep(menu_dur)204 SendInputs(createKeyRelease(DIR_DOWN))205 sleep(menu_dur)206 SendInputs(createKeyPress(DIR_DOWN))207 sleep(menu_dur)208 SendInputs(createKeyRelease(DIR_DOWN))209 sleep(menu_dur)210 SendInputs(createKeyPress(DIR_RETURN))211 sleep(menu_dur)212 SendInputs(createKeyRelease(DIR_RETURN))213 sleep(menu_dur)214 SendInputs(createKeyPress(DIR_DOWN))215 sleep(menu_dur)216 SendInputs(createKeyRelease(DIR_DOWN))217 sleep(menu_dur)218 SendInputs(createKeyPress(DIR_RETURN))219 sleep(menu_dur)220 SendInputs(createKeyRelease(DIR_RETURN))221 sleep(menu_dur)222 SendInputs(createKeyPress(DIR_RETURN))223 sleep(menu_dur)224 SendInputs(createKeyRelease(DIR_RETURN))225226227228229230#def setMousePos(PosX, PosY):231232233#def useBomb():
...
control.py
Source:control.py
1import ctypes2import time3SendInput = ctypes.windll.user32.SendInput4W = 0x115A = 0x1E6S = 0x1F7D = 0x208Q = 0x109E = 0x1210UP = 0xC811LEFT = 0xCB12RIGHT = 0xCD13DOWN = 0xD014ENTER = 0x1C15ESC = 0x0116TWO = 0x0317# C struct redefinitions18PUL = ctypes.POINTER(ctypes.c_ulong)19class KeyBdInput(ctypes.Structure):20 _fields_ = [("wVk", ctypes.c_ushort),21 ("wScan", ctypes.c_ushort),22 ("dwFlags", ctypes.c_ulong),23 ("time", ctypes.c_ulong),24 ("dwExtraInfo", PUL)]25class HardwareInput(ctypes.Structure):26 _fields_ = [("uMsg", ctypes.c_ulong),27 ("wParamL", ctypes.c_short),28 ("wParamH", ctypes.c_ushort)]29class MouseInput(ctypes.Structure):30 _fields_ = [("dx", ctypes.c_long),31 ("dy", ctypes.c_long),32 ("mouseData", ctypes.c_ulong),33 ("dwFlags", ctypes.c_ulong),34 ("time", ctypes.c_ulong),35 ("dwExtraInfo", PUL)]36class Input_I(ctypes.Union):37 _fields_ = [("ki", KeyBdInput),38 ("mi", MouseInput),39 ("hi", HardwareInput)]40class Input(ctypes.Structure):41 _fields_ = [("type", ctypes.c_ulong),42 ("ii", Input_I)]43# Actuals Functions44def PressKey(hexKeyCode):45 extra = ctypes.c_ulong(0)46 ii_ = Input_I()47 ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra))48 x = Input(ctypes.c_ulong(1), ii_)49 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))50def ReleaseKey(hexKeyCode):51 extra = ctypes.c_ulong(0)52 ii_ = Input_I()53 ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008 | 0x0002, 0,54 ctypes.pointer(extra))55 x = Input(ctypes.c_ulong(1), ii_)56 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))57def shiftUp():58 extra = ctypes.c_ulong(0)59 ii_ = Input_I()60 ii_.ki = KeyBdInput(0, 0x12, 0x0008, 0, ctypes.pointer(extra))61 x = Input(ctypes.c_ulong(1), ii_)62 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))63def noall():64 noForward()65 noLeft()66 noRight()67 noBrake()68def upRelease():69 extra = ctypes.c_ulong(0)70 ii_ = Input_I()71 ii_.ki = KeyBdInput(0, 0x12, 0x0008 | 0x0002, 0,72 ctypes.pointer(extra))73 x = Input(ctypes.c_ulong(1), ii_)74 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))75def shiftDown():76 extra = ctypes.c_ulong(0)77 ii_ = Input_I()78 ii_.ki = KeyBdInput(0, 0x10, 0x0008, 0, ctypes.pointer(extra))79 x = Input(ctypes.c_ulong(1), ii_)80 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))81 downRelease()82def downRelease():83 extra = ctypes.c_ulong(0)84 ii_ = Input_I()85 ii_.ki = KeyBdInput(0, 0x10, 0x0008 | 0x0002, 0,86 ctypes.pointer(extra))87 x = Input(ctypes.c_ulong(1), ii_)88 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))89def goForward():90 extra = ctypes.c_ulong(0)91 ii_ = Input_I()92 ii_.ki = KeyBdInput(0, 0x11, 0x0008, 0, ctypes.pointer(extra))93 x = Input(ctypes.c_ulong(1), ii_)94 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))95def noForward():96 extra = ctypes.c_ulong(0)97 ii_ = Input_I()98 ii_.ki = KeyBdInput(0, 0x11, 0x0008 | 0x0002, 0,99 ctypes.pointer(extra))100 x = Input(ctypes.c_ulong(1), ii_)101 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))102def goRight():103 extra = ctypes.c_ulong(0)104 ii_ = Input_I()105 ii_.ki = KeyBdInput(0, 0x20, 0x0008, 0, ctypes.pointer(extra))106 x = Input(ctypes.c_ulong(1), ii_)107 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))108def noRight():109 extra = ctypes.c_ulong(0)110 ii_ = Input_I()111 ii_.ki = KeyBdInput(0, 0x20, 0x0008 | 0x0002, 0,112 ctypes.pointer(extra))113 x = Input(ctypes.c_ulong(1), ii_)114 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))115def goLeft():116 extra = ctypes.c_ulong(0)117 ii_ = Input_I()118 ii_.ki = KeyBdInput(0, 0x1E, 0x0008, 0, ctypes.pointer(extra))119 x = Input(ctypes.c_ulong(1), ii_)120 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))121def noLeft():122 extra = ctypes.c_ulong(0)123 ii_ = Input_I()124 ii_.ki = KeyBdInput(0, 0x1E, 0x0008 | 0x0002, 0,125 ctypes.pointer(extra))126 x = Input(ctypes.c_ulong(1), ii_)127 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))128def goBrake():129 extra = ctypes.c_ulong(0)130 ii_ = Input_I()131 ii_.ki = KeyBdInput(0, 0x1E, 0x0008, 0, ctypes.pointer(extra))132 x = Input(ctypes.c_ulong(1), ii_)133 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))134def noBrake():135 extra = ctypes.c_ulong(0)136 ii_ = Input_I()137 ii_.ki = KeyBdInput(0, 0x1E, 0x0008 | 0x0002, 0,138 ctypes.pointer(extra))139 x = Input(ctypes.c_ulong(1), ii_)140 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))141# directx scan codes142# http://www.gamespp.com/directx/directInputKeyboardScanCodes.html143if __name__ == '__main__':144 while (True):145 PressKey(0x11)146 time.sleep(1)147 ReleaseKey(0x11)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!