Page 218 - Learn To Program With Scratch
P. 218
wins the game. Otherwise, the Player sprite returns to its initial position,
and the player can try again. When the green flag icon is clicked to start
the game, the Player sprite runs the script shown in Figure 8-15.
Let the Helper sprite assign random locations for
the Player and the Target sprites.
Let the Target sprite move to its assigned location.
Give the player multiple chances to hit the target.
Set initial position and direction of the Player
sprite and clear the Stage from drawing pen
marks.
Let the Helper sprite get the player’s answers
(angle and distance).
Move the Player sprite based on the provided
answers.
Figure 8-15: The Player sprite script that runs when the green flag icon is clicked
The script broadcasts NewGame to instruct the Helper sprite to assign
new locations for the Player and the Target sprites u. The Helper sprite runs
a simple procedure (not shown) that updates the following five variables
with random numbers that will keep Player and Target visible (and separated
by a certain distance) on the Stage:
XPlayer and YPlayer The x- and y-coordinates of the Player sprite
XTarget and YTarget The x- and y-coordinates of the Target sprite
initAngle The initial direction of the Player sprite
Once the script has new positions for Player and Target, it broadcasts
StartGame to move the Target sprite to its new location v. (The script for
the Target sprite is not shown here.) Then the script enters an infinite loop
to give the player multiple chances to hit the target w. The loop will be ter-
minated by a stop all command (in the CheckAnswers procedure) when
the player hits the target.
Each loop iteration sets the initial position and direction of the Player
sprite and clears all pen marks from the Stage x to delete the trace left
behind from the previous guess. The script then broadcasts GetAnswers y,
and in response, the Helper sprite prompts the player to enter an answer, as
shown in Figure 8-16. The Helper sprite then splits the answer into two parts
(before and after the comma) and updates angle and distance accordingly.
Follow along with the comments on Figure 8-16 to see how this script works.
The Player sprite is then moved, with its pen down, as directed by the
player z. This leaves a visual trace of the movement route that the player
can use to refine his or her estimate for the next round.
196 Chapter 8
www.it-ebooks.info

