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

code
Right-click HERE to Download

Notes:  This project uses two special techniques to keep the skater on the pond:

  1. The center point of the turtle detects the colors. The two skater shapes have been lengthened so that the center point of each shape is the skating blade. In this way, when the blade reaches another color, the reaction is triggered. (If the shape were cropped so that the center point were the skater's hips, then her skating leg would sometimes leave the ice.)
  2. A hidden turtle is placed in the middle of the pond. (It has been renamed hidden-turtle and hidden with the ht command.) When the skater passes over a different color, it points towards this hidden turtle and then adjusts its heading a random amount (up to 30 degrees). The hidden turtle ensures that the skater is always crossing the pond and never leaving it altogether.

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.

code
Right-click HERE to Download
Click go, then click the arrows to steer the car. The green color has been programmed with this instruction:

if who = "car [go stopall]

The four car shapes are:

They must be closely cropped:

this

not this


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.





code

(This project also uses the same 4 procedures as the project above to steer the car when the arrows are clicked: move-up, move-right, move-down, and move-left.)

When car turtle touches the large green turtle it resets its position. When it touches the house turtle, it triggers an announcement.
Right-click HERE to Download

« Previous
Menu
Next »


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