1, 2, 3, code ! - Cycle 3 activities - Step 2.4. Gathering resources and managing scores

Summary

Students complete their program by adding resources to gather (new sprites) and creating a variable for their score (this score increases as more resources are gathered). They learn to program conditional instructions (if ... then) and use sensors.

Key ideas
(see Conceptual scenario)

“Algorithms”

  • A loop allows the same action to be repeated multiple times.
  • A test can be used to choose which action to carry out if a condition is true or not.
  • A condition is an expression that can be either true or false.

“Machines”

  • A variable is a name given to a memory area. It is used to store a value and reuse or change it later.

“Languages”

  • Certain instructions are executed at the same time as others. This is known as parallel programming.

Matériel

For the class

  • Projector
  • Enlarged version (A3 or A4) of Handout 1

For each pair of students

  • A computer with Internet access or, if there is no good connection available, a computer with Scratch preinstalled.

For each student

Glossary

Loop,  test, sensor, random number

Teaching notes:

  • This is the project’s central step, as students will have to discover and use a number of new concepts to manage resources: tests, variables, sensors and operators.
  • In order to avoid tackling all these new ideas at once, we suggest splitting the step into several basic tasks. Even then, activity 2 is relatively complex and will require guidance from the teacher.
  • From now on, it is unrealistic to expect all students to progress at the same pace (otherwise, the most advanced groups will quickly become bored and distracted). Breaking down learning into steps and tasks helps facilitate the teacher's management of the class: everyone has something to do, whatever stage they’re at.
  • We recommend putting together pairs with a similar level rather than pairs where a struggling student and a more advanced student work together (as in the latter case, experience has shown that struggling students become passive and let the others do the work).

The teacher explains that the mission has to collect various resources to survive, including water (in the form of ice, on this planet) and food (in the form of plants). The rover will have to pick them up, and a “score” will be used to count the number of resource items collected.

 

 Activity 1: Importing a resource (ice) in the form of a new sprite (5 minutes)

