|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| How can I use x and y coordinates to guide a turtle? | |||||||||||||||||||||||||||||||||||||
|
We can use x and y coordinates to reposition a moving turtle or to
define boundaries for a turtle's movements. (See also "How do I make a turtle react when it comes upon something?" for other
methods of triggering reactions to a turtle's environment.) Method 1: A turtle can follow the contours of a colored area. Use sety and ycor or setx and xcor.
Note: In this project, there are three forces influencing the boat's movement: the forward command, the turtle's heading of 160, and the programmed color. With its heading of 160, the boat turtle keeps trying to move downward and to the right, while the programmed wave color keeps trying to nudge the boat turtle up (with sety ycor + 1) until the center of the boat turtle is no longer touching the wave color. As soon as the boat turtle rises above the wave color, it begins descending (slightly to the right) again because of its heading. Then the wave color pushes it back up again. So the boat turtle rides the waves. In the variation below, the boat turtle's movement has a fourth influence: touching the darker moving wave turtle gives the boat turtle an extra bounce.
Method 2: Trigger a reaction to the turtle's x and y coordinates. Use xcor and ycor. In the sailboat project below, the turtle constantly checks its position on the project page. If it gets too close to the boundaries of the "ocean," it changes direction. So this boat never sails off the right edge of the page only to reappear on the left edge; it sails back and forth.
Note: The boat turtle constantly checks its heading and adjusts its shape so that if it is moving anywhere toward the right it is facing right and if it is moving anywhere toward the left it is facing left. Meanwhile, two if commands check the boat turtle's x coordinate to see if it has moved too far to the right or left, and two other if comands check the boat turtle's y coordinate to see if it has moved too far up or down. In each case, the turtle adjusts its heading to move off in another direction without ever touching or crossing the ocean's boundary. |
|||||||||||||||||||||||||||||||||||||
|