Two-wheel MIEO beside a laptop where a cartoon fish chases the mouse pointer
Games & AnimationBeginner20 minOn screen

Follow Mouse

A sprite that chases your mouse pointer around the stage

What you'll build

In this project a fish swims after your mouse pointer wherever it goes. Move the pointer and the fish turns to face it and follows, always the right way up. When it reaches the pointer it changes colour. Behind the scenes a forever loop checks where the pointer is many times a second, which is exactly how robots and games keep reacting to the world around them.

What you'll learn

  • How a forever loop keeps checking and moving, many times a second
  • Turning a sprite to face something with “point towards”
  • Measuring with “distance to” and comparing numbers with “>”
  • Why rotation style matters when a sprite turns to the left
  • Changing a sprite's colour with a graphic effect

You'll need

Set up the stage

  1. Open Robolab and leave “No device selected” as it is, because this project runs on screen.
  2. Click the “Choose a Sprite” button under the stage and pick Fish from the library.
  3. This game stars the fish, so delete Roby: right-click Roby in the sprite list and choose delete.
  4. Click the “Choose a Backdrop” button and pick Underwater 1.

Code it

  1. Click the Fish sprite. From Events, drag out “when green flag clicked”, then from Motion add “go to x: 0 y: 0” so the fish starts in the middle.
  2. Add “set rotation style left-right”. Without it, the fish swims upside down whenever it turns to the left.
  3. From Control, add a “forever” loop. Inside it, put “point towards mouse-pointer”.
  4. Add “if … then” inside the loop. For its condition, put a “>” block from Operators in the gap, with “distance to mouse-pointer” from Sensing on its left and 10 on its right. Inside the if, add “move 4 steps”.
  5. Add a second “if … then” with “touching mouse-pointer?” as its condition. Inside it, put “change color effect by 5” from Looks.
  6. Click the green flag and move your pointer around the stage.

This project runs on screen in Robolab, so you don't need MIEO for it.

The finished program
The Robolab blocks for the Fish sprite, as listed below
when green flag clicked
go to x: (0) y: (0)
set rotation style [left-right v]
forever
  point towards [mouse-pointer v]
  if <(distance to [mouse-pointer v]) > (10)> then
    move (4) steps
  end
  if <touching [mouse-pointer v] ?> then
    change [color v] effect by (5)
  end
end
Checked in Robolab 1.0.10. Open it in Robolab from the File menu.

Test it

Click the green flag and move your pointer over the stage. The fish turns to face it and swims after it, flipping left and right but never upside down. When the fish reaches the pointer it stops moving and its colour slowly shifts while it stays there. Move the pointer away and the chase starts again.

If it doesn't work

  • The fish wobbles back and forth when it reaches the pointer? Make the 10 in the “>” block bigger, or the 4 in “move 4 steps” smaller.
  • The fish swims upside down? Check that “set rotation style left-right” comes before the forever loop.
  • Nothing moves? Click the green flag, then keep your pointer over the stage rather than the code area.

Make it your own

  1. Easy: change “move 4 steps” to 2 for a lazy fish, or to 8 for a speedy one.
  2. Medium: add a Jellyfish that drifts around the tank on its own, using “glide 2 secs to random position” inside a forever loop.
  3. Hard: turn it into a game of tag. Use the timer to count how long you can keep the pointer away from the fish, and stop the game when the fish catches it.

What to build next

Ready for a real robot?