image Tutorial on Size, position, speed, and simple Variable Attributes

So far we have learned to use score to keep count of the number of carrots eaten. What if we would like to change the color of the rabbit based on the number of carrots the rabbit has eaten? In such cases, score is insufficient, because when score is used as an antecedent in a conditional sentence, it can only be used for determining game win or lose.

Thus, in this lesson, we will cover a new topic: variable attributes, sometimes also referred to as variable attributes, if the variable values are integer or real types. Like Boolean attributes, variable attributes are associated with each individual object. However, its values are not just true or false, but take on any value, such as 3, 5, or 17.

To describe a variable attribute, we need to say "attribute of the object". For example, "the num_carrots_eaten of the rabbit".

Suppose we are interested in writing a game in which the rabbit collects carrots on the field. When 10 carrots are collected, the rabbit turns yellow. When 15 carrots are collected, the rabbit turns pink. How would you describe this game? Here is an example of using variable attributes:

There is a rabbit and 20 carrots.
The player controls the rabbit with the mouse.
When the rabbit touches a carrot, the carrot disappears and the num_carrots_eaten of the rabbit adds 1.
When the num_carrots_eaten of the rabbit equals 10, the rabbit turns yellow.
When the num_carrots_eaten of the rabbit equals 15, the rabbit turns pink.
When all carrots are eaten, the game ends.

You can also display the values of the variable attributes. To do so, simply add "The num_carrots_eaten of the rabbit is displayed." to the game plan. The position where the attribute will be displayed will be above the character. This position cannot be changed. You can display multiple attributes as well, as you will do in the "Try it yourself" below.

Sometimes you might want to combine variable attributes with other conditions. For example, when the num_carrots_eaten of the rabbit is greater than 10 and the rabbit sees a fox, then ... However, such compound antecedents involving variable attributes are not allowed. You must first convert the variable attribute to a modifier clause. In this running example, we would write:

When the rabbit whose num_carrots_eaten is greater than 10 sees a fox, the rabbit ...

In the above sentence, the variable attribute is converted to a modifier phrase "whose num_carrots_eaten is greater than 10". Of course, there are other ways to write this, such as converting the variable attribute to a Boolean attribute first. In this case we would write:

When the num_carrots_eaten of the rabbit is greater than 10, it becomes empowered.
When an empowered rabbit sees a fox, it ...

What would the following do?
When the fox touches a rabbit, the size of the rabbit becomes random between 10 and 20.

Other than the custom variable attributes, there are some built-in variable attributes, such as speed, size, position_x, position_y, etc. For example, the following sentences will make the rabbit increase its size when it touches a carrot, and stay on the left half of the canvas:

When the rabbit touches a carrot, the size of the rabbit increases.
When the position_x of the rabbit is greater than 300, the position_x of the rabbit decreases.


Try it yourself:

How would you modify the game plan such that the rabbit turns green when 5 carrots are eaten?

How would you add 10 apricots and also show the number of apricots eaten on the canvas?

How would you end the game when all carrots and apricots are eaten by the rabbit? (Hint: first convert the variable attributes to Boolean attributes.)

Quiz: What does the following do?

When a rabbit touches a fox, the rabbit become happy for 0.1 second and the fox becomes happy for 0.1 second.
When the position_x of the happy rabbit is greater than the position_x of the happy fox, the fox dies.

Quiz 2: What does the following do?

When the position_x of the rabbit is less than the position_x of the fox, the rabbit becomes happy for 0.1 second and the fox becomes happy for 0.1 second.
When a happy rabbit sees[56] a happy fox, the fox explodes.


Complete Sample Games using Simple Variable Attributes

// Game #1. Destroy the hamsters, piglets, and dinos in sequence.


// Game #2. Destroy the monsters by shooting the precious stones.


// Game #3. Destroy the rocks by shooting the spinstar and moving the rocks.



Next Page

Tutorials Home