首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第二个循环忽略按钮按下

第二个循环忽略按钮按下
EN

Stack Overflow用户
提问于 2021-02-18 11:22:23
回答 1查看 29关注 0票数 0

我正在尝试写一个反应游戏,在运行Python2.7的Raspberry Pi上使用一些亮起的街机按钮,第一个灯/按钮按下序列效果很好,但在第二个灯亮起后,按钮按下将不会及时注册。

代码语言:javascript
复制
def FastButtons(btn):
    btn.was_held = True
    Choice = random.choice([17, 22, 5, 24, 16])
    RedButton.when_pressed = FastPush
    BlueButton.when_pressed = FastPush
    YellowButton.when_pressed = FastPush
    GreenButton.when_pressed = FastPush
    WhiteButton.when_pressed = FastPush
    FastGo(Choice)

def FastGo(led):
    global FastButtonTime
    if led == 17:
        leds.Red.on()
        sleep(FastButtonTime)
        leds.Red.off()
    elif led == 22:
        leds.Blue.on()
        sleep(FastButtonTime)
        leds.Blue.off()
    elif led == 24:
        leds.Yellow.on()
        sleep(FastButtonTime)
        leds.Yellow.off()
    elif led == 5:
        leds.Green.on()
        sleep(FastButtonTime)
        leds.Green.off()
    elif led == 16:
        leds.White.on()
        sleep(FastButtonTime)
        leds.White.off()

def FastPush(btn):
    global GameOver
    global FastButtonTime
    if GreenButton.was_held:
        led = btn.pin.number
        if led  == 17:
                status = leds.Red.is_lit
        elif led == 22:
                status = leds.Blue.is_lit
        elif led == 24:
                status = leds.Yellow.is_lit
        elif led == 5:
                status = leds.Green.is_lit
        elif led == 16:
                status = leds.White.is_lit

        if status:
            print ("it's Active")
            if FastButtonTime > .3:
                FastButtonTime = FastButtonTime - .05
                print FastButtonTime
            leds.off()
            Winner = True
        else:
            print ("it's not active")
            Loser()
            GreenButton.was_held = False
            GameOver = True
            Winner = False
        if Winner:
            sleep(1)
            FastButtons(GreenButton)

在第二次通过时,直到FastGo定义中的sleep完成时,它才会注册按钮按下,从而导致玩家输掉游戏

EN

回答 1

Stack Overflow用户

发布于 2021-02-19 00:47:48

我想我明白了为什么在研究了嵌套函数是如何执行的之后,它不会进行第二次处理,而且因为FastPush调用FastButtons,所以它必须等待这个过程完成,然后才会接受另一个按钮按下。

正因为如此,我将我所有的代码重组为一个函数,使用按钮被按下的速度作为输赢的验证,而不是光的状态。

如果有人知道如何制造它,以便在用户花费太长时间时灯将关闭,而不会阻碍按下按钮的能力,那就太棒了!就像现在一样,它会一直开着,直到按钮被按下。

对于感兴趣的人,我的新函数如下所示

代码语言:javascript
复制
def FastButtons(btn):
        if not BlueButton.was_held and not WhiteButton.was_held:
                btn.was_held = True
                global Winner
                TimeAllowed = 1.5
                while True:
                        if not Winner:
                                blink(leds)
                                sleep(2)
                        led = random.choice([17, 22, 5, 24, 16])

                        if led == 17:
                                leds.Red.on()
                                start = time()
                                RedButton.wait_for_press()
                                stop = time()
                        elif led == 22:
                                leds.Blue.on()
                                start = time()
                                BlueButton.wait_for_press()
                                stop = time()
                        elif led == 24:
                                leds.Yellow.on()
                                start = time()
                                YellowButton.wait_for_press()
                                stop = time()
                        elif led == 5:
                                leds.Green.on()
                                start = time()
                                GreenButton.wait_for_press()
                                stop = time()
                        elif led == 16:
                                leds.White.on()
                                start = time()
                                WhiteButton.wait_for_press()
                                stop = time()
                        TotalTime = stop - start
                        print (TotalTime)

                        if TotalTime < TimeAllowed:
                                Winner = True
                                leds.off()
                                if TimeAllowed > .3:
                                        TimeAllowed = TimeAllowed - .05
                                RandomSleep = random.uniform(0.5, 3.5)
                                sleep (RandomSleep)
                        else:
                                FastLoser()
                                break

def FastLoser():
        global Winner
        Loser()
        GreenButton.was_held = False
        Winner = False
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66253151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档