Raspberry_Pi_Education_Manual

Notes:

To do all this, you’ll need, along with the green flag, blocks from Looks and Motion. To move the car, we use our old friend the “forever” loop. The car moves one step each cycle.

If the car leaves the track, it will touch the green grass. This event will cause the first “if” block to crash the car and stop the script for the car.

As the car crosses the finish line, it will touch the white line. This event will cause the second “if” block to say “I win!” and stop all the script running for both cars.

2. Controlling the red car

Ok, so we can get the red car moving. But we need to be able to steer it. To do this we need two “when [ ] key pressed” (from Control) events are used to control the car.

As you’d expect, the “right arrow” key turns the car to the right and the “left arrow” key turns the car to the left.

3. Do the same for the purple car

Similar code is used to control the purple car with some slight alterations. The easiest way to do this is to make the red car and then duplicate the sprite. Remember to change the costumes of your new car from red to purple.

A beginner’s guide to Scratch

48

Made with FlippingBook flipbook maker