Programming velocity and speed in Scratch

Example Velocity Calculations

To begin, I'll run through a calculation with a starting speed of 2.5. A down arrow key press will set y velocity to -2.5, which is multiplied by 0.9 in the set y velocity block; the new y velocity becomes -2.25, which is used in the change y by block. The rocket moves by -2.25.

To make the rocket accelerate, the player needs to press the arrow keys continually. If the player does not press another arrow, the value of y velocity gradually decreases to zero. Beginning with a value of -2.25, a second pass through the forever block would yield a y velocity of -2.025 (-2.25x0.9), a third pass would yield -1.8225, and so on.

In another example, if the rocket is changing its x position by five and the player presses the left arrow key one time, then the new x velocity is 2.75, which means the rocket is still traveling to the right. Remember, in Scratch, positive numbers move up or right. Negative numbers move down or left.

Change Costume Direction

Each arrow key press also sets the orientation of the rocket, as seen in the switch to costume () block. I made the additional costumes by copying the original costume. From the Costumes tab, I clicked the Edit button on each costume to open it in the image editor. Using the rotate and flip tools, I changed the image copies to face the appropriate directions.

As you see in my script, I created a costume to represent up, down, left, and right. I also changed the costume names to correspond to the direction I wanted to make them fly so that I can find the right costume when I use them in my scripts.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content