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 thatyou 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 thegame 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 valuein 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” blockfrom the motion menu. When this stack is run now, the player spritewill face the mouse pointer and move forward. The sprite needs to move towards the mousepointer 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 aroundthe stage. The sprite should follow the mouse pointer. If you want to slow down or speedup 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 issuethat you didn’t expect, an important first step is to think about what you wanted theprogram to do, and compare it to what happened. Here you wanted the sprite to point towardsthe 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 stoppedpointing 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 isnot 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 menuand select the ‘touching’ block. Change the value to “mouse pointer.”
This now reads: “If not touching the mouse pointer, then point towards the mouse pointerand move forward 10 steps.” Otherwise, do nothing.
Click on this block stack to try it out. You should see that the player sprite only followsthe mouse pointer while it’s not touching the mouse pointer. When the two touch, thesprite 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 ofyour CS First Guru.