2

I am fairly new to Python and am trying to make a "List the States" game (in which the player has to input the name of the 50 states in the US). However, I wanted to add a countdown to stop the game if you ran out of time. The problem is that I can't get the countdown to update while the "screen.textinput" box is open.

I have tried using multiprocessing and threading modules to run 2 simultaneous while loops. With the first, it opens two separate windows and with the latter, I found info online that it doesn't work with the turtle module.

This my code at the moment:

timer = turtle.Turtle()
timer.penup()
timer.ht()
timer.goto(-300, 250)

game_is_on = True

while game_is_on:
    # Update the timer
    screen.update()
    end = time.time()
    timer.clear()
    timer.write(f"{int(300 - (end - start))}")
    # Input state name and analyze input
    right_answers = len(previous_answers)

    if right_answers == 0:
        answer_state = screen.textinput(title=f"Guess the State", prompt="What's another state's name?").title()
    else:
        answer_state = screen.textinput(title=f"{right_answers}/50 States Correct",
                                        prompt="What's another state's name?").title()
    if answer_state in data.state.to_list() and answer_state not in previous_answers:
        name = Names(answer_state, data)
        previous_answers.append(answer_state)
    if right_answers == 50:
        game_is_on = False
    if answer_state == "end":
        game_is_on = False

Any ideas how to make this work? Perhaps there is a simpler way out that I am not seeing?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Somewhat related: [Update turtle/gui while waiting for input python](https://stackoverflow.com/questions/23620960/update-turtle-gui-while-waiting-for-input-python). Maybe use `onkeypress` and keep track of what the user has typed by hand. – ggorlen Sep 03 '22 at 01:12

0 Answers0