|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| How do I make a turtle react when it comes upon something? | |||||||||||||||||||||||||||||||||||||
|
As the turtle checks its surroundings, it can detect colors, the
presence of other turtles, or its x and y coordinates. The turtle
itself can react to its surroundings by changing its shape or heading or
size, for instance. It can also trigger announcements, sound effects,
changes in other turtles, or other events. The samples below
demonstrate several techniques for triggering reactions to colors or
other turtles. (See also "How can I use x and y coordinates to guide a turtle?") As for the reactions themselves: the possibilities are
endless! Method 1: Look for a change in the color underneath the moving turtle. Use colorunder (with when or if) or program a color to react to a turtle (as in Bouncing Off Walls 1.) In the project below, the turtle (dressed as a skater) constantly checks the color underneath it. When the color is not color 92, the skater changes its shape and its heading.
Notes: This project uses two special techniques to keep the skater on the pond:
It is not always possible to adjust the dimensions of the shapes to trigger a reaction when the leading edge reaches a different color. In the project below, notice how the car extends a bit into the grassy area before the color change is detected. If we lengthen the car shapes to center the front edge of the car, then the car would jump partway onto the grass when it changes shape and direction.
Method 2: Trigger a reaction when one turtle touches another turtle. Use touching? (with when or if).
Only the colored portions of turtles react to touching other turtles. They ignore the clear areas. So the car turtle reacts as soon as it touches green. It does not protrude into the grassy area as in the project above.
|
|||||||||||||||||||||||||||||||||||||
|