#checking play

1 messages · Page 1 of 1 (latest)

winter grail
#

upload the whole file. Use the + to the lift so you don't have to paste it, which takes a lot of vertical space.

#

@stray dragon let's talk in this thread

stray dragon
#

Thanks!

winter grail
#

ok, give me a few minutes

stray dragon
#

Thank you. I've been trying to figure this out for 2 weeks so I truly appreciate the help

winter grail
#
def play_file(filename):
    """Plays a WAV file in its entirety (function blocks until done)."""
    print("Playing", filename)
    with open(f"/sounds/{filename}", "rb") as file:
        audio.play(audiocore.WaveFile(file))
        while audio.playing:
            if any(neokey1.get_keys()) or any(neokey2.get_keys()):
                audio.stop()
                return
#

change play_file() to the above. How it works:

#

neokey_something.get_keys() returns a list of True/False values for all the keys, e.g. something like [True, False, False, False]

#

any() returns True if any of the items in the sequence it is passed is "truthy" (not False or None). So this is checking if any of the buttons are pressed.

#

If so, then it stops the audio early, and returns immediately.

#

Then your existing code will check which button is actually pressed and start playing the new sound

stray dragon
#

Hmmm I'm getting the clicking again

winter grail
stray dragon
winter grail
#

Oh, I know what the problem is, it's that it starts playing right away, and the key is still pressed, so it exits immediately

#

lemme think about that...

stray dragon
#

Take your time

winter grail
#

quite rewritten. Uses lists of fileanmes and colors. Note how there isn't repeated code

#

you'll have to test it: I only have one NeoKey

stray dragon
#

Sure thing one sec

winter grail
#

i encourage you to figure out how it works

#

i also removed unused code

stray dragon
#

It works PERFECTLY

#

I will

#

Thank you so much!

winter grail
#

It's rare to write code that works the first time!

stray dragon
#

Oh I know! (I dabble in ASM for NES dev so I know the feeling

winter grail
#

note the + that concatenates the two lists instead of doing an addition, for instance

stray dragon
#

It’s little things like that man