Project Folders
Teacher Resources
Student Resources
How Do I?
Site Map
Our Team
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.


The sail button is set to many times. The wave color is programmed with this instruction:

sety ycor + 1

The boat turtle's heading is set to 160. The boat's shape is extended so that the bottom of the boat is at the center of the shape.

Right-click HERE to Download

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.

Right-click HERE to Download

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.

Right-click HERE to Download
The sail button is set to many times.

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.

« Previous
Next »

© copyright 2001 - 2012  OpenWorld Learning.   All rights reserved.