x=0while x==0: target = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\ bot\references\target.png'),region=(0,0,1024,768),confidence=.7) time.sleep(0.5) target2 = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\ bot\references\target2.png'),region=(0,0,1024,768),confidence=.7) print(target,target2) if target and target2 is None: pyautogui.keyDown('W') elif target or target2 != None: pyautogui.keyUp("W") print(target or target2) target_point = pyautogui.center(target or target2) targetx, targety = target_point pyautogui.click(targetx, targety) x=1(代碼應該用導入的模塊重新創建)大家好!我試圖為游戲創建一個簡單的機器人,它在未檢測到目標時向前移動,但在檢測到目標時停止移動。為什么這不能讓 W 鍵被按下?奇怪的是,當檢測到 target 或 target2.png 時,它會按 W,否則不會?
1 回答

墨色風雨
TA貢獻1853條經驗 獲得超6個贊
這里的問題是 python 將某些值視為 True,而將其他值視為 False。
在 Python 中,None
, 0
, 和""
(空字符串) 都被認為是 False。任何其他值都被視為 True。
在您的代碼中,有這一行:
if target and target2 is None:
雖然這句話聽起來不錯(如果兩者都為 None),但真正發生的是target
在評估中轉換為布爾值:
if bool(target) == True and target2 is None:
由于target
不是None/0/""
,布爾轉換返回 True。這導致了代碼中的意外行為。
同樣的想法適用于elif
聲明
添加回答
舉報
0/150
提交
取消