2
這是我的Python 2.7.13
腳本,基本上是所謂的視頻遊戲的「一鍵宏」。基於從遊戲屏幕的一部分獲取的信息,它按下正確的按鍵組合而不是玩家。所以玩家發送了f
密鑰,腳本按下其他鍵f
。Python的一鍵宏遊戲
它正在工作,但在隨機時間(開始後1-5分鐘)腳本停止或類似的事情。我可以在Windows任務管理器中看到腳本正在運行,但是當我按下f
鍵時沒有任何反應。
在開始時,我寫了一些更多未優化的代碼,它多次使用屏幕截圖/按鍵。當時腳本「凍結」更多。
這可能是因爲太多的屏幕截圖嗎?還是我在某個地方搞砸了?
import pyautogui, pythoncom, pyHook
# Determine if an ability is ready or not
def ready(x,y, im):
if (im.getpixel((x,y)) != (0,0,0)):
return True
else:
return False
def ability1(im):
return (ready(17, 16, im) or ready(35, 16, im))
def ability2(im):
return ready(134, 9, im)
# Listen for keypress
def OnKeyboardEvent(event):
im = pyautogui.screenshot(region=(249, 770, 194, 26))
if (event.KeyID == 70): # Pressed the "f" key
if (ability1(im)):
pyautogui.hotkey('shift','4')
return True
if (ability2(im)):
pyautogui.press('-')
return True
pyautogui.press('1')
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()