禁用一个按钮直到另一个按钮被按下?

Disable a button UNTIL another Button is Pressed?

您好,提前谢谢您。我写了一个脚本,才意识到它有一个重大缺陷。

在下面的代码中 BUTTON # 8 调用了时间表。它工作得很好,直到我不止一次按下 BUTTON # 8。

 game_state = True
    run = True
    
    while run:
        for event in pygame.event.get():
    
            if event.type == JOYBUTTONDOWN:
                print(event)
    
                if event.button == 8:
                    if game_state == True:
                        schedule.every(3).seconds.until(timedelta(minutes=60)).do(sendCommands)
    
                    else:
                        game_state = True  # schedule on
    
            if event.type == JOYHATMOTION:
                if event.value[1] == 1:
                    game_state = False
    
    
                if event.value[1] == -1:
                    game_state = False
    
    
    
            if event.type == JOYBUTTONDOWN:
                if event.button == 4:
                    game_state = False

if game_state == True:
        schedule.run_pending()

pygame.quit()
sys.exit()

我希望 JOYBUTTON # 8 只调用一次调度,并且在 game_state = False 之前不会再次调用。我注意到,如果我不小心多次按下按钮,计划会被调用多次,运行 一次调用多个实例。如何防止多个实例同时出现 运行?

有没有办法在我按下 BUTTON # 8 后禁用它,直到我按下 JOYHATMOTIONBUTTON # 4?

仔细阅读问题中的句子。答案隐藏在问题中:

I would like JOYBUTTON # 8 to only call the schedule once, and not again until game_state = False.

您需要初始化 game_state = False 并在 game_stateFalse 按下按钮时启动计划。计划开始时设置game_state = True

game_state = False
run = True

while run:
    for event in pygame.event.get():

        if event.type == JOYBUTTONDOWN:
            print(event)

            if game_state == False and event.button == 8:
                schedule.every(3).seconds.until(timedelta(minutes=60)).do(sendCommands)
                game_state = True

            if event.button == 4:
                game_state = False

        if event.type == JOYHATMOTION:
            if event.value[1] == 1 or event.value[1] == -1:
                game_state = False