Week 25 · Unit 10: Be a game developer · 3 lessons
2
Build a game step by step
Computing 4: Unit 10, pp. 138–151
GoalPlan a game with several sprites, build it one sprite at a time and trace the algorithms for each sprite.
New words
sprite · a character or an object in a Scratch projecthat block · the first block of a script, which says what starts itplan table · a table that says what each sprite does and when
Explanation
A game with several sprites is easier to build in small steps. First, make a plan table. For each sprite, write what starts it, what it does, whether it needs a sub-routine and whether it needs a loop. Then build the first sprite, run the program and test it. Only then add the next sprite and its blocks, and test again. A sprite can start in different ways: the green flag, a key or a click. Each way has its own hat block. If a bug appears, you know it is in the part you added last.
make a plan table→add one sprite→add its blocks→run and test→add the next sprite
Worked examples
Sprite
Starts when
What it does
Sub-routine
Loop
crab
clicked
steps sideways, then says “Missed me!”
sidestep
none
starfish
green flag
goes to its place, then twirls and waits
twirl
repeat (5)
A beach game has two sprites. The plan table says what each one does. The crab is clicked by the player. The starfish works by itself after the green flag.
The starfish. It starts facing 90 (right). Each twirl turns it 90 degrees clockwise, makes its colour change by 30 and plays a pop. There are 5 twirls, so it turns 5 × 90 = 450 degrees. 90 + 450 = 540, and 540 − 360 = 180, so it ends facing 180 (down). The colour effect is 5 × 30 = 150. The program takes 5 × 1 = 5 seconds.
The crab. The sub-routine sidestep points the crab left, moves it 60 steps and points it right again. The crab starts at x = 120. Each click moves it 60 to the left, so the three clicks in the picture (labelled 1, 2 and 3) take it to 60, 0 and −60.
Class activity
Plan a game (with family): choose a theme, such as a pond, a street or a jungle. Choose three sprites. Draw a plan table with the columns sprite, starts when, what it does, sub-routine and loop. Fill in one row for each sprite. Then act the game out with paper cut-outs on a table: one person is the player, and the other moves the paper sprites as the plan says. Does the plan make a game that is fun to play?
Practice
1
A turtle starts when the green flag is clicked. It goes to x: −150, y: −80. Then it plays a sound, moves 20 steps and waits 0.5 seconds, 4 times in all. Fill in the plan table row.
Sprite
Starts when
What it does
Sub-routine
Loop
turtle
?
?
?
?
Starts when: the green flag. What it does: goes to its place, then plays a sound, moves 20 and waits. Sub-routine: optional, for example crawl. Loop: repeat (4).
2
A snowflake program: when the green flag is clicked, set size to 100 %, then repeat (4): run spin. The sub-routine spin turns ↻ 45 degrees, changes the size by −10 and waits 0.5 seconds. The sprite starts facing 90. Work out its final direction, size and the time taken.