Snap! Project 5: Sprite Sizer

Description: Make the turtle sprite bigger or smaller with the distance sensor

Programming Concepts: using sensors to control Snap! graphics

Materials: Hummingbird Controller, Distance Sensor

Difficulty: Intermediate

Approx. Time: 30 Minutes

Hummingbird setup

1. Connect the distance sensor to sensor port 1.

2. Plug the Hummingbird into your computer.

Programming steps

1. We are going to make the value of the distance sensor determine the size of the turtle (arrow) sprite. To change the sprite's size, go to the "Looks" category and move a "set size to" block to your screen.

2. Next, attach a distance sensor block to the size block.

3. This resizing should occur repeatedly, so place a forever loop around the size block.

4. Run the code by clicking the loop.

The code works, but the arrow gets smaller when your hand is closer to the sensor. Real objects get larger when they are closer, so it would make more sense if we made the arrow get bigger when the distance sensor detected that the object was closer.

5. Add a subtraction block that calculates 105 minus the current sensor reading. 5 is the minimum value the distance sensor can produce, so 105 minus the sensor's value will produce a size of at most 100%.

6. Run the code again. This time the arrow should get larger when the object is closer.

7. Now let's make the size change more noticeably by making the arrow three times larger. Add a multiplication block to do this.

8. Run the program and the arrow should be much larger when your hand is close.

Example file

This file was created with the Birdbrain Robot Server. Use this utility to convert it to a form that can be used with the Chrome Finch connection app.

example-file-5-sprite-sizer.txt