Scratch Tips: Randomly Animating a Sprite

Scratch is a powerful tool that allows your student to fully explore programming logic.  Where other tools guide you along the way, Scratch gives creative freedom.  We typically use this tool during the second half of our sessions to reinforce concepts and to see what the kids come up with.

This short tutorial will demonstrate how to randomly animate a sprite.  It can be used to create enemies, obstacles, or just background graphics for your Scratch experience.

What is a Sprite?

First off, a ‘sprite’ is Scratch’s terminology for any object in which you can apply programming logic. They are located in the bottom middle portion of your Scratch interface.  Typically students really enjoy adding new sprites as it allows them to choose their own characters and objects.

ScratchTip1

For this example, we’ve created a dog sprite that our cat will need to avoid.   The dog will randomly move around the screen making it difficult for our cat to reach the ball.

Step 1: Move the Dog

Since the dog will move as soon as we start we want to use the ‘when clicked’ event from our control blocks.  This tells us that when we click the flag above our animation, execute the code immediately under this block.  Drag the ‘when clicked’ block to your workspace on the right.

ScratchTip2

Step 2: Move the Dog “Forever”

Now we want to tell the code that the dog should move forever.  We don’t want him to stop until we stop the game.  Drag the ‘forever’ block from the ‘Control’ section immediately under ‘when clicked’ in your workspace.  It should look like:

ScratchTip3

What do we want the dog to do forever?  Move around, right?  There’s a really cool block in the ‘Motion’ section called glide.  It allows you to specify how many seconds and to where your sprite should glide.  Drag the glide block inside the forever block in your workspace.

ScratchTip4

Step 3: Animate

That’s great but we’re not quite finished yet.  Why doesn’t this achieve our goal?  Well, we’re telling our sprite to glide for 1 second to a specific x,y location.  Once they get there they’ll just stay (at -94x, 40y in this example).  We want to make the movements completely random in this example.

Under ‘Operators’ you’ll find a ‘pick random 1 to 10’ block.  You can use this to create the desired effect.  We’ll need three random generators: 1) number of seconds; 2) x coordinate; and 3) y coordinate.

Number of seconds is easy.  We’ll use the default 1 to 10 numbers in the random generator.  Drag this block into the white area around the number ‘1’ in the glide block in our workspace.  It should look like:

ScratchTip5

For the x- and y-coordinates, we’ll have to modify the numbers in the random generator.  Our x-axis in Scratch runs from -240 to 240.  That’s what we’ll put in and then drag into the x: area of the glide statement.  Our y-axis runs from -180 to 180.  Use the same process for it.  Your finished workspace should look like:

ScratchTip6

You can click the start flag at the top and your sprite should be randomly moving around the screen at different speeds.

Try it Out

The full example can be viewed online here:  https://scratch.mit.edu/projects/84587392/

Click ‘See Inside’ to see all of the code.

Have fun!