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.
Show Comments

Note

Discuss topics in this section with classmates.

Show Comments
Next Section - Chapter 14 - Summary