You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python自动化脚本无法回退至Stage 0,跳转至下一阶段问题求助

问题根源

函数内的Stage = 0是局部变量,和全局作用域的Stage并非同一变量,所以修改后不会影响全局的Stage值。比如在Stage3时触发CheckForFull(),函数里修改的只是自己的局部Stage,全局Stage仍为3,执行完Stage3的代码后依然会被设为4。

解决方案

方案1:声明全局变量

在每个需要修改Stage的函数开头,添加global Stage声明,让函数直接操作全局变量:

def CheckForFull():
    global Stage  # 声明使用全局变量
    if pyautogui.locateOnScreen("Full.PNG", confidence = .8, grayscale = True):
            pydirectinput.moveTo(940, 689)
            ait.click()
            pydirectinput.moveTo(500, 500)
            pydirectinput.press("esc")
            pydirectinput.press("r")
            pydirectinput.press("enter")
            Stage = 0

def CheckForShop():
    global Stage
    if pyautogui.locateOnScreen("Exit.PNG", confidence = .8, grayscale = True):
        pydirectinput.moveTo(957, 1000)
        ait.click()
        pydirectinput.moveTo(500, 500)
        pydirectinput.press("esc")
        pydirectinput.press("r")
        pydirectinput.press("enter")
        Stage = 0

def CheckForRebirths():
    global Stage
    if pyautogui.locateOnScreen("TopRebirths.PNG", confidence = .4):
        pydirectinput.press("esc")
        pydirectinput.press("r")
        pydirectinput.press("enter")
        Stage = 0

方案2:返回重置状态(更推荐)

避免全局变量依赖,让检测函数返回是否触发了重置,在主循环中统一处理:

第一步:修改检测函数

def CheckForFull():
    if pyautogui.locateOnScreen("Full.PNG", confidence = .8, grayscale = True):
            pydirectinput.moveTo(940, 689)
            ait.click()
            pydirectinput.moveTo(500, 500)
            pydirectinput.press("esc")
            pydirectinput.press("r")
            pydirectinput.press("enter")
            return True  # 返回True表示需要重置
    return False

def CheckForShop():
    if pyautogui.locateOnScreen("Exit.PNG", confidence = .8, grayscale = True):
        pydirectinput.moveTo(957, 1000)
        ait.click()
        pydirectinput.moveTo(500, 500)
        pydirectinput.press("esc")
        pydirectinput.press("r")
        pydirectinput.press("enter")
        return True
    return False

def CheckForRebirths():
    if pyautogui.locateOnScreen("TopRebirths.PNG", confidence = .4):
        pydirectinput.press("esc")
        pydirectinput.press("r")
        pydirectinput.press("enter")
        return True
    return False

def CheckForAll():
    # 任意一个检测触发就返回True
    return CheckForFull() or CheckForShop() or CheckForRebirths()

第二步:修改主循环

每次执行完阶段操作后,先检查是否触发重置,再决定是否切换Stage:

while True:
    if CheckForAll():
        Stage = 0
        continue  # 直接回到循环开头,重新处理Stage 0逻辑
    
    if Stage == 0:
        for i in range(2):
            pydirectinput.press(["space","w", "a", "s", "d", "r"])
        keyDown("w")
        keyDown("space")
        time.sleep(8)
        keyUp("space")
        keyUp("w")
        # 切换Stage前再次检查是否触发重置
        if not CheckForAll():
            Stage = 1
    elif Stage == 1:
        keyDown("d")
        time.sleep(3)
        keyUp("d")
        if not CheckForAll():
            Stage = 2 
    elif Stage == 2:
        keyDown("w")
        time.sleep(3)
        keyUp("w")
        if not CheckForAll():
            Stage = 3
    elif Stage == 3:
        keyDown("a")
        time.sleep(3)
        keyUp("a")
        if not CheckForAll():
            Stage = 4
    elif Stage == 4:
        keyDown("s")
        time.sleep(3)
        keyUp("s")
        if not CheckForAll():
            Stage = 1
    else:
        print("Stage system is broken...")

这样修改后,只要检测到触发条件,会立刻将Stage设为0并跳过后续的Stage切换,避免进入下一阶段。

内容的提问来源于stack exchange,提问作者Dark Cloud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 13:01:12