Lost in space

By Russell Barnes. Posted

Program your own animation of a spaceship heading for Earth, using a scaling effect to make the ship smaller as it moves into the distance

In this tutorial, you’ll be creating an animation sequence which, perhaps unexpectedly, involves a rotating space monkey! This project will show you how to move, rotate, and scale sprites. This is something which will also come in very handy for other projects and games. So, start a new Scratch project and get ready to do some animating. If you need any help navigating the Scratch menus, refer to our Scratch Essentials book.

 Learn to Code with Scratch takes you through all the Scratch basics!

STEP-01 Prepare your artwork

After deleting the cat (right-click and Delete), it’s time to import a new stage background and sprites. Let’s begin by creating our space scene, changing the stage to a field of stars: click Stage in the Sprite List (bottom-right), select the Backgrounds tab (top-middle), then click Import and navigate to ‘stars’ in the Nature folder. Since none of the sprites used in this project are in the Scratch 1.4 library, you should download them. First, let’s import the Earth and Spaceship sprites: for each, click the star/folder above the Sprite List, then navigate to the folder where you’ve stored your sprites.

STEP-02 Move the spaceship

Click the Spaceship sprite in the Sprite List to select it, then click the Scripts tab. Listing 1 shows the script you need to add to this sprite to make it move. First, we point it upwards (point in direction 0) and tell it to go to x: -150 y: -150, near the bottom-left corner. After waiting one second, we use the handy point towards Motion block to point it at our Earth sprite. We then use a repeat loop to keep moving it towards Earth, two steps at a time.

 Listing 1

STEP-03 Scale the ship

To simulate the spaceship moving further away from us, we need to gradually reduce its size as it moves towards Earth. This is easily achieved by adding a single extra block to its existing script. Click the Looks button in the top-left pane, drag a change size by block and drop it just below your move 2 steps block, within the repeat loop. Change the 10 of the change size block to -0.5. The code should look like Listing 2. Now, try clicking the green flag to see your space rocket hurtle towards Earth, getting smaller all the time.

 Listing 2

STEP-04 Add a space monkey

Now let’s add a few extra features to our space scene. For a bit of fun, we’ll add a floating monkey who’s lost in space. Click on the star/folder icon again and navigate to your Lost In Space sprites folder, then select Monkey. As with any sprite, you can adjust its size using the Grow/Shrink sprite icons above the stage. Now let’s give our monkey a space helmet! Select it in the Sprite List, then click the Costumes tab and the Edit button. In the Paint Editor, select the Ellipse tool, the outline option (on the right) below the tools, then a yellow colour from the palette. Now draw a yellow ellipse around the monkey’s head for a helmet. To make things more interesting, we’ll make our monkey spin around by adding the simple looping script in Listing 3.

 Listing 3

STEP-05 Bounce and shine

Finally, we’ll add a shining star and bouncing rock. Import them both from your Lost In Space sprites folder, then position and scale them on the stage to your liking. For the star, add the code from Listing 4 (two repeat loops inside a forever one), to repeatedly scale it up and down in size. Add the Listing 5 code to the rock to get it moving, including a special block to make it bounce off whenever it reaches the edge of the stage.

 Listing 4

 Listing 5

STEP-06 Taking it further

Your animation should look pretty cool by now. Try playing around with various parameters to see how they affect the speed, movement, and scaling of the objects. You could also add your own touches, such as using a change color effect block to give the spaceship a fancy disco-light effect as it moves!

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.