How to have a piece of code continue to run throughout a function, while also allowing the other code to run?

74 views Asked by At

My goal is when button A or B is pressed anytime throughout the whole function, it switches to a new process (screen). I am currently trying to use threading, but it is not working, and I am honestly not sure how I can get my plan to work.

def buttonB():
    if button_b.read():
        clear()
        play()

def buttonA():
    clear()
    howToPlay()

#Creates Main Title Screens
def main():
    t1= _thread.start_new_thread(buttonB, ())
    # Creates Game Ttile Screen
    clear()
    display.set_pen(GREEN)
    display.text("Blink of an Eye", 30, 25, 240, 5)
    display.update()
    time.sleep(2)
    clear()
    screen = 1
    # Switches between two "screens" 
    while True:
        #How To play Title Screen
        if screen == 1:
            display.set_pen(YELLOW)
            display.text('''How To Play: Press 'A' ''', 15, 35, 240, 4)
            display.update()
            time.sleep(1)
            clear()
            screen = 2
        
        # Play Title Screen
        elif screen == 2:
            display.set_pen(MAGENTA)
            display.text('''Play: Press 'B' ''', 15, 35, 240, 4)
            display.update()
            time.sleep(1)
            clear()
            screen = 1

Please assist me on how I may write a piece of code that allows the code to constantly check in the function to see if button a or b is being pressed, and when it gets the hey, this button was pressed, the code no longer checks and it moves to another function. I would also like to clarify that this code has other functions that use the same button for different reasons, so I only want it to work within the one function so it doesn't interfere with the other code. (I have the same problem for the rest of the code, but I thought I wouldn't add it because it is the same problem).

1

There are 1 answers

2
iamdeedz On

You need three functions for this. One to check if button a was pressed.

def is_button_a_pressed():
    return True

Another for checking if button b was pressed.

def is_button_b_pressed():
    return False

I just returned True or False for these as I don't know how you check. Same effect, except I manually change it.

And finally one with a while-loop which constantly runs both of these functions until one of them succeeds (returns True).

def check_buttons():
    global has_button_been_pressed
    while not has_button_been_pressed:
        if is_button_a_pressed():
            has_button_been_pressed = True
            how_to_play()

        if is_button_b_pressed():
            has_button_been_pressed = True
            play()

To implement these functions into your code, all you need to do is pass the check_buttons function into _thread.start_new_thread and you're done!

The "full" code might look something like this (obviously it's not actually the full code but it's the part you needed).

has_button_been_pressed = False
def is_button_a_pressed():
    return True


def is_button_b_pressed():
    return False


def check_buttons():
    global has_button_been_pressed
    while not has_button_been_pressed:
        if is_button_a_pressed():
            has_button_been_pressed = True
            how_to_play()

        if is_button_b_pressed():
            has_button_been_pressed = True
            play()


def main():
    _thread.start_new_thread(check_buttons, ())
    # Creates Game Title Screen
    clear()
    display.set_pen(GREEN)
    display.text("Blink of an Eye", 30, 25, 240, 5)
    display.update()
    time.sleep(2)
    clear()
    screen = 1
    # Switches between two "screens" 
    while not has_button_been_pressed:
        if screen == 1:
            display.set_pen(YELLOW)
            display.text('''How To Play: Press 'A' ''', 15, 35, 240, 4)
            display.update()
            time.sleep(1)
            clear()
            screen = 2

            # Play Title Screen
        elif screen == 2:
            display.set_pen(MAGENTA)
            display.text('''Play: Press 'B' ''', 15, 35, 240, 4)
            display.update()
            time.sleep(1)
            clear()
            screen = 1

Hope this helps!