image Tutorial on NPC Movements, Conditional and Unconditional

image So far, we have described What characters are in the game plan by the Setting. In the rest of the tutorial, we proceed to describing the Plot of the game plan.

In the plot of your game plan, you describe (1) how the NPCs move based on their relationships with other characters and (2) how the player character is controlled. Let us start with (1), how the NPCs move. Remember that we describe these for a given time instant, as in snapshot of a single frame. The plot will be repeated for every frame, even if the positions for all characters might have changed slightly from one snapshot to the next.

Furthermore, because this is object-oriented design, we describe the movements and actions in the perspective of a particular character. All sentences in your game plan should be either a conditional or unconditional statement. If you want a character to ALWAYS be doing something, use an unconditional statement. If not, use a conditional statement.

Unconditional statements, which can be overridden by conditional statements, describe what a character should do all the time. Consider the following examples of unconditional sentences about NPC movements:

The foxes move east. The foxes will always move to the right, unless another condition is met.
The foxes start out moving east. The foxes will always move to the right at the start of the game. After that, they can move in any other direction.
The foxes wander around. The foxes will always move randomly around the playing area, unless another condition is met.

We can also change the speed at which a character is moving. The default for a non-player character is 1 pixel per frame. That is, in our 600x600 canvas, it will travel all the way across from left to right in about 12 seconds, given 50 frames per second. We will come back to specify how fast a character moves later. For now, we will focus on the basics of describing movements.

In conditional statements, the keyword 'when' or 'if' comes before the antecedent clause, which is the part of the sentence which states a requirement that must be met. Only when the requirement (antecedent clause) is fulfilled can the resulting action, which is stated in the consequent clause, happen. Consider the following examples of conditional sentences about NPC movements:

When a rabbit sees a fox, the rabbit moves down. 'A rabbit sees a fox' is the antecedent clause, and 'the rabbit moves down' is the consequent clause. At any given time, if a rabbit comes close enough to a fox to 'see' it, the rabbit will move in a specific direction - in this case, down.
The rabbit moves up when it sees a fox. The dependent, or 'when,' part of the sentence doesn't always have to go first!
The foxes wander around. When a fox sees a rabbit, the fox flees the rabbit. At any given time, if a fox comes close to the rabbit, the fox will start moving away from the rabbit. Otherwise, the fox will just wander around.
When a fox sees a rabbit, the fox chases the rabbit. A character can also chase another character!

We will now discuss more about the object-oriented design. In the sentence,

We can think about it as if we put ourselves in the position of the fox. It is as if we are saying: When you write conditional statements, it may help to put yourself in the object that you wish to describe. Then, replace the first-person (I) with the object that you are describing. In this case, we replace the 'I' with the fox.

Now, seeing some character depends on the proximity of the objects involved. We can over-ride the distance by adding the distance value after the verb 'see', within square brackets []. For example:

In this above sentence, the fox can see a rabbit if the rabbit is within 200 pixels from the fox. Try changing the distance value and see how that affects the behavior of the fox!

Let us consider the following sentences containing pronouns. What do you think will happen?

How will the pronoun "it" be resolved? The rule of thumb is that if it is difficult to resolve the pronoun, the pronoun is bound to the subject (fox in both sentences). Thus, the first two sentences will be translated as "When a fox sees a rabbit, the fox chases the rabbit", while the third sentence cannot be interpreted due to ambiguity, as any character can be the aggressor and any can be the victim in the virtual world. Again, pay attention to how you use the pronouns!

So remember: if you wish for a character to do something all the time, use an unconditional statement. Otherwise, choose to write it as a conditional statement. All sentences in your game plan plot should be either a conditional or unconditional sentence.

Finally, consider the sentence
The foxes chase and eat the rabbits..
While the sentence may seem clear to you, it is actually ambiguous (unclear because there are multiple possible interpretations). Should the foxes chase or eat the rabbits? If both, then is there some sort of sequence of actions inferred by the sentence?

