Avoiding Collisions

You can use conditionals to detect when two turtles are getting close to each other and then have the turtles take evasive action. In the code below they try both try to turn right just as ships do if they are heading straight for each other.

Run the program below. Notice that this code doesn’t quite work as intended. Both jaz and mia turn completely around. How could you modify the code to fix it so that they turn to avoid each other, but don’t end up turning completely around? You might want turn only if the distance between the x values and the y values is less than some amount. If you have trouble figuring out a solution, click on the tab to view one way to solve this.

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

Here is one solution.

Show Comments
You have attempted 1 of 3 activities on this page