ST EK List:
No EK's found.

Sprite Following the Mouse

In this lab, you will program two sprites. One sprite will follow your mouse. The other sprite will chase the first. When they meet, they will have a short conversation.

Puppy chase

On this page, you will program the first sprite to follow your mouse and program the second sprite to point in the direction of the first sprite in preparation for following it.

  1. "U1L5-SpriteChase"Create a new project called U1L5-SpriteChase
  2. Talk with Your Partner
    mouse x and mouse y report the mouse's location on the stage. They are in the Sensing palette.
    Read and think about this script before you build it. What do you expect it to do?
    when green flag clicked{forever{go to x:(mouse x) y:(mouse y)}}
  3. Now build the script and run it. Move your mouse around the stage while the program is running.

    when green flag clicked The block at the top of the script is called a hat block. Its shape emphasizes that it can be used only at the beginning of a script. Hat blocks don't say what the script should do; they say when it should do it. Hat blocks say what event should start the script running. In this case, the script starts when the green flag button is clicked.

    To stop this script, click the red stop sign: stop button

  4. Talk with Your PartnerDoes the program work the way you expected?
  5. Create a second sprite by clicking on the add a new turtle sprite button just below the stage.  
    Then use the point towards block to make it point towards Sprite. The animation below shows how. getting a new sprite
  6. The new sprite, named Sprite(2), appears on the stage. It's brand new, so it has no scripts. The buttons beneath the stage let you select the sprite you want to program.
Pair Programming Swap
  1. Change your script for Sprite(2) so that once the green flag is clicked, Sprite(2) will point towards Sprite forever.
    pointing towards Sprite forever
  2. While a script is running, its border lights up. If the running time is short, you may not notice the brief flash.
  3. Click green-flag button and check that your script does what you intended:
    • Sprite always follows your mouse when you move your mouse around.
    • Sprite(2) stays in one place, but always points toward Sprite.
  4. Both sprites' scripts use Whengreen-flag buttonclicked, so both scripts run when green-flag button is clicked.
Now Is a Good Time to Save