Skip to content
https://storage.googleapis.com/files.cs-first.com/game-design/game-icon%402x.png

3. Programming the Player Sprite

Transcript

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

You can make the sprite follow the mouse with the go to mouse pointer block.

But that would make it too easy to avoid the chaser sprite, 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.

Making the sprite look like it's chasing the mouse pointer.

This will make the game more challenging and fun.

To start, click on the player sprite.

Then, click motion and drag out a point towards block.

Change the value in the drop down menu to mouse pointer.

Click to run this block.

Great! The sprite points towards the mouse pointer.

Make the sprite move forward after it points towards the mouse pointer be adding a move block.

Click this block stacked to task.

Awesome!

The player sprite faces the mouse pointer and moves forward.

Next, program the sprite to point towards the mouse pointer forever.

Click control and drag out a forever block.

Test your code by clicking on the block stack, then 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 block.

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

There's a bug in the program.

When the player sprite touches the mouse pointer, it starts shaking.

When the sprite goes to the mouse pointer, it never stops pointing towards, or moving, making it look like it's shaking.

This sprite should point towards the mouse pointer, move ten steps, and repeat this until it reaches the mouse pointer.

To fix this bug, 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 foreword ten steps.

Otherwise, do nothing.

Click this block stack to try it out.

Great! The player sprite follows the mouse pointer only while its 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.

Click the flag to test.

Awesome! Both the player and chaser sprite start to move.

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

Now it's your turn!

Program the sprite to follow the mouse pointer only if its not touching the mouse pointer.

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.