Chapter 4. Cat and Mouse

0 732 Easy

It is not fun to play games without scoring! Well now, we will create a game and add score system. Cat and mouse is such a classical game! Today, we are gonna make a game like this, but there is something special about our main character cat, he does not need to run, but only stay where he is, turn around, and constantly fire hidden weapons to hit the scampering mouse.

Players get 1 point if the mouse gets hit once. The one who get the most scores within the given time will be the winner.

In this tutorial, we will also learn variable and the micro:bit accelerometer.

4.1 Accelerometer

Accelerometer, also called gravity sensor, is widely used in our daily life, for instance, gradienter, passometer in the smart phone. The accelerometer on a micro:bit measures acceleration as its name suggests or detects the movement along three axes. Get the data of these three axes, then the micro:bit board’s gesture such as moving, shaking, tilting or free falling can be detected.


4.2 Algorithms Analysis

- There are three sprites in the game: cat, mouse, and ball. The ball is the weapon the cat uses to hit the mouse. The mouse has two shapes, we can choose one as the state when it gets hit.

- Tile the micro:bit board to control the cat’s movement:

- The mouse moves randomly in the stage, if it touches the edge of the stage, it will point to the opposite direction from which it was coming;

- The ball is the cat’s hidden weapon. It is placed under the cat. When button b is pressed, the ball moves forward until it touches the edge. If the ball hits the mouse, the player gets one point, at the same time, the mouse changes its shape and the screaming sound is played.

4.3 Programming

1. Add the sprites mouse, cat and ball, and set the size to 50%, 70% and 40% respectively. Then add a background for the stage. Shown as figure 4-2.


2. Shape design

Click the sprite mouse, we can find there are two shapes, mouse a and mouse b. Here we set the mouse b as the state when it gets hit. To make it obviously, we can fill the mouse’s body and head with red color.


3. Program the mouse

Figure 4-4 is the program that the mouse runs on the floor. To multiply the routine of the mouse, we add a “pick random () to ()” block to make the mouse move randomly.

4. Program the cat initially

The cat’s movement is controlled by the micro:bit board. Tilt the board to left, the cat turns left for 15 degrees. Tilt to right, turn right for 15 degrees. Shown in figure 4-5.


Except the gestures of tilting to left or right, micro:bit also can recognize several other gestures.

5. Program the ball initially

The initial state of the ball is hidden. The program of the ball will be run in a loop: hidden in the bottom, detecting if the button is pressed, ejecting the ball. As figure 4-7.


6. Complete the program

When we test the program, we found that there are two problems about the program of the ball: 1. The ejecting direction of the ball is unchanged but we want that changes with the cat turning around. 2. After the ball hit the mouse during its way, nothing changes. However, we want it like this way: when the mouse gets hit, add one score for the player, meanwhile, the ball broadcast a message “got hit”, then after the mouse received the news, it changes its shapes and makes screaming noise.

How to solve the first problem? —create a variable “direction”. When the cat turns around, we assign a value to the direction, then the ball points to that variable “direction”.


et’s get to know about the “point in direction ()” block. As shown in figure 4-8, the top is 0 degree, and 180 degrees is a semicircle, which points to the down. The left semicircle ranges from 0 to -180 while the right semicircle from 0 to 180. -180 and 180 point in the same direction.

The final script of the cat is shown in figure 4-9, tick the checkbox next to the variable “direction” block, then the value of it will be displayed on the left-upper corner of the stage area. Tilt micro:bit board to make the cat turn around, we can see the value of the direction equals to that shown below the stage. Now let’s go to the second question. What will happen after the ball hits the mouse? Firstly, we need to add a score system. Set a variable “Score”, and its initial value is 0. If the ball hits the mouse, add scores for the player. Meanwhile, broadcast the message “got hit”. The program is shown as below.


Then we need to program the mouse. The ball broadcast the message “got hit”. When the mouse received the message, it will make screaming noise. So, we need to add the sound play block here. Select “scream1” from the sound library. The final program of the mouse is shown in figure 4-11.


4.4 Variable and Broadcast

1. Variable

We may think of a variable as a container or box where we can store data that we need to use later, and each box can only hold one value (number, text and Boolean data) at a time. For example, use it to store an integer, after we put 1 into it, we put 2 into it, then we can only get 2 from this box. Similarly, if there has been a string “blue” in the variable, when we place another string “red” into it, only the latter can be stored. The name of the box is the variable’s name and the value of the variable is placed inside the box.

Mind+ supports three types of data: Boolean, number and string.

Boolean data type has two possible values: True and False. One of the most common uses for Boolean variable is inside if statement.

Number type includes integer and decimal.

String is a collection of a series of characters. It can be letter, number (0-9) or any symbols that can be output from the keyboard (+, *, -, etc.). So, string can be used to store name, address, title and so on.

List: after we get known about variable, it would be so much easier to understand what a list is. If we think of a variable as a box with a name for storing data, then a list is a string of boxes, in which each box can hold one value. The string of boxes only has one name.

Create a variable: click “Variable” module, a default variable “my variable” can be found in its block area. Create a variable and name it. For instance, name it as “score”. Now we can design our variable: assign, increase by degree, hide or show the variable. Shown as figure 4-12.


2. Broadcast

We can use the “broadcast” function of mind+ to order a sprite or several sprites to execute the preset scripts. The function related with “broadcast” can be found in “event” module (Three in total).





block to send the message you want to broadcast. Click the drop-down box of the block, select “New Massage” then input a content into the pop-up box “new message name”. You can input Chinese, English or numerical symbol.


Different from the block


sends a broadcast throughout the whole project-any scripts that are halted with “When I Receive ()” block and are set to the broadcast will activate. It lets scripts send broadcasts, and have them wait until all scripts activated by the broadcast end.

Scripts that begins with this block


will be invoked once the specified broadcast has been sent by a calling script.

4.5 Programming Result


4.6 Training Camp

We have added score system for the game, but can you make some improvements based on that? For example, imagine what will happen if the mouse encounters the cat accidently.

How about adding a timing system for the game? Just give it a try!

All Rights