The students return to the program they saved during the previous session and import a new sprite: the ice (image available in the “Sprites" subfolder of the files provided, as previously).

The teacher reminds the students to manually set the position of this resource, as they did for the rover in the previous session. They can position the ice anywhere on the stage, so long as the sprites (ice and rover) do not overlap.

 

For example:

Teaching note:

Here we notice that each sprite has its own scripts area (switch between the rover program and the ice program by clicking on the sprite in question). There can be as many programs as there are sprites: all programs are executed in parallel.

 

 Activity 2: Making the resource say “Well done!” when touched by the rover (20 minutes)

Each pair needs to amend the ice program so that it says ”Well done!” when it is touched by the rover. The teacher should let them try to work it out, then check on the groups to guide them if they get stuck.

This task involves:

  • Knowing how to make the sprite say “Well done!” (all the students know at this stage)
  • Knowing how to launch an instruction only once a given condition is fulfilled. That is done via the “Control” category, where the instruction “if ... then” is to be found

  • Knowing how to detect when one sprite touches another. That is done via the “sensing” category of the scripts tab (“touching ... ?” instruction). Once the instruction has been selected, clicking on the arrow opens a list of the sprites already created. Here, we are in the ice program and want to test whether this sprite is touching the rover, so we click on “rover.”

 

The ice sprite program then becomes:

 

Unfortunately, when you execute the program and the rover is driven towards the ice, it does not work. Why? The class can discuss together what the program does, step by step:

  • The ice is placed in the chosen position
  • A test is carried out: if the ice touches the rover, then it says “Well done!”
  • Then ... nothing.

When reading the program, we notice that the test is only carried out once, when the program is launched (just after setting the position of the ice). But at that moment, the two sprites are not touching. So the “Well done!” message is not displayed, which is normal.

For the program to work properly, the test “if the ice is touching the rover” has to be carried out constantly, so as to trigger the desired action as soon as the condition is met.

To do that, simply place the test within a “forever” loop, which is to be found in the “Control” category. The ice program becomes:

 

 

 Activity 3: Making the resource disappear when touched (10 minutes)

This very simple task simply requires replacing the instruction “‘say Well done!’” with an instruction that makes the sprite disappear. The instruction in question is “hide” which is to be found in the “Looks” category.

Please note: Once the program is launched, the resource is now always hidden (because we have not told it to come out of hiding!). It is therefore needed to add the instruction “show” just after the instruction “when green flag clicked.”

The ice program becomes:

 

 

 Activity 4: Creating a “score” variable (5 minutes)

The teacher reminds the students that they need to create a score and increase it each time a resource is touched. The students can explore the various categories of instruction: the one they are looking for is in the orange “Data” category, and called “Make a variable.”

 

 

Teaching notes:

  • The variable created may be accessible either to only one sprite (the one in the program within which it was created) or all sprites. In other programming languages, these are known as local and global variables, respectively.
  • For a program to be easy to understand, it is important to give explicit names to the variables you create. This good practice also limits programming bugs. The name of the variable could, therefore, be simply “score.” Some students use names that have no meaning, or which demonstrate confusion between the variable and operations using the variable (for example, they might call the variable “add 1 to score”).
  • When the variable is created, it is displayed on the screen, alongside its value. To get rid of this display, simply untick the box to the left of the variable name, in the “data” category.

Here, the aim is to keep a score. This variable no doubt needs to be used by several sprites (the various resources), so it needs to be accessible to all of them.

The teacher should point out to the students that they have already used variables in previous sessions (the X and Y coordinates that give the position of the sprite on the stage). These variables were already available and the students used them (carried out tests, set values, etc.) without having to make them.

 

 Activity 5: Increasing the score when a resource is gathered (10 minutes)

The students decide how the score should be increased (for example, it could increase by 1 every time the rover touches a resource).

Then, the students should look for an instruction to increase the score by 1:

They simply need to place it in the ice program, within the test “touching rover?”, just above or below the instruction “hide.”

 

Teaching notes

  • The variables X and Y, which are pre-existing and relate to the position of the sprite, are available in the “Motion” category, along with the instructions relating to them (setting a value, increasing the value, etc.). The variables created by the user, like the score here, are in the “Data” category. There are two instructions relating to them:
  1. “set “score” to (...)": this command can be used to store the value zero in the “score” variable. The zero can be replaced by any other value.
  2. “change “score” by (...)” this command takes the previous value of the variable “score” and adds 1: this new value is now saved in the “score” variable. This is the instruction we need here.
  • To get familiar with these instructions, we recommend letting students try them out for a few minutes, with the variables they are using displayed.
  • There is an unplugged activity to better understand the use of variables.

 

 Activity 6: Resetting the score to zero (10 minutes)

The students test several times what happens when the rover touches the ice. The program seems to work (the ice is present, the rover touches it, the ice disappears and the score is increased by 1). However, if you stop the program and start it again, the score does not return to zero.

To reset the variable to zero, simply add the instruction “set score to 0” at the beginning of the program.

Teaching notes:

  • On the face of it, it should be possible to reset this value in the program of any sprite: the important thing is for it to be done once, and only once. But the score is a variable that will no doubt be used by other sprites (plant, when we add it). There is no reason to choose the ice sprite over a plant one. That is why we recommend resetting the variable in the rover program (which is our “main” program), just below the resetting of its position.
  • You can also decide that the main program is that of the backdrop and not that of a sprite. In that case, you can put all the resets in the backdrop, under a “when green flag clicked” instruction.
  • When you create a variable, it is a good idea to get into the habit of resetting it straight away.


Resetting the position of the rover and the score in the rover program.

 Activity 7: Making resources reappear in random positions (15 minutes)

The game can be more fun if the resources reappear after being gathered, but not always in the same place. A random position is best. That means using the instruction “go to,” which the students already know, as well as a new instruction available in the green “Operators” category. This new instruction is called “pick random ... to ....”

As the X axis in Scratch varies between -240 and +240, and the Y axis between -180 and +180, the following command is used to position the sprite at a random position on the stage:

There is no longer any need to hide the sprite, as it is simply moved. The ice program becomes:

It is also possible to make the initial position of the resource random rather than fixed (at X=126 and Y=87 in our example).

Teaching notes:

  • Teacher guidance can be quite free. For example, you could have a student recap the values between which X and Y vary, or find other examples in daily life were random things are needed (dice rolls, card games, national lottery, etc.) .
  • Students will then look in the “Operator” category for the instruction that gives a random value between -240 and 240 (for X) and between -180 and 180 (for Y). They should find that without any difficulty.

 

 Activity 8: Importing a new resource (plant) and repeating the same tasks as for the ice (20 minutes)

The students now need to add a second resource (plant) and repeat the work done for the ice:

  • Import the “plant” sprite
  • Constantly test whether this sprite touches the rover, and if yes:
    • Increment the score by 1
    • Make the sprite reappear elsewhere on the stage (random position)

This activity is very useful for students as it allows them to repeat and consolidate the various concepts already seen.

 

Conclusion and lesson recap activity

The class summarizes together what they have learned in these various tasks:

  • It is possible to create variables in a program. It is best to reset each variable, meaning to give it a value when the program is launched.
  • A computer program can generate random numbers. This means that each execution can give a different result.

 

The students write down these conclusions in their science notebooks. The teacher updates the poster entitled “Defining computer science.” 

 

Prolongement

Les variables, instructions conditionnelles et boucles peuvent être retravaillées à l’occasion de plusieursexercices en ligne. Attention, ces exercices n’utilisent pas Scratch.


 


<< Step 2.3 Sequence II Step 2.5 >>

 

Project partners

Aucun résultats