I try to write a program in python that lets the user to put a bet on one of the turtles and after the race tells him if he was correct or not. I want to let the user choose if he wants to restart the race by clicking 'r' - ( i used the turtle.onkey method) and it worked fine until I put this line of code: bet = screen.textinput("Choose your bet", "Enter blue or green: "). now the program lets the user choose his bet like I wanted but then it wont react when 'r' is pressed.. I want to clarify that after the textinput line of code the program won't react to any of the .onkey methods. anyone has idea why it happens?
THE CODE:
import turtle
import random
import ctypes
speed = random.randint(0, 2)
myTurtle = turtle.Turtle()
myTurtle2 = turtle.Turtle()
screen = turtle.Screen()
def up():
myTurtle.setheading(90)
myTurtle.forward(10)
def down():
myTurtle.setheading(270)
myTurtle.forward(10)
def left():
myTurtle.setheading(180)
myTurtle.forward(10)
def right():
myTurtle.setheading(0)
myTurtle.forward(10)
def up2():
myTurtle2.setheading(90)
myTurtle2.forward(10)
def down2():
myTurtle2.setheading(270)
myTurtle2.forward(10)
def left2():
myTurtle2.setheading(180)
myTurtle2.forward(10)
def right2():
myTurtle2.setheading(0)
myTurtle2.forward(10)
def restart():
myTurtle.setposition(400, -300)
myTurtle.setheading(90)
myTurtle2.setposition(-400, -300)
myTurtle2.setheading(90)
speed = random.randint(0, 2)
bet = screen.textinput("Choose your bet", "Enter blue or green: ")
while myTurtle.ycor() < 300 and myTurtle2.ycor() < 300:
if speed == 1:
up()
elif speed == 2:
up2()
speed = random.randint(0, 2)
if myTurtle.ycor() == 300:
if bet == "blue":
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You lost", 0x00010000)
else:
if bet == "green":
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You lost", 0x00010000)
screen.title("Turtle race")
turtle.listen()
myTurtle.shape("turtle")
myTurtle2.shape("turtle")
myTurtle.setposition(400,-300)
myTurtle.setheading(90)
myTurtle2.setposition(-400,-300)
myTurtle2.setheading(90)
myTurtle.dot(10, "blue")
myTurtle2.dot(10, "green")
myTurtle.pencolor("blue")
myTurtle2.pencolor("green")
myTurtle.speed(0)
myTurtle2.speed(0)
bet = screen.textinput("Choose your bet", "Enter blue or green: ")
while myTurtle.ycor() < 300 and myTurtle2.ycor() < 300:
if speed == 1:
up()
elif speed == 2:
up2()
speed = random.randint(0, 2)
if myTurtle.ycor() == 300:
if bet == "blue":
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You lost", 0x00010000)
else:
if bet == "green":
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You lost", 0x00010000)
turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(restart, 'r')
turtle.onkey(up2, 'w')
turtle.onkey(down2, 's')
turtle.onkey(left2, 'a')
turtle.onkey(right2, 'd')
turtle.mainloop()