Skip to main content

What do operators do?

Hello everyone! Today I would like to explain to you what operators in Scratch are. How these could be described in the simplest way? Well, operators are symbols or characters that perform specific actions or make calculations on given values. In Scratch we have three main types of operators: 

  1. Arithmetic operators that are used to perform mathematical calculations. Examples of these can be:
    • addition (+)  - adds two numbers
    • substraction (-)  - substract second number from the first one
    • multiplication (*)  - multiplies two numbers
    • division (\) - divides one number by the other
  2. Comparison operators that are used to compare two values and return true or false (boolean value). Examples from that group are: 
    • equal (=) - returns true if two values are equal, false otherwise
    • not equal (<>) - returns true if two values are not equal, false otherwise
    • greater than (>) - returns true if the first number is greater than the other, false otherwise
    • less than (<) - returns true if the first number is smaller then the other, false otherwise
  3. Logical operators: these are used to combine or modify conditions (logical statement). Some examples include:
    • And
    • Or 
    • Not
These operators can be used in Scratch command blocks to perform various actions such as changing variables, making decisions with if statements or creating complex calculations. We are going to take a closer look at the operators usege later on in this article.

There is also few other operators used for string operations but we are not going to cover those in this example.

To see how operators are used in practice, let's try to write an easy game where Franky moves around the stage and tries to collect all the strawberies that show up in the random places on the screen but there is a catch!  At the same time stones shows up together with strawberies. If Franky eats a rock, he is going to get a terrible stomach ache and games ends after eating, let's say, 3 rocks. 

First thing to do is to load Frankys costume file which contains a sprite of our hero. To make the game more colorful, let’s pick up a nice and cozy backdrop. I have chosen a Forest from rich Scratch library. Then we add two sprites: Rocks and Strawbery to the scene. As a last step of our preparation we create another backgrop with a huge "GAME OVER" text in the middle. 

As we are ready with a preperation, we can finaly scratch some code!

Our first goal is to start moving Franky around with arrow buttons. To do this we have to check which arrow has been clicked. Do you remember our article about conditionals? Yes, we are going to use if statements sorrounded with forever loop. In each case we are going to change coordinates of sprite so that it moves around the stage. To achieve that we will use set x to ... and set y to ... blocks to set new position abd to increase or decrease vertical or horisontal position we will use arithmetic operators. Sou our code of Frankys sprite should looks as follows:

Scratch listing 1

At the first glance it might look a bit complex but actually we are navigating our sprite through screen coordinates (please find details in Scratch Wiki site: https://en.scratch-wiki.info/wiki/File:Xy-grid.gif).
Next, let's go to Rocks and Strawberry sprites accordingly and add a piece of code which places these collectibles on the screen after green flag was clicked.


As you can see pick random ... to ... operator was used here. What it does? It give us a random number in within given range. So as long as we stick to Scratch standard resolution and coordinates we should get position which places somwhere on the screen but not outside its boarders.
Now we need to create two variables for our use:
  • Score - this is going to be a counter of strawberries collected
  • Lives - number of lieves of our hero. That value is going to be decreased every time Franky eats a rock

Next, let's switch back to Frankys code and make sure we can react in situations when we collect one of our items:

Except for arithmetic operator of addition, we also have logical operator and which checks if both its arguments are true. Otherwise it returns value of false and as a result such an if statement won't be considered. As you can clearly see, in case when we collect strawberry items on the map are placed again under a random coordinates. Similarly when we have collected rocks and we still have some lives left. In case when it was our last life we would like to change our backdrop to "GAME OVER" and hide all the sprites, so in Strawbery and Rocks code section we have to add following blocks sequence to react on reveiving newly defined messages (our post about sending messages):


In addition, all the sprites (Franky, Rocks and Strawberry) should contain following snippet that hides it when game is over.


Last thing to do is to make sure that our backdrop shows up correctly. In order to do this select our backdrop and go to its code section. Our goal here is to make sure that Score and Lives is set to its initial value and also set up default backdrop to Forest. Additionaly if GameOver message is received we switch backdrop to greated by us, showing "GAME OVER" text.




Now feel free to click the green flag to see the result. As you can see the use of operators allow us not only to change the position of Franky on the screen by adding \ decreasing his coordinates but also build more complex if conditions, claculating random number in range or compare values such as lives.








If you did not manage to get here due to various reason with your code, please find my solution attached here.

As always hope that you had some fun and I wish you happy scratching!

Comments