python 视频游戏机器人无法正确使用键盘

q5lcpyga  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(196)

我一直在尝试为英雄联盟制作一个机器人,使用Python3和pyautogui。我可以使用pywin32使机器人左键单击和右键单击,但是使用pyautogui的按键无法正常工作。pyautogui能够在点击文本框时输入,但无法使用其他能力。具体来说,level_up函数不起作用。以下是我的代码:

  1. from pyautogui import *
  2. import pyautogui
  3. import time
  4. import keyboard
  5. import numpy as np
  6. import random
  7. import win32api, win32con
  8. def click_right(x,y):#works fine
  9. win32api.SetCursorPos((x,y))
  10. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
  11. time.sleep(.01)
  12. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)
  13. def click_left(x,y):#works fine
  14. win32api.SetCursorPos((x,y))
  15. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
  16. time.sleep(.1)
  17. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
  18. def type(word):
  19. for char in word:
  20. pyautogui.keyDown(char)
  21. time.sleep(.05)
  22. pyautogui.keyUp(char)
  23. def buy_item(item):#item is a string, works fine
  24. click_left(1209,998)#open shop
  25. time.sleep(.2)
  26. click_left(313,281)#click to search for item
  27. time.sleep(.2)
  28. type(item)#type item name
  29. time.sleep(.1)
  30. click_right(318,354)#buy item
  31. time.sleep(.1)
  32. click_left(1170,217)#close shop
  33. def buy_items(items):#items is a list of strings, works fine
  34. click_left(1209, 998) # open shop
  35. time.sleep(.2)
  36. for item in items:
  37. click_left(313, 281) # search item
  38. time.sleep(.2)
  39. type(item)
  40. time.sleep(.1)
  41. click_right(318, 354)
  42. time.sleep(.1)
  43. click_left(1170, 217)
  44. #doesn't work
  45. def level_up(ability):#ability is a char in 'qwer'
  46. with pyautogui.hold('ctrl'):
  47. pyautogui.press(['q'])
  48. def main():
  49. time.sleep(3)
  50. buy_items(['dagger', 'zeal', 'cloth'])
  51. time.sleep(1)
  52. level_up('q')
  53. #
  54. if __name__ == '__main__':
  55. main()

字符串
有没有一种方法可以正确地做到这一点?有没有一个比pyautogui更好的软件包可以使用?

xcitsw88

xcitsw881#

我从pyautogui切换到pydirectinput,它现在工作。

  1. def level_up(ability):#ability is a char in 'qwer'
  2. pydirectinput.keyDown('ctrl')
  3. pydirectinput.keyDown(ability)
  4. time.sleep(.1)
  5. pydirectinput.keyUp('ctrl')
  6. pydirectinput.keyUp(ability)

字符串

相关问题