Module 4 - The “Cat Sets a Trap for the Mouse” Project

Z Wiki Mistrzowie Kodowania
Skocz do: nawigacji, wyszukiwania
Logo Mistrzowie.png

These materials have been developed by Ośrodek Edukacji Informatycznej i Zastosowań Komputerów (Centre for Information Technology Education and Application of Computers) in Warsaw as part of the “Mistrzowie Kodowania” ("Coding Masters”) Programme funded by Samsung Electronics Polska, licensed under CC-BY-SA “Attribution – Share Alike 3.0 Poland”



General objectives:

  • Review and revision of previously presented Scratch commands and language structures;
  • Introduction of the concept of variable;
  • Introduction of randomness;
  • Implementation of a games in accordance with a scenario developed under the teacher’s guidance.

Specific objectives:

  • The student will understand and know how to use appropriate iteration instructions (repeat and always).
  • The student will understand and know how to use appropriate conditional instructions (if and if ... otherwise).
  • The student will be able to use randomness in the programme.
  • The student will understand the concept of variable and will know how to use it in the programme.


Discussion of the solution of Module III additional task

Time to complete this part: about 15 minutes

At the beginning of the lesson we remind students what they were supposed to prepare between classes. We invite a volunteer to present his/her solution. We ask if anyone came up with a different solution. We discuss the solutions (we praise each correct solution).

If there was no homework, we start by repeating key issues from the previous module. We also ask if the students have any doubts about their own projects. We invite students to show anything they are proud of if they want to.

Designing and writing a programme which automatically controls sprites – the Cat lurking and waiting for the Mouse

Time to complete this part: about 45 minutes

We remind students of the game project where the Kitten tried to catch the Mouse which was controlled by the player using a computer mouse (Module II). Then we explain what the next project will involve. This time we will want both the Kitten and the Mouse to be controlled by the computer. The Kitten will not chase the Mouse, but it will set a trap for it and lurk in order to catch it. The Mouse’s task will be to outsmart the Kitten. Naturally, both must have a chance to win.

We should start designing the programme by sketching how it will work on the board and discussing ideas about how to implement it. First we draw the Kitten in the left part of the stage, and the Mouse in the right part.

Task: We ask the students to add both characters (sprites) to the project stage and name them Kitten and Mouse, respectively.

If necessary, we individually remind the students how to add a new sprite.

We ask the following question: What can a mouse trap involve? What should it contain in order to be attractive for the Mouse?

Ideas will probably vary. We should focus on those which students can already implement in Scratch. We start by discussing the bait. It can be e.g. a piece of cheese or some other mouse delicacy. We ask the students where we can get the cheese for the trap. We suggest that we can draw it ourselves. Next we ask what the cheese should be in our project. We hint that it should be another sprite. This will make it easier to introduce the interaction of the Mouse when it catches the cheese (wins) without getting caught by the Kitten. It will also be easier to direct the Mouse to the cheese – in the same way in which the Kitten moved towards the Mouse earlier (in the module II project).


Task: We ask the students to add another sprite to the stage design. Each student should draw his/her costume resembling cheese and change the name to Cheese.

If necessary we individually remind the students how we can draw a new sprite costume.

We ask the following question: What can the Mouse trap look like, assuming that the Kitten will have a chance to catch the Mouse lurking near the trap, and the Mouse will have a chance to outsmart the Kitten?

Most likely there will be a lot of ideas. We suggest that the trap should have two entrances. The Kitten will lurk only at one of them. If the Mouse chooses the other one, it will outsmart the Kitten – this way both characters will have a chance to win the game. Next, we consider what should be used to build the trap. We can suggest that the Kitten may set it, i.e. draw it. The Mouse will not be able to go through the walls drawn (built) by the Kitten. Then, together with the students we think about what the trap should look like. If necessary, we propose that it should be a square without one wall, placed with the missing wall close to the left edge of the stage. As a result, there will be two entrances at which we can place the Kitten. Students should already be able to draw the square. The cheese should be placed in the centre of the square. The trap may resemble the one shown below.


Task: We ask the students to create and test a script for the Kitten which will draw the trap after it is started. We remind them that we have already drawn a square in class.

Discussion: After the students have created the script we look through the solutions and discuss how to improve them.

We should pay attention to the following elements:

  • the Kitten’s setting at the starting position (the cheese should always be in the centre),
  • clearing the screen before drawing the trap,
  • setting the colour of the trap walls,
  • pointing the Kitten toward the right side of the stage to draw the trap walls,
  • thickening of the trap lines,
  • setting the Kitten toward the right side of the stage, so that it lurks waiting for the Mouse to come.

From the students’ designs we select one in which the majority of these elements have been implemented and show it as a model. When selecting the design to be shown we should also pay attention to how the square has been drawn – whether the block repeat <3> times has been used and whether when the drawing was finished the pen was raised, as any other solution may cause problems later.

Task: We ask the students to complete the Kitten script in accordance with the above guidelines.

The guidelines should be written down or drawn on the board. After completion we check students’ solutions and, if necessary, help them individually. Should students have problems, we may jointly discuss which blocks can be useful for the task.


A sample Kitten script may look like this:


Task: We ask the students to add a Cheese script – after starting the programme the Cheese is always set in the middle of the trap, but away from the Kitten. On reaching the Cheese the Mouse should not get caught by the Kitten.

Adding the Cheese script should not take much time, because students have already added the appropriate blocks in the Kitten script.

A sample Cheese script may look like this:


Discussion: We introduce students to the next task – controlling the Mouse. We want the Mouse to move automatically towards the Cheese as soon as the programme is started. We need to discuss how we can achieve this and what will happen depending on the following events (we write down key discussion findings on the board):

  • The programme is started

The Mouse should be set in the starting position. Then it should repeat the following actions: rotate in the direction of the Cheese, move by a certain number of steps and see if it has reached the Cheese or the Kitten, or if it is touching a wall. If it has not reached the Cheese or the Kitten, it should always repeat the activities from the moment of rotating towards the Cheese. The number of steps should be small so that the Mouse does not pass through a trap wall – otherwise our programme will not work properly.

  • The Mouse is touching the Cheese

The Mouse can say something, e.g. “Tasty cheese” and the programme ends

  • The Mouse is touching the Kitten

It can say “Ouch, the cat has caught me!” and we end the programme

  • The Mouse comes to a trap wall (touches it)
    • It should think e.g. “Hmm... How can I get to the cheese?”
    • It should somehow bypass the wall.

Together with the students we come to the conclusion that they are able to programme the first three events. If necessary, together we can select blocks useful for the task, but it is better that this part of the programme is completed by the students on their own. What does require discussion is the task of Bypassing the wall. We discuss with the students what the Mouse should do to be successful when it encounters a wall. We should come to the conclusion that if whenever it reaches wall it goes back as many steps as it came forward, then turns right (by 90 degrees to go parallel to the wall) and goes a few steps forward, it will always eventually bypass the wall and avoid meeting the Kitten.


Task: We ask students to create and test a Mouse script in accordance with the guidelines which have been written down.


A sample Mouse script may look like this:


Adding randomness to the Mouse’s movements

Time to complete this part: about 30 minutes

We note that this version of our programme always works in exactly the same way – the Mouse reaches the Cheese regardless of the starting position.

We ask the following question: What can we do to make our programme less predictable – to allow the Kitten to sometimes catch the Mouse in the trap which has been set?

Various ideas may be provided. We hint that the Mouse should try to turn left, and at another time right, because it is not certain where the Kitten is (at the top or the bottom). We ask on what basis the Mouse can make such a decision not knowing on which side the Kitten is waiting. We can refer to a situation in which e.g. before a football match it needs to be decided which team will begin the game. In such a case a coin toss is often used, i.e. a form of drawing.

Task: We ask our students to find a block associated with such a drawing.


We ask the following question: From what pool of numbers should we draw? What should we make this dependent on?

The Mouse can turn left or right. That’s why we should draw one of two values – e.g. 1 or 2. Then we can assume that 1 means turning right and 2 turning left. We think about how to decide after a value has been drawn. What blocks can we use for this?

The students will come to the conclusion that the block if will be useful. However, a better solution would be to use the block if... otherwise. There remains the problem of what to insert in the if condition. Students will probably come to the conclusion that it should be the result of the drawing. However, it should be compared to a determined value (e.g. 1). Together with the students we create and explain the part of the script responsible for the drawing and selection of the direction in which the Mouse is to rotate. We pay special attention to comparing the values providing true or false as feedback.


The modified script part should look as in the below illustration.


Task: We ask the students to modify the script and introduce the principle that when the Mouse touches the wall a drawing takes place to determine which way it should turn. We also ask them to test the programme.

The effect is almost always the same. The Mouse is stuck by the wall. We discuss why this is so. We reach the conclusion that the Mouse, as a result of drawing which way to turn whenever it touches a wall, ends up spinning in the same place (because it rotates once to the left and once to the right). Together with the students we think about what should be done to allow the Mouse to go further. We reach the conclusion that already at the beginning it should decide which way to turn if it bumps into a wall. It must somehow remember this decision. That is why it will need some memory. Computer programmes store various things using variables, so we will learn how to use them. We can compare a variable to a drawer in which we place something to be used later on.

Task: We ask the students to got to the Data category of blocks, and then create a variable called left or right. For this purpose they should select the Create variable button. They should enter left or right in the Variable name window.

If there are questions about whether the variable should be available only for this (currently selected) sprite or for all sprites , we ask students to leave the default option for all sprites.


We discuss with the students what new options appeared in the variables, and how we should modify the Mouse script now. Together we come to the conclusion that drawing should take place immediately after the programme is started, and its result should be stored in the left or right variable by setting its value. It is also necessary to change the decision-making part – instead of drawing, the variable left or right should be inserted there for comparison.

Task: We ask the students to make changes to the script, and then test the programme a few times.

A sample corrected Mouse script may look like this:


Suggested additional tasks to be self-completed by students

  1. A drawing determines the Kitten’s position at the trap (at the top or at the bottom of the trap).
  2. We add the support of L and P keys changing the direction in which the Mouse is to turn, and G and D keys controlling where the Kitten is to wait for the Mouse.
  3. We change the Kitten script so that it moves randomly up and down along the left edge of the screen.