To answer these above questions, it might help to ask yourself to write the sentence such that every verb has a distinct, separate character. How would you re-write the sentence? One possible solution is the following:
The foxes chase the rabbits and the foxes eat the rabbits..
Now both verbs 'chase' and 'eat' have separate subjects, and it appears that the sentence is clearer (less ambiguous). However, this sentence is still ambiguous. That is, when should the foxes eat the rabbits? Do you see this problem? Will the foxes eat the rabbits wherever the rabbits are?

If we think a bit more, a clearer way to describe the above sentence is to break down the sentence into two:

Likewise, a similar sentence like "The fox chases the rabbit to eat the rabbit." should be converted to two sentences:

Therefore, in Game Changineer, in order to reduce ambiguity, it is required that every verb has its own separate subject. When you break down sentences in such a manner, you are preparing yourself to think more like a computer scientist! Remember to keep your sentences simple, concise, and unambiguous (clear).


Try it yourself: Try typing in the following into the textbox on GameChangineer, then click "Execute" near the bottom of that page to generate the code for your game plan. If there are no typos and everything is understood, click the button at the bottom to see your code in action!

There are twenty carrots scattered in the playing area.
There is one rabbit.
The player controls the rabbit with the mouse.
There are two foxes.
The foxes wander around.
When a fox sees the rabbit, the fox chases the rabbit.

Now try changing "When a fox sees the rabbit, the fox chases the rabbit." into "When a fox sees the rabbit, the fox flees the rabbit." How does your game change?

Quiz: Starting the game plan with "There is a fox and a rabbit. The player controls the rabbit with the mouse."
What is the difference between the following two sentences?

Will the fox always be chasing the rabbit in both cases?


Programming Concepts (optional material):
Recall that an object that holds a set of data and actions about itself (see Programming Concepts in Describing Setting 1 - Scattering ). In the sentence "Foxes move east," the object is 'fox', the action is 'move', and the data being changed are the position attributes.

Also recall that abstraction is when you focus on the attributes instead of the implementation details (see Programming Concepts in Describing Setting 2 - Aligning ). "Foxes move east." describes an action that every instance of the fox object can do. In particular, the action here is "move". Thus, we need to have a method, or member function, to implement this action. Here, the actions are represented by the member functions that the object can perform. For this example of "Foxes move east.", the fox object needs to have a method that acts on its position attributes, giving the illusion that it is moving.

By focusing the description of a game-plan program around the interaction among objects, you are engaged in an object-oriented (OO) design style. This style allows us to structure our programs in a way that we can focus on associating every data and action with specific objects. OO design style allows us to perform many actions easily, thereby simplifying the coding of complex tasks. For example, the two sentences "Foxes move around. Rabbits move around." both involve the action "move". But we can associate the move operation with specific objects. This is different from procedural programming paradigms, in which program actions are designed by sequentially describing a series of instructions without associating themselves with specific objects. In the above example, we cannot attach the move operation to the objects fox and rabbit in procedural programming.

However, procedural programming concepts are useful, even in object-oriented design style. They are useful for describing sequences of tasks requiring a particular ordering.

Again, the details of the method that implements moving is automatically generated by the system and is hidden away. Instead, we simply use the methods available to us to write the game plan. In other words, you do not need to describe the details of the "move" member function. Do you remember what this is called? Yes, it is abstraction. It allows us to focus on the big picture without worrying about the low-level details.

The sentence "When a fox sees a rabbit, the fox chases the rabbit." has the keyword "when" in it. This indicates that this is a conditional statement . A conditional statement contains two parts, an antecedent and a consequent. The antecedent describes the condition under which the consequent (resulting action) should be performed. That is, the consequent is performed only if the antecedent is true. So in this example, when a fox sees a rabbit, the chase method of the fox will be performed. Conversely, when a fox does not see a rabbit within its vicinity, it will not chase any rabbit.



What happens when a character hits the border (reaches the edge of the canvas)? Continue to the next section.

Next page

Tutorials Home