|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
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).
The project below is a slight modification of the maze project above. The only difference is that this project uses a special turtle which is an exact replica of the grassy area. (Just drag the selection tool to surround the whole background, copy and paste into a shape, then spill white into the areas that are not the desired color. If there are other turtles, such as the arrow turtles in this project, stamp them to imprint them on the background before making the background shape. Place this shape on a turtle and position it carefully over the background.)
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.