Describing the characters involved helps the computer understand how the game is set up. Although it is not necessary, the player usually controls some specific character in the game. This helps the game to be more engaging. The initial position and speed of the characters are also optional. If none are described, the computer will place them randomly in the playing area.
Consider the following sentences:
|
These three examples are all legitimate descriptions of the same situation. Play around and see if you can find new ways to describe it. | |
There are 30 carrots. They are scattered. | The pronoun "they" will be associated with carrots. | |
30 carrots are scattered near the top. | You have the option to specify where you'd like the objects placed. The place terms include "top", "bottom", "right", "left", and "center". This sentence will randomly scatter 30 carrots only in the top half of the playing area. | |
There are two fast foxes. | You also have the option to specify the speed of the characters. More on speed in a later tutorial. |
Note that every sentence must end with a period (.). It is important to note that the chance of misinterpretation increases when you write long and complex sentences. So it is recommended that when you write, keep sentences short and concise. Break long sentences into multiple sentences whenever possible. Also, pronouns should be used carefully, as vague pronouns lead to unclear game plans!
Finally, you can mark a sentence as a 'comment' by adding '//' at the beginning of the sentence. So, the first of the following 2 sentences will place 3 rabbits into the setting, while the second one will not place 4 foxes, since it is treated as a comment.
There are twenty rabbits scattered in the playing area.
Quiz: How would you describe 10 foxes on the top half and 5 rabbits in the bottom half of the area?
The next concepts are declaration and instantiation. Declaration denotes that some objects exist in the program, and instantiation is the creation of a number of concrete instances of a given object. In this above example sentence "There are 30 carrots", we declare that there are carrots in the program, and we are asking the program to create 30 instances of the carrot object.
Now, let us discuss more about the data associated with objects, often also referred to as "attributes," of an object. Attributes come in different types, such as numbers, colors, etc. For the carrot object, one may visualize the edible red-orange vegetable. Some attributes that may be of interest include the object's location, its size, whether it has been eaten or not (though we do not physically eat the object in the digital world), etc. Can you come up with some examples of an object from your class or home? Can you list some attributes that the object has?
A related sentence, "Foxes are fast." describes another object (a fox) which has the attribute "fast". This attribute indicates that the moving speed of the fox is higher than other objects in the game plan. Other attributes for the fox object may include its location, direction that it's traveling, etc. The carrot may or may not have a speed attribute. That is, if your game plan includes sentences that talk about carrots moving around, then the carrot object will have the speed attribute as well, even though one might find this game funny. In the virtual world, anything is possible, including carrots that run around. Similarly, if foxes never get eaten, the fox will not have an attribute to indicate whether any instance of fox has been eaten or not.
The sentence, "The carrots are scattered." will require the program to somehow allocate the 30 carrots onto the playing field by assigning specific values to the carrot's positions. Thus, scattering the carrots will involve iterating through all 30 carrot instances and individually assigning positions to each of them. Since the task of assigning a position to each carrot is the same for each instance of the carrot object, we can repeat this task over and over in a loop. Having such a loop can greatly eliminate the need to explicitly assign position values for all objects. This allows us to perform many similar actions by using the same code. Imagine the case where there are hundreds or thousands of instances of an object. Having such loops will be of great help to us.
In addition to scattering the characters, there are other ways to initially position them. Continue the tutorial to learn about them.