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).
You need three functions for this. One to check if button a was pressed.
Another for checking if button b was pressed.
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).
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).
Hope this helps!