Endless loop with two randomly walking turtles

After I hung up my previous program (a turtle that walked randomly and bounced off the walls until it hit them 4 times), I tried to do the next exercise in the guide, which suggests two turtles with random starting points that walk along the screen and bounce off the walls until they shake each other - there is no counting variable to decide when they should stop. I managed to write the whole thing, except for the part where they collide and stop: I calculated a logical function that returns True if the X and Y coordinates of the turtles are the same, and False if they don’t, but instead they continue to go, and the only The way to terminate the program is to make the translator exit the game. What am I doing wrong?

 import turtle import random def setStart(t): tx = random.randrange(-300,300,100) ty = random.randrange(-300,300,100) t.penup() t.goto(tx,ty) t.pendown() def throwCoin(t): coin = random.randrange(0,2) if coin == 0: t.left(90) else: t.right(90) def isInScreen(w,t): leftBound = w.window_width() / -2 rightBound = w.window_width() / 2 bottomBound = w.window_height() / -2 topBound = w.window_height() / 2 turtlex = t.xcor() turtley = t.ycor() stillIn = True if turtlex < leftBound or turtlex > rightBound or turtley < bottomBound or turtley > topBound: stillIn = False return stillIn def collide(t,u): if t.xcor() == u.xcor() and t.ycor() == u.ycor(): return True return False def randomWalk(t,w): if not isInScreen(w,t): t.left(180) else: throwCoin(t) t.forward(100) def doubleRandom(t,u,w): while not collide(t,u): randomWalk(t,w) if collide(t,u): break randomWalk(u,w) wn = turtle.Screen() wn.bgcolor('lightcyan') steklovata = turtle.Turtle() steklovata.color('darkslategray') steklovata.shape('turtle') setStart(steklovata) catshower = turtle.Turtle() catshower.color('orangered') catshower.shape('turtle') setStart(catshower) doubleRandom(steklovata,catshower,wn) wn.exitonclick() 

EDIT: to check if there was an error in the collide(t,u) function or in the while that calls it, I wrote another function that sends both turtles to the same place and prints some text (if someone wonders if this is an internal joke, like every flipping name I came up with), if collide(t,u) returns True . When I ran it, the DID text printed, indicating that collision detection was working correctly ... but the loop somehow did not tell Python that the turtles should stop when they collide. This is the function:

 def raul(t,u,w): t.goto(1,1) u.goto(1,1) if collide(t,u): t.write('RAUL SUNTASIG') 

Does this give you guys any ideas as to why it doesn't work?

+4
source share
3 answers

Edit: A completely modified answer.

I added print statements to the collide routine and got the following:

 -300.0 -200.0 -100.0 -100.0 -300.0 -100.0 -100.0 -100.0 -300.0 -100.0 -200.0 -100.0 -300.0 -100.0 -200.0 -100.0 -300.0 1.13686837722e-13 -200.0 -100.0 -300.0 1.13686837722e-13 -200.0 1.27897692437e-13 -300.0 1.13686837722e-13 -200.0 1.27897692437e-13 -200.0 4.02080297728e-14 -200.0 1.27897692437e-13 -200.0 4.02080297728e-14 -200.0 100.0 -200.0 4.02080297728e-14 -200.0 100.0 

Here's how you fix it:

 def collide(t,u): if abs(t.xcor() - u.xcor()) < 1 and abs(t.ycor() - u.ycor()) < 1: return True return False 

Oh, and you should do a collide() check after every randomWalk() , not just the first one.

+1
source

Your code only checks for collisions after moving turtles. In about half the cases, turtles will start an odd number of steps from each other, in which case they will always be several steps apart when a collision is detected. Even if one of them moves to another place, the other will leave before you check for a collision. To fix this, do an additional collision check between moves:

 while not collide(t, u): randomWalk(t, w) if collide(t, u): break randomWalk(u, w) 

There is one more thing to consider; turtles always make the right turns, or 180s, when they hit the wall. This introduces parity problems, similar to the above, that can prevent or delay a collision if the turtles cannot make the right turn sequence or are forced to wait until they make only the right wall collisions to point them in the right direction. You can fix this by making turtles a random choice from all 4 directions to walk:

 def throwCoin(t): # 4-sided Ruritanian nickel t.left(90*random.randrange(4)) 
0
source

Use larger turtles.

If you currently call it a collision, if the x and y coordinates of the two turtles exactly coincide, this will be a very difficult condition to meet.

If you imagine that your turtles have a radius of 5 points, then at any time the distance between the centers of the center of the two turtles is less than 10, they will collide.

0
source

Source: https://habr.com/ru/post/1495410/


All Articles