Time for action - slide sensor

Like we did with the light sensor exercise, we're going to open a sprite that already has scripts associated with it. In this case, we'll use the gravity marbles.

  1. Start a new Scratch project and select the choose new sprite from file option.
  2. Delete the original sprite.
  3. Open the Things folder and select the Gravity Marble sprite.
    Time for action - slide sensorslidermotion, controlling
  4. Click the flag and interact with the marble by pressing the arrow keys to make the marble rise and fall. Try to understand the code. When you've had enough, continue with the next step.
  5. We want to raise the marble with the slider sensor on the PicoBoard. Replace the if key up arrow pressed? block with the greater than block from the Operators palette.
  6. From the Sensing palette, add the sensor value block to the first position of the greater than block, and select the slider option. Enter the number 20 in the second value of the greater than block.
    Time for action - slide sensorslidermotion, controlling
  7. Give it a go. Click the flag and move the slider. Enable the slider sensor watcher to display the slider value on the stage.

As long as the slider's value is greater than 20, the marble rises. We can still use the right and left arrow keys to move the marbles across the stage.

What just happened?

This was another example where we substituted a keyboard input with a sensor value from the PicoBoard. This time we used a sprite that was configured to demonstrate gravity. As the slider measured a value of 20, the marble rose. When we reduced the slider value to less than 20, the marble dropped.

If we examine the gravity marble scripts, we learn that the marble will always fall unless it's acted on by some other force. In this case, the force that moves the marble up is the slider. Let the slider value fall below 20, and the marble falls -0.05 pixels at a time.

Using gravity

Stand up from your chair and jump up as high as you can. Grab a pencil and toss it to the ceiling. You know this story. Gravity grounds everything in real life.

When we design games with jumping sprites or falling objects, we can make the game more realistic by adding gravity. And it doesn't matter if we use a PicoBoard, an arrow key, or some other input.

Have a go hero

Want to accelerate the marble's rise up the stage? Replace the 0.1 value in the change y velocity block to use the slider sensor value.

Watch the marble take off. Try adjusting the rate of the marble's ascent in other ways using the slider sensor value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.106.149