I'm trying to make a game where the turtle needs to be able register and respond to user inputs (Simplified, a letter will appear and the user needs to click it on the keyboard. So if it shows "b" the user types "b"). I only added the letters a to f to make it simpler during the test. Each of them has a function that that'll execute when the letter has been pressed and the program is listening to it.
Everything worked out fine until I added a while function. Currently there is nothing in the while function (Asides from pass) but after I made it the code will no longer respond to user inputs. Could someone tell me how to fix this? My end goal is to have the program always listening for user input while a while loop runs and does its code. Below is my current code
import signal, turtle
def timeout_handler(signal, frame): # End of timer function
raise Exception('Time is up!')
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(10) # Number inside is how long the game will last.
def hit_a():
print("a registered")
def hit_b():
print("b registered")
def hit_c():
print("c registered")
def hit_d():
print("d registered")
def hit_e():
print("e registered")
def hit_f():
print("f registered")
turtle.onkey(hit_a, "a")
turtle.onkey(hit_b, "b")
turtle.onkey(hit_c, "c")
turtle.onkey(hit_d, "d")
turtle.onkey(hit_e, "e")
turtle.onkey(hit_f, "f")
turtle.listen()
while True:
pass
# Add program here
turtle.mainloop()
EDIT: I need a while code in there or at least a loop to keep a section repeating. In pseudocode it looks like this:
Several turtles each write one letter somewhere on the screen
Program waits for user to input a letter
Award/deduct points based on if they got it right or wrong
go back to line 2 if they got it wrong.
repeat
I plan on adding more stuff but I first need to get the base game going. The only way I know on how to code this is by using a while loop. But using one appears to be impossible since it prevents the program from listening to user inputs. Note that I need this all on turtle, not on the terminal (Command prompt if on windows) As I will colour code the letters to show which one to avoid and which ones to enter. How should I write this?
I also want to quickly mention that I have "avoided" this problem before. In the code below the program responds to user inputs when it's in the while loop. (Ignore the problems and the functions onkey() is assigned to. The idea is that the program responds while in the loop). However, I can't find out why in this code the program responds while in the loop but in the code above it doesn't register any user input
turtle.onkey(lower_game1_number, "s")
turtle.onkey(increase_game1_number, "w")
turtle.listen(xdummy=None, ydummy=None)
while True: # game1 is a turtle
game1.write(first_game_num_display,False,"center",("Arial",30))
game1_timer = random.randint(2,4)
time.sleep(game1_timer)
increase_game1_number()
game1.undo()
print(game1_timer)
mainloop()