Project Folders
Teacher Resources
Student Resources
How Do I?
Site Map
Our Team
How do I make a shape rotate as my turtle turns?
A turtle wearing the default turtle shape (shape 0) appears to rotate as it points in different directions, but if a turtle wears any other shape, the shape itself does not rotate as the turtle moves. We can make a shape appear to rotate by telling a turtle to switch between different related shapes.

All of the methods use setshape (setsh) and setheading (seth).

Method 1:   Limit the turtle to four directions (up, down, right, left) and tie a specific turtle shape to each turtle direction. In the project below, four rotated car shapes have been created easily from one design. After drawing one shape, close it, copy the closed shape (Edit -> Copy); paste it into 3 more closed shapes; then double-click to open each shape, name it, and rotate it as needed.





Each yellow arrow is a turtle with a procedure name as its instruction (either move-up, move-right, move-down, or move-left). These procedures each specify a turtle shape and a heading and move the car turtle forward a small amount. Viewed from above, the car appears to turn as the turtle turns.

Right-click HERE to Download
Click the arrows repeatedly to drive the car around the green square.

Method 1 variation:  Another variation of this method uses one procedure to set the turtle in motion and then uses the four procedures to change just the turtle's shape and heading:

Right-click HERE to Download
Click start, then click the arrows to steer the car. By the way, this project actually uses a large grass-shaped turtle to detect when the car strays from the road. Learn several methods for detecting turtle positions here: How do I make a turtle react when it comes upon something?

Method 2:   Switch between two (or more) animations, depending on the turtle's heading.

In the project below, the fly button is set to many times so the procedure runs forever. Imagine a compass divided vertically in half. If the turtle's heading is greater than 0 and less than 181, the butterfly faces to the right. If the turtle's heading is greater than 180 and less than 360, or if it is 0, the butterfly faces to the left. Whichever way it faces, the butterfly constantly flutters its wings by alternating between two shapes.

Right-click HERE to Download
Click fly and then move the slider to steer the butterfly. When does the butterfly face to the left and to the right?

Method 3:   As the turtle's heading changes, the turtle switches between 8 (or more) turtle shapes.

Imagine a compass divided into 8 equal sections of 45 degrees each. If the turtle's heading is pointing roughly towards 45 degrees, the shape should appear to face in that direction also. In the project below, that particular shape is called s2. (When the turtle's heading is greater than 22 degrees and less than 68 degrees, the turtle's shape will be set to s2.

Right-click HERE to Download
Click fly and then move the slider to steer our hero. Notice how he switches between 8 shapes.

Note:  Method 2 demonstrates the use of if; Method 3 demonstrates the use of when. In a procedure, the computer checks an if command just once to see if the condition is true. So in Method 2 we need to run the fly procedure "many times" to continue checking the turtle's heading. On the other hand, the computer continually checks a when command to see if the condition is true. So in Method 3, the button is set to once, and two forever commands are launched to control the turtle's motion and heading. The 8 when commands run as 8 active processes as long as the button is depressed.

It would be possible to redesign these two projects so that Method 2 uses when commands and Method 3 uses if commands. Just be careful to set the button to many times when using if commands and set the button to once when using forever and when commands. If you set the fly button to many times when using forever or when commands, the computer will try to relaunch these processes over and over again without cancelling any active processes, and you will quickly get an error message: "I can't start a new process in fly."

« Previous
Next »

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