image Tutorial on Plot - Border Control

image There are a few things a character can do when it reaches a border, such as:

Reverse direction - the character bounces off the border and moves in the opposite direction. When a fox reaches a border, it reverses direction.
Enter from the other side - the character disappears from the screen and reappears on the opposite border. When a fox reaches a border, it wraps around.
Die - the character disappears from the screen. When a fox reaches a border, it dies.

Sometimes we would like the character to wrap around, but with a new coordinates. There are two ways:

In the first sentence, the fox will exit one side and enter from the opposite border with an entirely new coordinates. This gives an illusion that a fox exits one border and a new fox enters from the other border.

In the second sentence above, the fox will exit one side and enter from the opposite border, but its coordinates will be restricted to be between 200 and 300. For example, consider the case where the fox exits the right border. Then, it will enter from the left border, but its y-coordinate will be between 200 and 300.


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!

Example 1:
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 start out moving in a random direction.
When a fox reaches a border, it reverses direction.

Example 2:
There are 10 rabbits and one fox.
The rabbits and fox wander around.
When a fox sees a rabbit, the fox chases the rabbit.
When a rabbit sees a fox, the rabbit flees the fox.
When a rabbit reaches a border, it wraps around.
When a fox reaches a border, it reverses direction.

Quiz: What will the following do?
There is one rabbit. The rabbit moves east. When the rabbit reaches a border, it reverses direction.

Will the rabbit turn around when it reaches the right border? (This is a tricky question -- remember there is an unconditional move for the rabbit)

Why will it get stuck when it reaches the right border? Well, think about it the following way: when the rabbit reaches the right border, according to our game plan it should reverse direction and move west. But after the rabbit moves west a bit, it is no longer at any border. Thus, the conditional expression no longer applies to the rabbit. In this case, it will again move east by the sentence "The rabbit moves east."

Did you get it? If so, you are exercising Computational Thinking!

How would you fix the above problem? Things to watch out for: Not all clauses make valid conditions in a conditional statement. For example, simply moving around is not a valid conditional expression. As you try to write more game-plan programs, you will gradually learn how to describe in a concise and coherent way.


Programming Concepts (optional material):
In this tutorial session, we discuss an abstract method that checks if an object reaches a border in the playing area. In addition, methods that perform reversing-direction, wrapping-around, and dying are also needed. All the implementation details for these methods are also generated automatically for you, allowing you to write your game-plan program at a much higher level of abstraction.

Decades ago, programs were written in machine language, which consists of 0s and 1s, the language that the hardware speaks. Clearly, reading a sequence of 0s and 1s is not the easiest thing to do. So, programming languages were invented, which allow programmers to write their code in language constructs that are much easier for humans to comprehend. In this sense, programming languages allow us to write programs at a higher level of abstraction relative to the machine language of 0s and 1s.

Using plain English to write a program is another step up in the abstraction ladder, in that you do not need to write the details of many of the methods. Yet you are still able to describe a very interesting and meaningful program. Thus, plain-English can be considered to be at a higher level of abstraction than existing programming languages.

Thus, you may observe that the effort to write a program becomes easier at each higher level of abstraction. This is true since the details from the lower levels have been abstracted away.

Even though writing a program is getting easier with new languages, it is still good practice to write the program piece by piece. Even with GameChangineer, we recommend that you try to see if your program is working with every additional 2 or 3 sentences that you add. This way, you only have to test a small amount of code at a time to make sure your game is working. This process of finding and correcting mistakes in computer programs is called debugging. Debugging is an important part of programing and requires the programmer to use logic.

Deductive reasoning is a form of logic that uses general observations to form a specific conclusion. When you play Sudoku, you use deductive reasoning when you come to specific conclusions about the missing numbers by observing the numbers given to you. You will use deductive reasoning as you debug your programs, since you have to make specific conclusions about why your program doesn't work.

In short, if you want to build a large game, you should start with a couple of sentences and then build on top of that. This way, you can easily test a small amount of code and then continue to build on a core of logic that you know is correct.


What happens when two characters collide? Continue to the next tutorial page!

Next page

Tutorials Home