- Practice
- About 14 min
- 5 practice questions
Scratch: movement with the green flag and loops
Build a script that starts with the green flag, places a sprite with go to x: 0 y: 0 and moves it, read x and y positions on the stage, and use repeat loops to shorten scripts.
If something looks wrong, please tell us.
By the end of this lesson you can
- Start a new project and begin a script with 'when green flag clicked'.
- Use 'go to x: 0 y: 0' and 'move 100 steps' and predict where the sprite ends up.
- Read x and y positions on the stage.
- Use a repeat loop to do the same thing many times.
Now we make a sprite move. The textbook's first program starts a project, places the cat at the centre of the stage and moves it forward. Then we look at loops, which repeat blocks so that you do not have to drag the same block again and again.
Building the movement script
- Start a new project with File → New (in the online editor, File → New may open a fresh project directly) and give it a name when you save.
- From the Events category, drag when green flag clicked to the script area. The textbook reaches this through the Script menu; in newer versions Events is a category in the Code tab.
- From Motion, drag go to x: 0 y: 0 and snap it under the green-flag block.
- From Motion, add move 10 steps underneath and change 10 to 100.
- Click the green flag above the stage to run the program. Its exact position depends on the version.
The first movement script
- when green flag clicked Starts the script
- go to x: 0 y: 0 Sprite jumps to the centre
- move 100 steps Sprite moves 100 steps in the direction it faces
Positions on the stage
Scratch places sprites using x and y numbers, like a graph. The centre of the stage is x: 0, y: 0. x grows to the right and becomes negative to the left; y grows upward and becomes negative downward. In the standard Scratch stage, x runs from −240 to 240 and y from −180 to 180. A new sprite faces right (direction 90), so 'move 100 steps' from the centre takes it to x: 100, y: 0.
Worked example 1: predict the position
Question: A sprite facing right runs: go to x: 0 y: 0; move 100 steps; move 50 steps. Where does it end? Then, if 'go to x: 0 y: 0' is removed and the flag is clicked a second time, where does it end? Working: first run: 0 + 100 + 50 = x: 150, y: 0. Without the go-to block, the second run starts from x: 150 and adds 150 more: x: 300, which is past the right edge (240), so the sprite is pushed against the edge of the stage. This is why the 'go to x: 0 y: 0' block is useful: it resets the sprite every time.
Loops
A loop repeats a set of blocks. In the Control category, repeat 10 runs the blocks inside it the given number of times, and forever keeps running them until you press the red stop sign. Loops make scripts shorter and easier to change.
What a repeat loop does
- Run the blocks inside
- Count one more time
- Check: done the set number? If not, go round again; if yes, carry on below the loop
Then back to step 1: Run the blocks inside
Worked example 2: walking a square
Question: A sprite at x: 0, y: 0 facing right runs: repeat 4 [move 50 steps, turn 90 degrees]. What path does it follow and where does it finish? Working: each time round it moves 50 steps and turns a quarter turn. Four quarter turns = 4 × 90 = 360 degrees, a full turn, and four equal sides make a square of side 50 steps. It finishes back at x: 0, y: 0, facing right again. Without the loop you would need 8 blocks; with it you need 3 (the loop and the two blocks inside).
For a walking animation, put move 10 steps, next costume and wait 0.1 seconds inside a repeat loop: the sprite moves a little and changes costume each time, so it seems to walk. This is the frame idea of lesson 2 done by the computer.
when green flag clicked → go to x: 0 y: 0 → move 100 steps. Centre = (0, 0); x to the right, y upward. repeat n runs blocks n times; forever runs until stopped.
Key takeaways: scripts start with an event block such as the green flag; Motion blocks place and move sprites using x and y; loops repeat blocks so that animations stay short and tidy.
Check your understanding
-
1. Which block should be at the top of a script that runs when you click the green flag?
- move 10 steps
- go to x: 0 y: 0
- when green flag clicked
- play sound
Show answer
Answer: C. 'When green flag clicked' is an Events block that starts the script; the other blocks are snapped beneath it.
-
2. A sprite facing right at x: 0, y: 0 runs 'move 100 steps'. Where is it now?
- x: 0, y: 100
- x: −100, y: 0
- x: 100, y: 100
- x: 100, y: 0
Show answer
Answer: D. Facing right means moving along the x direction: 0 + 100 = x: 100, while y stays 0.
-
3. Why is 'go to x: 0 y: 0' placed before 'move 100 steps'?
- To reset the sprite to the centre each time the program starts
- To play a sound
- To delete the sprite
- To change the costume
Show answer
Answer: A. Without it, each run would start from wherever the sprite stopped last time, and the sprite would soon reach the edge of the stage.
-
4. A sprite runs repeat 3 [move 20 steps]. How far does it move altogether?
- 20 steps
- 60 steps
- 23 steps
- 3 steps
Show answer
Answer: B. The loop runs its block 3 times: 3 × 20 = 60 steps.
-
5. Which block keeps running its inside blocks until the stop sign is clicked?
- repeat 10
- go to x: 0 y: 0
- forever
- when green flag clicked
Show answer
Answer: C. 'Forever' repeats without end until the program is stopped; 'repeat 10' stops after 10 times.
Sign in to keep track of the lessons you finish.