I'm having trouble solving an exercise in a programming book.
The exercise asks that I create 2 turtles and move them randomly, so that each turtle walks 50 units forward, then turn either to the left or to the right, 90 degrees. Turtles should be "born" in a random place within the enclosed space of the screen. They must walk freely until they 'touch'. When this occurs, the program should stop. I was able to write everything down to this final condition.
My difficulty is at the end of the code, in the caminho_ate_parada
function. my idea is that as long as the coordinates (x, y) of alex and tess are different from each other, the program keeps running. If the coordinates (x, y) of alex are equal to the coordinates (x, y) of tess, the program must stop. the odd thing is that sometimes the program for when the two turtles are on the same value of the axis (x), however in different values of the axis (y).
What can I increment in the code to solve this?
As I can not send more than 2 files here, I created a blog just to put the code images
import random
import turtle
alex = turtle.Turtle()
tess = turtle.Turtle()
wn = turtle.Screen()
tess.shape('turtle')
tess.color('blue')
tess.speed(0)
alex.shape('arrow')
alex.color('red')
alex.speed(0)
def isInScreenAlex(w,t1):
leftBound = - w.window_width()//2 + 100
rightBound = w.window_width()//2 - 100
topBound = w.window_height()//2 - 100
bottomBound = -w.window_height()//2 + 100
turtleX = t1.xcor()
turtleY = t1.ycor()
stillInAlex = True
if turtleX > rightBound or turtleX < leftBound:
stillInAlex = False
if turtleY > topBound or turtleY < bottomBound:
stillInAlex = False
return stillInAlex
def isInScreenTess(w,t2):
leftBound = - w.window_width()//2 + 100
rightBound = w.window_width()//2 - 100
topBound = w.window_height()//2 - 100
bottomBound = -w.window_height()//2 + 100
turtleX = t2.xcor()
turtleY = t2.ycor()
stillInTess = True
if turtleX > rightBound or turtleX < leftBound:
stillInTess = False
if turtleY > topBound or turtleY < bottomBound:
stillInTess = False
return stillInTess
def compXa():
MaxXAxisA = int(random.randrange(- wn.window_width()//5, wn.window_width()//5 ))
while MaxXAxisA % 50 != 0:
MaxXAxisA = MaxXAxisA + 1
return MaxXAxisA
def compYa():
MaxYAxisA = int(random.randrange(-wn.window_height()//5, wn.window_height()//5 ))
while MaxYAxisA % 50 != 0:
MaxYAxisA = MaxYAxisA + 1
return MaxYAxisA
xa = int(compXa())
ya = int(compYa())
alex.penup()
alex.goto(xa, ya)
alex.pendown()
def compXt():
MaxXAxisT = int(random.randrange(- wn.window_width()//5, wn.window_width()//5))
while MaxXAxisT % 50 != 0:
MaxXAxisT = MaxXAxisT + 1
return MaxXAxisT
def compYt():
MaxYAxisT = int(random.randrange(-wn.window_height()//5, wn.window_height()//5))
while MaxYAxisT % 50 != 0:
MaxYAxisT = MaxYAxisT + 1
return MaxYAxisT
xt = int(compXt())
yt = int(compYt())
tess.penup()
tess.goto(xt, yt)
tess.pendown()
def positionAlex():
if isInScreenAlex(wn, alex) == True:
coin = random.randrange(0,2)
if coin == 0:
alex.left(90)
else:
alex.right(90)
alex.forward(50)
else:
alex.left(180)
alex.forward(50)
def positionTess():
if isInScreenTess(wn, tess) == True:
coin2 = random.randrange(0,2)
if coin2 == 0:
tess.left(90)
else:
tess.right(90)
tess.forward(50)
else:
tess.left(180)
tess.forward(50)
def caminho_ate_parada():
while tess.xcor() != alex.xcor() and tess.ycor() != alex.ycor():
tess.xcor()
tess.ycor()
alex.xcor()
alex.ycor()
positionAlex()
positionTess()
caminho_ate_parada()
wn.exitonclick()