Programming velocity and speed in Scratch

Lead Image © kudryashka, 123RF.com

Space Maze

We show you how to program speed and direction by guiding a rocket through a maze of alien UFOs.

What's the difference between velocity and speed? That's the question at the heart of this project. Velocity gives speed and direction. For example, "I traveled west on the Interstate at 73mph" is an example of velocity.

In the Space Maze game in this article, a rocket will have the ability to fly up, down, right, and left at varying speeds. The player must navigate the rocket through a field of UFOs to land on a big head in space. The maze, however, is randomly generated, requiring the player to pick a path carefully from the home base to the head.

Every time the rocket collides with a UFO, the rocket has to start the journey over. The object is to reach the head as quickly as possible, without being attacked by a UFO.

Gather Resources

Scratch has a wonderful, but relatively small, library of built-in sprites and backgrounds. For this project, I chose to search the web for "free space sprites" to see what I could find. I found a collection of artwork that includes rockets, UFOs, backgrounds, and a big head. As a bonus, it is available under a creative commons license. Figure 1 shows the game with the list of sprites.

Figure 1: The Space Maze game with imported sprites and costumes.

After downloading a ZIP file of the space sprites [1], you'll need to get the files onto your Raspberry Pi, so you can import them into a new project. Alternatively, you can download the Space Maze project, which includes the sprites and code [2]. To add the new sprites, click the Choose sprite from file icon under the Scratch stage. That's the icon with a file folder on top of a star. Navigate your filesystem to find and add the sprites.

To add the background, first click on the Stage from the list of sprites and then click the Backgrounds tab. Use the Import button to browse for and select one of the backgrounds. Figure 2 shows the Backgrounds tab with the Import Background dialog box open.

Figure 2: The import background dialog box for the Scratch stage.

Of course, you are free to use any sprites you want for the project. When I first created this game, my five-year old wanted to make the default cat navigate through a field of monsters from the fantasy sprites included with Scratch. Our objective was to reach the sun, which is also a Scratch sprite, so this project is adaptable.

Program Rocket Velocity

To help you determine how fast the rocket should move, you need to know the x velocity, y velocity, and speed. So, the first step in programming the rocket movement is to define those three variables. From the Data palette, click the Make a variable button to create the variables x velocity, y velocity, and speed. Make each For this sprite only, because they will only apply to the rocket.

The x velocity variable corresponds to the left and right arrow keys, and the y velocity variable corresponds to the up and down arrow keys.

The speed variable is a customizable setting that determines how fast the rocket should move with one arrow press. The higher the value assigned to the speed variable, the faster the rocket accelerates.

The snippet in Listing 1 shows how to use variables to calculate the velocity for a left or right arrow click. The key () pressed? block can be found in the Sensing palette and is evaluated by the if block.

Listing 1: Setting x velocity with an Arrow Key

As an example, assume the value of speed is 2. On the basis of the change () by () block, one left arrow click would evaluate to -2, which points the rocket left, and one right arrow click would be 2, which points the rocket right. That's not enough to move the sprite, but it's an important starting point because it gives x velocity a value that will be used in another calculation at the top of the main forever block of this program (see Listing 2).

Listing 2: x velocity Moves the Rocket

The set () to () block multiplies the x velocity value, set by a left or right arrow press, by 0.9 and assigns the new value to x velocity. Finally, the change x by () block moves the rocket according to the value assigned to x velocity.

Multiplying x velocity by 0.9 simulates friction, which provides a way to make the rocket slow down and eventually stop unless the player keeps pressing arrow keys. A multiplier of less than one allows the rocket slow down. If you use a multiplier greater than one, the rocket picks up speed every time a new value is calculated in the forever block loop.

Listings 1 and 2 show how x velocity changes; the up/down arrow keys will have corresponding y velocity calculations. The full script to set the rocket's velocity can be seen in Listing 3 and starts when the green flag is clicked.

Listing 3: Setting Rocket Velocity

All the work happens in the forever block, which runs until the game stops. The set () to () blocks and the change blocks run with each pass through the forever block, continually evaluating new values for x velocity and y velocity. Each of the four arrow keys has its own conditional statement defined by the if key () pressed? block.

The values calculated for x velocity and y velocity from arrow presses are used by the set () to () blocks. At the start of the game, x velocity and y velocity are set to 0, which means the rocket will not move until an arrow key is pressed. Speed is set to 2.5.

The starting variable values can be seen in Listing 3. The when I receive () block is signaled by the velocity script via the start over broadcast, and it's critical in setting the starting values for the rocket.

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