Level 1Unit B · Session 9
9

Unit B · Telling a computer what to do

Moving it yourself

What we are making: a sprite you drive with the arrow keys. From here on, the person using your program is part of it.

The stage is a grid

Everything on the screen has two numbers: how far across it is, and how far up. Across is called x. Up is called y. The middle of the stage is x 0, y 0.

Go right and x gets bigger. Go left and it gets smaller, and past the middle it goes negative. The stage runs from about −240 to 240 across, and −180 to 180 up.

New words
xHow far across. Bigger is further right.
yHow far up. Bigger is further up.
change x byMove sideways by that much, from wherever you are.
go to x: y:Jump straight to that exact spot.
key pressed?A question: is that key being held down right now?
💡 Tip
Drag your sprite around the stage and watch the numbers change. Scratch shows x and y just above the sprite list, and they update as you drag. Thirty seconds of doing that teaches the grid better than any explanation.
📷
images/l1-s9-xy-readout.png
Screenshot: the x and y boxes above the sprite list, with the sprite somewhere off-centre

Do it — the simple way first

One key, one script
when right arrow key pressed
change x by 10
📷
images/l1-s9-script-one-key.png
Screenshot: the one-key script. Build this first
  1. Build that. Press the right arrow. The sprite steps right.
  2. Add three more scripts, one for each arrow. Left is change x by −10. Up is change y by 10. Down is change y by −10.
  3. Try to move diagonally by holding two keys. It does not work properly, and that is the problem the next part fixes.

Do it — the smooth way

Those separate scripts feel jerky, because Scratch waits a moment before repeating a held key — the same pause you get holding a letter down in a word processor.

The fix is the shape you met last session: forever with if inside it. Now the program checks the keys itself, as fast as it can, instead of waiting to be told.

Smooth, and diagonals work
when green flag clicked
forever
if right arrow key pressed ? then
change x by 10
if left arrow key pressed ? then
change x by -10
📷
images/l1-s9-script-four-keys.png
Screenshot: all four directions as four ifs inside one forever loop — copy it exactly
  1. Rebuild it this way, with all four directions inside one forever loop.
  2. Press two keys at once. It moves diagonally now.
  3. Change 10 to 3. Then to 25. Find the speed that feels right to play.
What you seeWhyWhat to do
The sprite does not move at allThe green flag script is not running.Click the green flag. The forever version needs it; the key-pressed version does not.
It moves in jerksYou are still using the four separate scripts.Use forever with if inside. That is the whole point of this session.
It goes off the edge and disappearsNothing is stopping it.Scratch keeps a sliver visible. Drag it back, or add an if that checks x.
It moves diagonally on its ownTwo scripts are fighting.Delete the old separate key-pressed scripts once the forever one works.

What you learned

  • Everything on the stage has an x and a y.
  • change moves from where you are; go to jumps to an exact place.
  • Forever plus if is how a program watches for something continuously.
  • Your program can now be controlled by whoever is using it.
Challenge optional — only if you finish early
  • Make the sprite move faster the longer you hold the key down.
  • Make it leave a trail behind it using the pen blocks.
8. If this, then that