Programming velocity and speed in Scratch

Set UFO Speed

As a starting point, I imported the UFO sprite four times to use as my enemy ships. The more UFOs you add to the game, the more difficult the game will become, because they are obstacles the rocket has to avoid on its way to the target.

Listing 4 shows the scripts for the first UFO. At the start of the game, the go to x: () y: () block selects a random starting point. The ranges correspond to the (x, y) coordinates on the perimeter of the Scratch stage.

Listing 4: UFO Speed and Collisions

The point in direction block provides a starting direction. Although the direction describes 360 degrees, in Scratch, a circle has values between -180 and 180, instead of 0 and 360. Therefore, 270 degrees would be -90 in Scratch's point in direction () block.

After your scripts are set up for one UFO, you can copy them to the remaining UFOs by right-clicking on the script and selecting duplicate. The script will attach to your mouse pointer, so you can just drag and drop it onto another UFO icon in the sprites list. Repeat until all the UFO sprites have the same scripts, then you can customize each UFO.

For example, all my UFOs point in different directions. For one UFO, I replaced the point in direction () block with the point toward (rocket) block, which ensures the game always starts with a UFO aimed at the rocket, and I increased the move block value to 20. The player better not sit idle for long.

The move block specifies the number of steps (pixels, really) to move across the stage. A higher value will increase the speed of the UFO. Adjusting the value in the move block is another point of customization you can make to the UFOs.

Notice with the UFOs that I am not calculating any directions, except at the start of the game. The sprite does bounce off edges, but that just kicks the UFO back in the opposite direction at the same speed. The scripts for the UFOs and rocket illustrate the difference between speed and velocity in Scratch programming.

Sense a Collision

The second stack of blocks in Listing 4 senses whether a UFO touches the rocket; if the two sprites touch, the UFO sends the broadcast message rocket hit. Hitting a UFO will cause the rocket to explode into flames and go back to its starting position.

The rocket hit broadcast message is received by the rocket, as seen in Listing 5. To give the illusion of exploding, the switch costume by block displays the explosion costume, which I created by copying and editing one of the existing rocket costumes. If working with the image editor is not your thing, you can download my sample project or hack out something that looks like flames. The change () effect by () in the Looks palette could provide an alternative block and includes options to pixelate or to create a mosaic of the sprite.

Listing 5: Coding the Rocket Hit

After exploding the rocket, the script sends a broadcast message start over. When the rocket receives this message, the script resets the sprite to the starting position, which you've already seen in the velocity script in Listing 3.

By using the start over broadcast both to start the game and when the rocket collides with a UFO, I can avoid duplicating scripts. In Scratch 2.0, I could use a custom block to create a procedure and accomplish the same thing. However, I'm working with Scratch 1.4 on the Pi, so the creative use of broadcast blocks can give some of the same functionality as custom blocks.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content