Video - Points and Other Information in our Games
In this lesson, Stevie will talk about the “variables”.
Variables are a programming concept that is used in all programming languages.
Understanding the meaning and usefulness of variables is essential to the progress of these courses.
Watch this video more than once, freeze the picture wherever you need to read the texts more carefully and of course try the presenter's suggestions in your Scatch window.
In most computer games, the leading heroes collect points.
Suppose we create a game where a monkey is seeking bananas to eat.
But it must avoid the lions who are hunting to eat the monkey.
Every time the monkey eats bananas it earns points.
Let's see a few snapshots of such a game ...
You see how the score value changes whenever the monkey achieves its goal.
That is to eat a bunch of bananas.
The score is a variable. It is named so because the value it contains can vary during the execution of a program.
We can imagine the score as a box in our computer that at any moment may contain a different value. This value can change in a number of ways.
The programmer may set to decrease it when something occurs (for example, when the lion touches the monkey) or increase it in another case, as the programmer invents using his imagination.
Because the programmer can’t know at any moment the value he has in that box, as it can change in many ways, when he wants to use this value instead of the number he uses the box name.
Each time the programmer uses the word “score”, he chooses the number he has inside, whatever this number is.
Let's look at the game for a few seconds but from another perspective.
So we have five sprites. The bananas, the monkey and the 3 lions.
If we click on the lions to see the script assigned to each one,we will notice that each of them has a different script that allows him to have a different behavior in the game.
Let's see this in practice.
Note how the speed of lions varies.
One goes very slowly, the other a little faster and the third even faster.
If you become a little more careful you will see how the speed of one of the lions grows as the score rises.
Let's now look at the code, the way the variable score is used.
In the banana scenario, you see how the original score value is set to 0.
So every time a new game starts, the score will start from the beginning.
The following lines contain codes that will be discussed in more details in one of the following lessons of this section.
Now we can say that these lines set the number containing in the variable score changes by +1 every time the banana touches the monkey.
That is, if the variable score contains the number 0, once the banana touches the monkey - it will become 1,if it happens again, it will become 2 and will continue to increase in this way.
In the 1st and 3rd lion the variable score is not used at all.
But the second lion is the one that changes its speed according to the score.
With these lines we can achieve this result.
You see how the programmer adjusts the lion's movement according to the value of the variable score.
Note how the variable score is used. With the instruction
Move (score / 4) steps, the programmer, of course, instructs the object to move as many steps as contained within the variable score divided by 4.
As the score value increases with the evolution of the game, the lion's movement will be greater.