|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| 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.
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:
Method 2: Switch between two (or more) animations, depending on the turtle's heading.
Method 3: As the turtle's heading changes, the turtle switches between 8 (or more) turtle 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." |
|||||||||||||||||||||||||||||||||||||
|