Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

4. Speed and Variables

  • 1. Introduction to Painting
  • 2. Motion
  • 3. Color and Stamping
  • 4. Speed and Variables
  • 5. Add-Ons
  • 6. Reflection
  • 7. Variables in the World Around You
  • 8. Wrap-up: Share Your Project
  • 9. Wrap-up: Show Your Project
arrow_backward Back Next arrow_forward
Transcript

In this video, you will use variables to change the speed at which Tera moves toward the mouse pointer. By this time, you should have a fully functioning Paint with Tera project. Tera changes colors, stamps, and moves toward the mouse pointer. But, what if you wanted to give the user some control over how fast Tera moves?

Tera’s speed is determined by the number in the “move 10 steps” block. Use a variable to manipulate that value.

To make a variable, click on the data menu, then click on “Make a variable.”

Because you want the variable to determine how fast Tera moves, name it “Speed.” Click “OK.”

Notice that creating the variable caused new blocks to appear. Drag out the “speed” variable, and place it in the “move 10 steps” block. Test your program.

Tera spins, but doesn’t move. That’s because the sprite is moving “speed” steps. Look at the value of “speed” in the orange box in the top left corner. It equals “0.”

Tera is moving 0 steps each time this block runs, which is why it isn’t moving.

To change the value of the variable, drag out the orange “change speed by 1” block.

Clicking on it will make Tera resume moving. With each click, the value of the variable “Speed” increases.

In order to change the value of “Speed” while the program is running, you need to use an event like you learned about on Day 3. Events trigger code to run.

Pull out the “when key pressed” block. Change the value in the block to “up arrow,” and attach it to the “change Speed by 1” block. Test it by pressing the up arrow. Tera’s speed increases by one. You successfully changed the value of the speed variable!

To slow down Tera, drag out another “change speed by” block. This time, make the value negative 1. Attach another “When space key is pressed” event, and change the value to “down arrow.” You can now use the up and down arrows to make Tera move faster or slower. Test it out!

Now, customize your project. Change Tera’s costume by going to the costumes tab and adding a new sprite from the library. Also, try drawing on a costume to see its effect. Test it out to see your colorful creation.

Change the title to match your creation.

Finally, let people know how to use your project. Click on “See Project Page.” Under “Instructions” tell people how to use this project. Make sure to mention that the up arrow key will speed the sprite up and the down arrow key will slow the sprite down.

Congratulations on building an interactive art project! Ask a neighbor to try it out and see what they can create. Now, it’s your turn!

1. Create a variable named “Speed,” and put it into the “move 10 steps” block.

2. Make the variable’s value increase and decrease using the “change speed by” block and key presses. 3. Customize your project by changing the costume and the project’s title. 4. Write instructions letting people know how to use this project.

Instructions
  1. Create a variable, name it "Speed," and put it into the "move 10 steps" block.
  2. Make the variable increase and decrease using the "Change speed by" block and key presses.