Search
Close this search box.

How to Program a Simple Cat & Mouse Game With Scratch!

In this post we are going to learn how to make a simple cat & mouse game with Scratch.

So without further ado let’s get started!

Step 1: Getting Started with Scratch

In the beginning, you should open a web browser and type “Scratch” in the search bar. The first result you get should have a message like “Scratch – Imagine, Program, Share.” Click on the Scratch webpage link. Once you’re there, look for the option called “Create” in the drop-down menu at the top of the page and click on it. This will take you to the project editor screen where there is a blocks panel on the left and a cat sprite on the right. 

In the list, find the small cat image at the bottom of the coding window and click the trash can icon directly next to it to delete it. Then, move your cursor to the bottom right corner where there’s a plus sign on a cat icon. When you hover there, a sidebar with four options should appear. Select the magnifying glass to go to the sprite selection page.

Step 2: Picking Your Characters

*Note: There are no restrictions on your choice of characters. If you want, you can even have a referee throw a baseball in the air and run away!

First, you should enter the sprite selection page. In the search bar at the top left corner, type “cat” and choose the second cat sprite that appears. Then, access the sprite selection page again and search for “mouse.” There is only one mouse option, so select that one. Now you will see both a cat and a mouse displayed on your screen.

Step 3: Adding Some Background

There is no background in the scene yet. Let’s add one! Move your mouse over the “picture” icon located near the “new sprite” icon. A pop-up list will appear; select the “search” icon. At the very top, you’ll find some category options to filter your results according to your requirements. Choose “Outdoors.” I liked the Farm background, but feel free to choose the one you want. You will be taken back to the coding screen. The sprites are now on a farm!

Step 4: Adding Your First Bit of Code (Where Your Sprites Start)

First, go to the “general” category by moving your mouse pointer over a small cat icon on the right side of the screen.

The best part is just ahead! In the vertical bars on the left side of your screen, you can see numerous coding options. The code categories are located to the far left. You can see a list of the different categories of code when you click on the yellow “Events” category. Several code blocks will appear. Drag and drop the “When space key pressed” block into the Code Area. Next, move your mouse to the right side of the screen and position your cursor (in my case, on the bottom left corner). To do this, use the “motion” tab of the Block Selection Area. Drag the “go to x: ___ y: ___” block to the space below the “when space key pressed” block. Clicking in the “motion” category will allow you to drag the “go to x: ___ y: ___” block to the “when space key pressed” area. It will drop in exactly the right spot. Move the mouse, but shift the mouse to another area on the screen.

Step 5: Make the Cat Do What Cats Do

Insert Code for the Cat

First, go to the “motion” category in the block selection area. Select the “glide 1 sec to ___” block and place it inside your code area. Now, you can see a down arrow in that section where you will choose the glide destination. Choose “mouse 1” from the drop-down list. Next, go to the “control” category and drag the “forever” block above the “glide” block. All the motion blocks should be placed inside the “forever” loop. If dragging the motion block fails to place it in the loop, connect the loop to the program with the [Connect] option.

Now you can try it! After you press the space key, you can watch the cat on the screen chasing the mouse. For fairness to the cat, in the “glide” block, change the time to TWO seconds to make the glide take the same time as the initial 1-second motion towards the mouse. After you test the code, you should notice that the cat moves more slowly towards the mouse.

Step 7: Making the Cat Move (More) Naturally

Enhance the quality of the code for the Cat

Start your code and move the mouse sprite up and down using the keys on the keyboard. Notice the cat’s actions. It goes where the mouse was but doesn’t keep chasing it, right? For the cat sprite, return to the block selection area and in the “motion” category, drag the “point towards ___” and “move ___ steps” blocks inside the forever loop. Set the point block to the position of the mouse and set the cat to move five steps. When you run the code, the cat will continuously follow the mouse. Adjust the speed by changing the number of steps.

Step 8: Game Over

Mouse Codes

Add the following code section. Begin with “control” and add another if-then command to the mouse’s loop code. In the command, place an “if” sensor (found in “sensing”) and set it to detect if the mouse touches the cat. Finally, go to “control” and put the “stop all” block inside the command. This way, if the mouse is caught by the cat, the game will end.

Step 9: Watch Where You’re Going!

Avoid Collision Mishaps

Even while playing a game, you may notice that the mouse always hits the same wall, regardless of its movement. Let’s fix that! Go to the “motion” category. Insert a “turn ___ degrees” block into each movement section. Click on the parameter and a wheel will appear. Adjust the arrow to the desired angle by rotating it clockwise or counterclockwise. For instance, when moving right, adjust the arrow to the right, and when moving left, adjust it to the left, and so on. This ensures the mouse is facing the direction it moves, preventing collisions with walls.

Share the Post:

Related Posts