📜 ⬆️ ⬇️

Scratch for "advanced"

Scratch is a visually oriented programming language for children. There is an opinion that this is a children's language in order to simply play “into programming” and nothing good (serious) can be done from it. When I first started teaching classes for children on Scratch, it seemed to me as a person with two higher technical education. However, after a while I had to change my opinion. It turned out that even in this children's programming language there are hidden chips that can be seriously used even when teaching professional programming. I want to share with you my discoveries.

Cloning and Variables in Scratch


My student liked the game “Zombies vs. Plants . And he was interested to program it himself. Let's remember how this game works? On the right side we have a line of plants that shoots approaching zombies.

There are many solutions to this problem, but in my opinion a more elegant solution is to use the minimum possible number of sprites, i.e. use cloning.

How to make each individual clone have its own standard of living? What to choose variables or lists? We made an attempt to use lists, but after more detailed study we realized that this data structure does not help to solve the problem. Variables and lists in Scratch have 2 types of visibility for each data type - this is “for all sprites” or “just for this sprite” . I had to check the scope. We started with variables.
')
Reaching the working variant of the variable with the scope of the “only for this sprite” . In classical programming languages, this type of visibility is called local or private, depending on the context.

Sozadnie variable Life in Scratch


It turns out that if this variable with a given scope is used in a clone, then an instance of the variable belonging to the specific clone and which is used for the internal calculations / operation of the clone is created in the clone. In our case, each clone has its own “life”. If it became interesting, then I advise you to refer to the directory . And here is a picture that shows how sprites or clones work with variables of different types of visibility.

The scope of variables and lists in Scratch. And their boundaries
The variable with the “for all sprites” scope (left image) is one for all. By the way, here you can see with your children work with critical sections and what is a race for a resource. And if the variable's scope is “only for this sprite” , then the sprite and its clones have their own variable with the same name with which they work (figure from the right). And clones do not have access to the original variable and the variable of another clone.
This was an unexpected and pleasant discovery of Scratch properties for creating similar algorithms.

IMPORTANT: A variable is a very powerful tool for a real programmer, so I immediately teach children the proper naming of variables, as this skill will help them in projects of varying complexity.

Object names as their identifiers


IMPORTANT: all variables with zone visibility “only for this sprite”.

This tool for me was completely unexpected. Let's start with the story of how a student and I came across this feature of the language. My student decided to create a game with "artificial intelligence". The game represents star wars on spaceships and the player has the ability to control one ship, while opponents chase you and each other. So that they can pursue you or each other, they need to somehow decide who to pursue. This task can be solved in the forehead and set the order of pursuit of ships, but the game will lose its intelligence and quickly get bored. Therefore, the student decided to adjust the intellect in such a way that the ships with the intellect chased the nearest enemy. Let's see how he started to solve this problem.

Vlob solution for switching between sprites by minimum distance

In my opinion a good solution for creating a fast prototype. But what limitations are there? First, the complexity of the code as you add new ships. Consequently, the complexity of the code increases and code mutations are not excluded due to complexity. Time to search for errors and debug the algorithm will increase. And of course, no one has canceled such a cool metric as the advance level of SW Development. What to do? And you need to do the following, you need to make a generalized algorithm that will dynamically adapt to increasing or decreasing the number of ships.

For this algorithm, we need the language property - the name as an object identifier. The time has come for big changes, and here you, as a teacher, can tell you about such a phenomenon as refactoring. This is a sequential change of the code, improvement of its structure and its optimization. And the main thing is that every change does not break the work of the program, and the scope of changes should also be carefully selected. Too many changes are bad, too few changes are long. Formed skill refactoring will give an advantage and efficiency of the programmer. But to form a skill you need to work with your head, and not just poke. I managed to make 4 changes in the original program and the result did not change.

We try to change the decision and use the rotation in the direction of the object by name

Note that I have not yet deleted the blocks, but left them. What for? Because if something goes wrong, we can return the previous version, starting all over again. But as soon as I check the correct execution of the updated script, I will immediately remove them, so as not to be distracted by them later.

Did you notice? Now in the “turn to ...” block there is a variable and this thing works. Those. we have verified with minimal effort that this approach works. This is just awesome. Now we can start writing algorithms that will allow us to determine the object of pursuit of the nearest ship.

What is useful to us? This data structure is variable and list. From the algorithms - this is finding the minimum number (distance) in the list. We also need an algorithm that determines the name of the object to be pursued by the minimum distance.

Now a lot of code will be written. But in order not to choke and get confused in a heap of scripts, we need to use another cool function, namely the creation of our own blocks. This function is very powerful, but it is rarely used by schoolchildren, and even some programmers, and in fact it allows you to create a program that is understandable from the first reading. By the way, you can read about the techniques for creating a clear code in Martin Fowler’s book Refactoring. Improving existing code

As before any large-scale construction, we need to plan everything. Let's plan in which order we will develop the blocks:

  1. List of names of all ships
  2. Calculating the distance to all ships,
  3. Finding the minimum distance,
  4. Search for the name of the ship for pursuit by the minimum distance found.

The main rule is that each created “piece” and change must be checked.

In order to create a generalized algorithm, we need to sacrifice something, for example, the original names of sprites, we will have to standardize them and get the following alignment.

Changed sprite names

And now we are ready to describe the first part of the program. All we write for the 1st ship. Run this script separately and see the result. Go!

New block: fill in the list of ship names

If we add a new ship, we need to change only one number in this algorithm. Cool? Cool

Drive on. And let's teach him 2 algorithm. We look what happened. This is an immediate result with auxiliary functions.

New block: Fill in the list of distances

Hooray! Works. This can be checked as follows, it must be necessarily 0 one of the values ​​in the list of distances. If you have it wrong, look for an error.

Let's go further! Search for the minimum distance. Noticed that the distance is always positive. And there will always be a minimum number - 0. I-I-yay! Conclusion - look for a minimum, but not zero. You can try your hand and optimize the code yourself, so as not to do the calculation of the distance from the ship 1 to the ship 1 (this is not a typo).

Find the minimum value, but not zero

We teach the sprite of the new function and get.

New block: Find the name of the ship by distance

Do not forget to check! To do this, you can even stop the game and run a separate script.

We wrote everything that is necessary, now the main thing is to use these functions correctly.

Collected algorithm to determine the nearest ship

If everything works correctly for you, you should see the following:



Noticed that a ship with "artificial intelligence" is torn between different opponents. You can improve the program and complicate the decision. For example, if the pursued ship has little life left, then he finishes it, and if he finds it closer and with even less health, then finish off the weakest one first.

By the way, in this program you can reduce the number of actions and improve the algorithms. Try your hand. You can get this program here .

Summing up



It was on such cases that I learned how Scratch enables students to master a lot of real programming techniques faster. On it you can talk about the work of critical sections, about generalized algorithms, about variables and their scopes, about creating your own blocks and structuring code. And also in this language, you can talk about professional skills, such as refactoring.

This is only a small part of the programmer's toolkit, but it is very important. And in Scratch this is a little easier to explain.

Source: https://habr.com/ru/post/399495/


All Articles