Car AI Challenge

Remix this starter project.

Now program the car to drive around the track without going off the track. The car should be able to go around any of the tracks (there are 3 different tracks on the backdrop) without going off the track.

How to get the car to drive itself around the track

One easy strategy to do this is to add sensor costumes and check when they are touching the grass outside the road. If you look in the starter project you will see that the sensor costumes have already been created in the car sprite. There is left sensor, right sensor and front sensor which are just rectangular blocks.

So, in a loop you will do the following:

  • make the car move forward (move X steps)
  • check if left front corner is touching grass:
    • switch costume to the left sensor
    • check if it is touching the grass colour
    • if it is touching grass, turn the car right by a small amount and repeat until not touching grass
    • do all the above without screen refresh so you don’t see the costume change
  • check if right front corner is touching grass (same idea as for left)
  • then switch back to the car costume
  • keep going until the car reaches the finish line

Measure how long it takes to complete a lap

You will need to set up a timer variable to record the time of 1 lap of the track. Because there are 3 different tracks you can make 3 timer variables (if you have time), one for each track, OR, even better, make a list with 3 items (one for lap time for each track).

Share your project and add it the Car AI Competition studio.

Need More Help?

See this video. (Yes, I know it’s an older Scratch 2 video but the blocks are still pretty much the same).