Make Halocode the Steering Wheel of the Car on Stage


Programming Result

When you tilt Halocode to the left, the car on stage will move leftward. When you tilt Halocode to the right, the car will move rightward. Halocode is just like the steering wheel of the car.




Use the motion sensor

The motion sensor of Halocode detects how Halocode moves. We'll use the motion sensor in this example. When Halocode is tilted to the left, a corresponding piece of message will be sent to the car on Scratch Stage to make it move leftward. Likewise, when Halocode is tilted to the right, a corresponding piece of message will tell the car to move rightward.


1. Drag a Control block if () then () to the Scripts area. Add a Sensing block Halocode is ()? to the condition.



2. Likewise, for the right side, duplicate the script, and select "right-tilted".



Message broadcast

3. Add two Events blocks broadcast (). Create two pieces of message, namely "left" and "right".



4. Add an Events block when button is pressed and a Control block forever.



Add a sprite

5. Under "Sprites", click "+". In the Sprites Library, choose "Car" from the Transportation category.



6. Delete the default sprite "Panda".



Program the car

7. Add three Motion block: set rotation style (left-right), point in direction (-90), move (10) steps.



8. Add an Events block when I receive (), and choose message "left". When the car receives message "left", it will move leftward for 10 steps.



9. Likewise, for the rightward part, duplicate the script. Choose message "right", and change the direction to "90".

Note: the message received should be the same message that is broadcast.



10. Press the button of Halocode and then tilt it!

Download code

Was this article helpful?
0 out of 0 found this helpful



Please sign in to leave a comment.