Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Make a Cat and Mouse Game in Scratch

· 5 min read · Home Tech

In this article
  1. How Fast Should the Cat Move?
  2. How to Make the Mouse Move Smoothly
  3. How to Add a Win Condition
  4. How to Make the Cat Smarter (Avoid Walls)
  5. How to Add Multiple Food Items
  6. How to Fix the Cat Not Following
  7. How to Tell If Your Game Works
  8. Common Scratch Mistakes to Avoid
  9. How to Customize the Game

To make a cat and mouse game in Scratch, you control a mouse sprite with the arrow keys to collect food while a cat sprite chases it; the game ends when the cat touches the mouse. Start by creating a new Scratch project and choosing a backdrop and sprites. The core loop is: move the mouse, make the cat follow, and check for collisions.

  1. Open Scratch and click the Choose a Sprite icon; select Cat and Mouse1 from the library. Delete the default sprite if needed.
  2. Click the Choose a Backdrop icon and pick a simple backdrop like Bedroom 2 or Maze.
  3. Select the Mouse sprite. In the Code tab, drag out a when flag clicked block. Then add a forever block. Inside it, add four if key pressed blocks: one for up arrow (change y by 10), down arrow (change y by -10), left arrow (change x by -10), and right arrow (change x by 10).
  4. Still on the Mouse sprite, add a when flag clicked block. Then add a forever block with an if touching Cat block. Inside that, add a say block saying "Game Over" for 2 seconds, then a stop all block.
  5. Select the Cat sprite. Add a when flag clicked block, then a forever block. Inside, add a point towards Mouse block and a move 3 steps block.
  6. Add a Food sprite (like Apple) from the library. Place it somewhere on the stage. Add a when flag clicked block, then a forever block with an if touching Mouse block. Inside, add a hide block, wait 1 second, then go to random position and show.
  7. Add a Score variable by clicking Variables then Make a Variable named Score. On the Food sprite, inside the if touching Mouse block, add a change Score by 1 block.
  8. Click the green flag to test. Adjust the cat's speed (move steps) if it's too fast or too slow; try 2 to 5 steps.

How Fast Should the Cat Move?

Start with the cat moving 3 steps per frame. If the cat catches the mouse too quickly (within a few seconds), reduce to 2 steps. If the mouse can easily escape forever, increase to 4 or 5 steps. You can also add a wait 0.1 seconds block inside the cat's forever loop to slow it down without changing steps.

How to Make the Mouse Move Smoothly

The default arrow key movement changes x/y by 10 each frame, which feels jumpy. For smoother motion, use a glide block: replace the change blocks with glide 0.1 secs to (mouse x, mouse y) inside a forever loop. This makes the mouse follow the cursor smoothly. Alternatively, keep arrow keys but add a wait 0.05 seconds after each move to reduce speed.

How to Add a Win Condition

To make the game winnable, set a target score. On the Mouse sprite, add a when flag clicked block, then a forever block with an if Score = 10 block. Inside, add a say block saying "You Win!" for 2 seconds, then stop all. You can change the target number by editing the equals block.

How to Make the Cat Smarter (Avoid Walls)

If your cat gets stuck on walls, add a if touching edge block inside the cat's forever loop. When true, add a turn 180 degrees block. This makes the cat bounce off edges. For maze backdrops, the cat may still get stuck; consider using a simple open backdrop instead.

How to Add Multiple Food Items

Duplicate the Food sprite by right-clicking it and selecting duplicate. Each copy will have the same scripts, so they all hide and reappear independently. To avoid them overlapping, set different starting positions by dragging them on the stage before clicking the flag. You can also add a wait block in each food's script to stagger their appearance.

How to Fix the Cat Not Following

If the cat doesn't move, check that you used the point towards block and the move block inside a forever loop. Also ensure the cat sprite's name is exactly Cat (or adjust the block to match). If the cat moves but doesn't turn, add a point in direction block with 90 before the forever loop to reset its orientation.

How to Tell If Your Game Works

Click the green flag. The mouse should move with arrow keys, the cat should follow, and the score should increase when the mouse touches the apple. When the cat touches the mouse, "Game Over" should appear and the game stops. If the score doesn't increase, check that the Food sprite's if touching Mouse block is inside a forever loop and that the variable is named Score exactly.

Common Scratch Mistakes to Avoid

Forgetting to put the if touching blocks inside a forever loop is the most common error. Also, if you use when space key pressed instead of when flag clicked, the game won't start properly. Always use the green flag to start. If the game runs too fast, add wait 0.01 seconds in the cat's loop to slow it down.

How to Customize the Game

Change the cat's costume to a different animal by clicking the Costumes tab and selecting a new one. Add sound effects by importing a sound and using play sound blocks when the mouse touches food or when game over. You can also add a timer by creating a variable Time and using a when flag clicked block, then a forever loop that waits 1 second and changes Time by -1, stopping when Time reaches 0.