RicEditor tutorial: Create a cannon game!

How to move the falling stone

This part of the program will be a new task so we have to add a WHILE structure :


When the stone falls we want it to start at a random x value. So we have to add a random number generator. We find this under NXT Programming/Numeric:

We insert this in to the WHILE structure:

So let’s have a look at the help for this command (press CTRL+H to get the help window):

This command will give us a random number between zero and the max value. the x value for the NXT screen is limited from 0 to 100. but since the width of the falling is 9 the upper limit for the random number must be 100-9=91. We right-click the lower left corner of the command and select Create constant:

Now it looks like this:

We change the number to 91:
random_91We want to give Xstone this random value. But first, to move the stone in the y direction we must add a second WHILE structure inside the first one as shown below (You’ll find the WHILE strucuture in NXT Programming/structures)::


So we add local variable inside the inner WHILE structure. We find it in NXT Programming/Structures:

local variable

We insert this:


We must configure this variable. Move the mouse-pointer over it until you get a cursor like a hand and then click on it and select Xstone

Now it looks like this:

This local variable is configured as write to by default, which is what we want now. We wire the Random value to the local variable Xstone:

The X value for the stone is set. Now we must of course change the stones Y value so as it falls down. Since the stone should fall down, the coordinate must start at its highest value which is 64. If you look at the lower left corner of the WHILE structure you can see a blue square with the letter i inside:
lowerleft_WhileThe i stands for iteration, which tells us how many times the loop has execute. We can in fact call it a loop counter. And now we are going to use this counter to change the value of the Y coordinate for the falling stone. This time I think it is best to do the programming first and then explain afterwards. First we add a local variable, and configure it to Write and Ystone:

Now it looks like this:


We add a minus comannd found in NXT Programming/Numeric:

We arrange it like this:

So we wire the counter with the lower left corner of the minus (Subtract) command:

So we move the mouse-pointer on upper left corner of the minus (subtract) command. We right-click and select create constant:

The constant that pops up we set equal to 64:

Finally we wire the subtracted value to the local varaible Ystone:

In this way the value of Ystone will decrease for ech time the WHILE strucutre runs. the first time it is 64-0=64. the second time it is 64-1=63 and so on. If we test the program like it is now the stone will fall down and never stop. But there are a couple of reasons to stop the falling stone:

  1. When the game is over (In programming it is a good thing to stop all loops when the program is finished).
  2. The stone has hit the ground.
  3. The bullet has hit the stone.

This adds up 3 cases when we want this WHILE structure to stop. If one of these three occurs the WHILE structure should stop. If we look back to page 6 where we worked with collisons, we used the AND to check out if the Y cooridnate and X coordinate for the bullet was in a specific range. We checked two condiditons at a time, now we want to check out 3. And another difference is that now when only one of the three becomes TRUE the result should be TRUE. For that reason we now must use the command OR instead of AND. And since we need more than two inputs we must use the command OR Array Elements:

OR Array Elements

We add this command and wire the output (left side connector) to the loop condition:



If we look at the help text for this command we see the input on the left side is an Boolean Array:

help for OR Array Elements

It is not easy to see, but the green wire on the left side is wider than green wire on the right side. This indicates we have an array as input. We therefore have to add the command Build Array:


We insert this and place behind the OR Array Elements:


When you insert the Build Array command it has only one input, and we want 3. To add more inputs, move the mousepointer over the command until you get an arrow:



Then move the mouse downwards until you get 3 inputs. Now it should look like this:


So we wire Build Array command to the OR Array elements:


You see we got a bad wire here. The reason is clear, the value of a Build Array command is by default set to Real. The OR Array elements need Boolean values. But we don’t care of this at the moment, at once we have wired a boolean to the Build Array this will be fixed automatically. then all the inputs and outputs will be set to Boolean because for the Build Array all the inputs and outputs must be of the same type.

So over to what should stop the WHILE structure. The first is when the game is over. We insert a local variable found at NXT Programming/Structures:

localvariable menu

We insert this one:


So we right-click the local variable and select Game Over:


Now it looks like this:


So we right-click and select Change To Read:Change_to_Read

Finally we wire to the OR Array an to the outer WHILE structure:


Notice the wire between the Build Array and OR Array Elements is no longer a bad wire. So we must check if the falling stone has hit the ground. this will happen when Ystone becomes zero. We insert an Equal to 0 command found in NXT Programming/Comparison:


We insert this, and connect the left connector to the value of Ystone, and the right connector to the middle connector of the OR Array Element:


Finally, if the bullet has hit the falling stone we must also stop the falling stone. To fix this we add a local variable for Hit? and wiring it to the Build Array in the same way as for game over. The result is shown below:


There is a problem here to solve. When the falling stone is hit, the local variable HIT? becomes TRUE and the loop ends. But to prevent the loop to stop again we must change the local variable Hit? to FALSE. And here is our problem:

  1. If the value of the local variable is TRUE it must be changed to FALSE
  2. If the local variable is FALSE it must also be FALSE.

So how to fix a test that is FALSE either the value of HIT? is TRUE or FALSE? The answer is the command select found in NXT Programming/Comparison:

We insert the Select command:


So we wire Hit? to the middle connector of the select command:


Before we go on lets look at the help text for the Select command:


Here is the resulting test. The connector t is the result when s is TRUE and f is the result if t is false. At this time we want it to be FALSE for both t and f. We have to insert a boolean constant called False found in NXT Programming/Boolean:


We add this and connect it to both the t and f connector:


The local variable Hit? is then set to the result of the select command. We insert a local variable found in NXT Programming/Structures:

local variable

We insert this one, right-click and select HIT?:


The local variable is by default configured as Write so we can just wire it to the s of the select command:


Finally to slow down the falling stone we enter at wait for msecs and wait for 50 msecs:



The following two tabs change content below.
Svein-Tore Narvestad

Svein-Tore Narvestad

I have used LEGO Mindstorms as a teacher since 1999, and have used it widely. For example, to measure noise in kindergartens, to have the students create lemonade blenders using small pumps connected to the NXT, and in process control with industrial equipment: pneumatics, industrial sensors, etc.
Svein-Tore Narvestad

Latest posts by Svein-Tore Narvestad (see all)

Leave a Reply