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.

 

1.gif

 

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.gif

 

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

3.gif

 

Message broadcast

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

4.gif

 

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

5.gif

 

Add a sprite

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

6.gif

 

6. Delete the default sprite "Panda".

7.gif

 

Program the car

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

8.gif

 

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.gif

 

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.

0.gif

 

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

Download code


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

Comments

0 comments

Please sign in to leave a comment.