【问题标题】:Disable a button UNTIL another Button is Pressed?禁用一个按钮直到另一个按钮被按下?
【发布时间】:2021-10-08 23:17:54
【问题描述】:

您好,提前谢谢您。我编写了一个脚本,却发现它有一个重大缺陷。

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?

【问题讨论】:

    标签: python python-3.x pygame scheduled-tasks schedule


    【解决方案1】:

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

    我希望 JOYBUTTON #8 只调用一次时间表,直到 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           
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2014-06-17
      • 2021-10-07
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多