Page 246 - Scratch Programming Playground
P. 246
Add the new walk definition.
The value of steps is used
to make the cat walk . . .
. . . and to prevent the cat
from walking up steep slopes.
We want the cat to walk six units, as we did earlier, so
we use -6 and 6 in the walking script when we call walk.
In the define walk block, the steps input block is used in
the change x by blocks. this makes the code more compact,
because we can use the same script for moving the cat to the
left (with the walk -6 block) or to the right (with the walk 6
block).
the code in the repeat until loop uses the ground lift vari-
able to determine if the slope is a walkable slope or a wall that
should block the Cat sprite’s progress. the ground lift variable
starts at 0 and changes by 1 each time the repeat until loop
lifts the Cat sprite’s y position by 1. this loop continues looping
until the sprite is no longer touching the ground or ground lift
is equal to 8.
If ground lift is less than 8, then the slope isn’t that steep.
the sprite can walk up the slope, so the define walk script
doesn’t have to do anything else.
But if ground lift = 8, the repeat until loop stops looping.
this code means “the sprite has been lifted up by 8 but it’s
still touching the Ground sprite, so this must be a steep slope.”
220 Chapter 9

