Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

3. Programming the Player Sprite

Transcript

In this screencast, you will program the player sprite to follow the mouse pointer.

Watch as this screencast demonstrates how to do it, then try on your own.

You could make the sprite follow the mouse with the “go to mouse” pointer block that you used in the maze game. But, that would make it too easy to avoid the chaser sprites, because the player sprite will always move as fast as the mouse pointer.

Instead, program the player sprite to point towards the mouse and move forward ten steps.

This will make the sprite follow the mouse at a set speed each time the loop runs.

This will make the sprite look like it is chasing the mouse pointer. Doing this will make the game more challenging and fun. To start, click on the player sprite.

Then, click the motion menu and drag out the “point towards” block. Change the value in the drop-down menu to “mouse pointer.”

Now, when you run this block, the sprite will point towards the mouse pointer.

You need to make the sprite move forward after it points towards the mouse pointer.

To make that happen, select a “move 10 steps” block from the motion menu. When this stack is run now, the player sprite will face the mouse pointer and move forward. The sprite needs to move towards the mouse pointer forever. So, click the control menu and drag out a “forever” block.

You can test this by clicking on the block stack and moving the mouse pointer around the stage. The sprite should follow the mouse pointer. If you want to slow down or speed up the sprite, experiment with a few different values in the “move 10 steps” block.

Tinker with this value until you create a speed you like.

You may have noticed that when the player sprite touches the mouse pointer, it starts shaking.

When you’re faced with bug, or an issue that you didn’t expect, an important first step is to think about what you wanted the program to do, and compare it to what happened. Here you wanted the sprite to point towards the mouse pointer, move ten steps, and repeat this until it reached the mouse pointer.

However, when the sprite got to the mouse pointer, it never stopped pointing towards or moving, making it look like it was shaking.

To fix this bug, you can add an "if statement" that runs this code only if the sprite is not touching the mouse pointer. To do this, click on control, and select an "if statement". Place this "if statement: inside your "forever" loop, and put the motion blocks inside it.

Click on operators and select the “not” block. Then, go to the sensing menu and select the ‘touching’ block. Change the value to “mouse pointer.”

This now reads: “If not touching the mouse pointer, then point towards the mouse pointer and move forward 10 steps.” Otherwise, do nothing.

Click on this block stack to try it out. You should see that the player sprite only follows the mouse pointer while it’s not touching the mouse pointer. When the two touch, the sprite stops moving until the mouse is moved again.

Finally, add a “when flag clicked” event to your program.

Now when the flag is clicked, both the player and chaser sprite start to move.

In the next screencast, you will add a timer to keep score for this game.

Remember, if you have any questions, ask a neighbor or use your sticky to get the attention of your CS First Guru.

arrow_backward Back
Next arrow_forward
Instructions
  1. Make the player sprite point towards the mouse pointer, then move forward.
  2. Make the player sprite keep following the mouse pointer forever.
  3. BUG FIX: Only make the sprite move towards the mouse pointer if it is not touching the mouse pointer.
  4. Make the player sprite move when the green flag is clicked.