Scratch project- move!

We’re going to program a sprite to move using the arrow keys!

Instructions

Open Scratch (https://scratch.mit.edu).

If you have an account, sign in. If you don’t have an account, you should create one so your work is automatically saved.

Select “Create” at the top of the page.

The Scratch editing window will open.

Scratch always starts new projects with one sprite, the cat. We’re going to write some code that will let us use the arrow keys to move.

Here’s what our code will do: When we start it, it will constantly check if we are pressing any of the arrow keys. If we are pressing an arrow key, the code will either move the cat forward, move it backward, or turn it left or right.

On the left side of the screen, select the yellow circle labelled “Events.” The Events blocks will appear.

Drag this block into the code space:

Notice that the block is round on the top and has a notch on the bottom. Whenever a code block has a notch, we can attach another block.

Select the orange circle labelled “Control”. The control blocks will appear.

Find the block that says “Forever” and drag it to connect to with the flag block in the code window.

Notice that the forever block doesn’t have a notch at the bottom. This block will loop through all of the code until with stop the program.

Now that you have a loop, let’s put in some controls. There are a lot of blocks to add, but they all tell you exactly what they are doing.

Our first set of blocks will check if we are pressing the right arrow. If we are pressing the right arrow, then the cat will move forward.

Find the “if” block and put it inside the forever block. The forever block will get bigger so the other block will fit.

Now we need to sense if the right arrow key is being pressed. Go to the sensing blocks (the cyan/light blue circle) and add the shape that says “key space pressed?” to the “if” block.

Click where the block says “space” and change it to “right arrow”.

Now, whatever commands inside the “if” block will be be followed when you press the right arrow.

Switch to the “motion” blocks. Drag the block that says “move 10 steps” into the if statement.

You have written your first program! Press the green flag above the stage to run it. Now when you press the right arrow, the cat will move forward!

To make the cat move backward, add another if statement, but have it check the left arrow and change the move to negative steps. I also changed the steps to 5 and -5 to make the cat move slower. Make sure you put it UNDER the first “if” block, not inside it!

Now our cat moves forward and backward. The next step: add turns!

That’s it!