Sprite Following a Sprite

Brian, the solution file needs to be updated for the new version of TELL (see relevant piazza post), and the problem that includes TELL (the solutions appear to use broadcast instead). --MF, 11/6/17

On this page, you will change the script so that the sprites don't move forever, but only until they touch.

  1. Change Follower's code to point towards Leader only until it is touching Leader. (The following instructions show how.)
    1. Select Follower by clicking its button below the stage.
    2. Replace the forever block with a repeat until block.
    3. Drag touching ()? into the hexagonal space in the repeat until block. Set it to repeat until it is touching Leader.
  2. Do the same for Leader. Change its code to follow your mouse only until it is touching Follower.
  3. Test your program to see if it does what you want:
    • When you click green-flag button, Leader (the gray one) should follow your mouse and Follower should keep turning to face Leader.
    • If you bring Leader close enough to touch Follower, both sprites should stop moving.
  4. So far, Follower points toward Leader, but doesn't move toward it.
    1. Add a line of code to Follower's script so that it chases Leader. Here's the idea:
      Fill the input slot in the move block with a small number so Follower doesn't catch Leader too quickly.
      when green flag clicked:
repeat until (touching (Leader)?)
{
    point towards (Leader)
    move () steps ← Comment: Type a small value in this empty input slot.
}
    2. AAP-2.K.5
    3. What happens if the sprites are already touching? That is, what happens if you drag one sprite on top of the other and then click the green flag? Why?
    4. AAP-2.K.4
    5. Experiment. What happens if the input to move is 0?

    When a program keeps running forever, that's called an infinite loop.

AAP-2.K.3
The language used on the AP Exam doesn't allow spaces in names of inputs (such as number of fish) or in programmer-defined procedures (such as mouse y, which isn't built into their language). So this example translates them to
numFish
and
MouseY()
.
The reason for the
()
or box after
MouseY
is that
MouseY()
is a procedure call even though it doesn't take any inputs.

The script
repeat until (mouse y < 0) {say (number of fish)}
would be written as

REPEAT UNTIL(mouseY() < 0)
{
    DISPLAY(numFish)
}
or a gray rounded rectangle containing all of the following: on the first line, first the words 'REPEAT UNTIL' in all caps and then a smaller gray rounded rectangle containing 'mouseY () < 0' and on the second line, a white rounded rectangle with a smaller white rounded rectangle inside that contains first the word 'DISPLAY' in all caps and then a smaller white rectangle containing 'numFish'

Remember, you don't need to learn to write the made-up language used on the AP exam. You just have to be able to read and answer questions about it.

  1. Test your program a few times.
  2. Pair Programming Swap
  3. Right now, when the sprites meet, they just stop. Make them have a conversation when they stop. You can do that by adding code like this to Leader's script. Make up your own conversation. You can use any language you can type. Here's an example using several languages.

    Leader does things itself. It also tells Follower what to do and when to do it. This code puts Leader in charge of Follower.
    CRD-2.B.2, AAP-2.B.2, AAP-2.B.6

    A code segment is a sequence of connected instructions that carry out a purposeful action, such as the one pictured on the left, which animates a conversation. The instructions in the code segment are carried out in order, from top to bottom.

    partial script of a conversation between two sprites:
‘partially cut off script’
say (¡Hola!) for (2) secs
tell (Follower) to (say (Hello!) for (2) secs)
say (Ou vle manje yon bagay?) for (2) secs
think (Would you like something to eat?) for (2) secs
tell (Follower) to (say (Спасибо. Я очень голоден.) for (2) secs; think (Thank you. I’m very hungry.) for (2) secs)
glide (1) secs to x: (200) y: (30)
tell (Follower) to (glide (1) secs to x: (200) y: (30))
To prevent the two sprites from being stuck to each other unable to move, the sprites will need some space between them and some time apart before the chase starts. Blocks such as these may help.
go to x:(-200) y:(-50); wait (1) secs & go to x:(200) y:(-50); wait (1) secs
Save your work
  1. Give the sprites costumes.
    You can use a picture from the Internet by dragging the picture into the Snap! window.
  2. Change the background on the stage.
    There are instructions in the If There Is Time section on Unit 1 Lab 2 Page 3: Customizing and Debugging.