Project 548: Homeward Bound

4. Go Back if you Drive Off the Road

The robot cars that are carrying the animals are built to drive on the road, but if they drive off the road their wheels get damaged and they have to go back to the start to get them replaced so that they can take the animals home. Let's add code to our cars so that they go back to the start when they touch the grass.
Starting point file for this challenge

Your goal

Steps

1. Go to your "Underwater Animal" sprite

Select your "Underwater Animal" sprite and look at the code inside of it.

2. Add an "if ___ then ___" block inside the 'repeat until' loop

We tell the game to check if something is true or not using 'if-then' statements. Find the loop that says "repeat until touching Underwater" and add an "if __ then __" statement inside of it.

3. Checking if the Car is Off the Road Part 1

How should we check if the robot car has driven off the road? Should we check if the sprite is touching the grass? 

Do you see any problems with using the "touching color" block to test if the sprite is touching the green grass?

4. Checking Part 2

The problem with using the regular "touching color" block, is that the robot car is tall enough that it looks like the top of the car is touching the grass.

So we are going to check if the wheels of the robot car are touching the grass. Which we can do with the "color ___ is touching ___" block.

5. Pick the colors

The rim of the wheels of the robot car are white, so we are going to check if white is touching the green color of the grass. 

6. Send the Car Back to the Start

Get a "go to random position" block from the blue "Motion" tab and change 'random position' to "Start" and put it inside of your 'if-then' statement.

7. Stop the code

Because the robot car has to go back to the beginning if it goes off the road, we need to tell it to stop doing any of the other instruction it has been given.

Add a "stop all" block from the "Control" tab and change 'all' to "other scripts in this sprite".

8. Add Color Detection Code to the Other Animals

Add code the other five animal types to send the robot cars back to the start when their wheels touch the grass.