Scratch 04 · Make a keyboard-controlled character

A fox on a coordinate plane beside four arrow keys.

Scratch lesson 4 of 10 · Beginner · About 25 minutes

Build a little exploring game and steer your character with all four arrow keys. Learn how Scratch uses x and y to describe where things are on the Stage.

What you’ll make Make the four arrow keys move a sprite in the correct directions and send the sprite back to the same starting point each time.

Get ready

Open the Scratch editor (opens in a new tab) and start a new project.

  • Open a fresh Scratch project on a computer with a keyboard. Keep the starting sprite or choose another small character. Rename it Explorer in the sprite information below the Stage.
  • Pick a simple backdrop or leave the Stage plain while you build. The middle of the Stage is x = 0, y = 0. The x position changes as you move left or right. The y position changes as you move up or down.
  • Select Explorer before building. Drag the script blocks onto the coding area. Place the different hat blocks in separate stacks so each one can respond to its own event.

To keep your project, choose File → Save to your computer in Scratch.

Press an arrow. Trigger a key event. → Change x or y. Move along one axis. → Try the next key. Test all four directions.
Project flow · Press an arrow → Change x or y → Try the next key

Build it in Scratch

Choose the sprite named above each stack. The colours help you find the block categories. Indented rows sit inside the block above. Follow the end marker to see where each loop or condition closes.

1. Put Explorer at the starting point

Connect a Motion go to x: y: block to the green-flag block. Type zero in both boxes. Click the green flag, drag Explorer somewhere else on the Stage and click the flag again. You should see it return to the centre.

Block stack for Explorer
when green flag clicked
go to x: (0) y: (0)

2. Wire the right and left arrows

In Events, drag out two when [space] key pressed blocks. Change their menus to right arrow and left arrow. Give each hat one Motion change x by block. A positive number moves right. A negative number moves left. Click the Stage and tap each key once to compare.

Block stack for Explorer
when [right arrow] key pressed
change x by (12)
when [left arrow] key pressed
change x by (-12)

3. Add up and down

Make two more separate event stacks. Use change y by for both. Pressing up increases y, and pressing down decreases y. Watch Explorer as you test. Neither of these scripts needs to change its x position.

Block stack for Explorer
when [up arrow] key pressed
change y by (12)
when [down arrow] key pressed
change y by (-12)

4. Add a quick return key

Add a new key event and choose r. Reuse the starting coordinates from the green-flag script. Now somebody playing your game can reset their position even if Explorer has wandered to the edge of the Stage.

Block stack for Explorer
when [r] key pressed
go to x: (0) y: (0)

Test your project

  1. Click the flag. Tap right twice and up once. The sprite information should show x = 24 and y = 12 if each tap registered once.
  2. Tap left twice and down once. Explorer should return to the centre.
  3. Move in any direction and press R. Click the flag again to make sure both ways of resetting work.
  4. Try each arrow on its own. If a key behaves oddly, compare its menu and number with the block diagram.

If something goes wrong

An arrow moves Explorer the wrong way.
Use x for left and right and y for up and down. Left and down need a minus sign.
Only one direction works.
Check that each key has a separate Events hat and the menu names that key.
Arrow keys scroll the webpage.
Click the Stage before trying the arrows. Make sure the cursor has left any box where you were typing a number.

Make it your own

  • Make Explorer reach four imaginary checkpoints. Start at the centre and plan the fewest taps to get to x = 36, y = 24.
  • Change all four movement amounts to 6 or -6 and compare how many taps it takes. Keep the minus signs on left and down.

What you’ve learned You used four keyboard events to change two coordinates independently. A reset gives players a reliable way to start again.