RicEditor tutorial: Create a cannon game

Moving the bullet

In this game we want to fire a bullet when we press the ENTER button. We are going to use the same principle as for the falling stone: we use the counter inside a WHILE structure and let it count from 0 to 64. The difference is of course that we must start the WHILE structure when the ENTER button is pressed. First we start with a “outside” WHILE structure, and make sure to wire the loop condition to the local variable Game over.

So we insert a sensor command found in NXT I/O:


We insert this command and select red NXT Buttons:
select NXT buttons

We go back to the block diagram:


Since this command by default is set to check for the ENTER button, we don’t need to set what button to check. The output from this command is yes if the button is pressed (TRUE) or no (FALSE) otherwise. This value we’ll send to a CASE structure. We find the CASE structure in the menu NXT Programming/Structure/Case Structure;


We insert this and wire it to the NXT Button command:


So let’s insert a WHILE structure in the TRUE part of the Case structure:


We want to use the counter to move the bullet. We insert a local variable found in NXT Programming/Structures:
localvariable menu

So we insert a local variable in this inner WHILE structure:

Local inserted

When it is inserted we move the mouse-pointer on it, right-click and the select Ybullet:


The local variable is configured to WRITE:


We wire the local variable to the counter (the blue I in the lower left corner):


The Y coordinate for the bullet will increase with 1 for each time the loop runs. But when it is greater than 64 it is at the top of the NXT screen and then we must terminate the WHILE structure. We insert a Greater? command found in NXT Programming/Comparison:


Inserted it looks as below:


We wire the upper left corner to the wire connected to the counter:


For the lower left corner we right-click and select Create constant. We set the value to 64:


It is easy think we now must wire the right corner of the Greater? command to the stop sign. But remember, the bullet must also stop when it hit the falling stone. We need to insert the local variable HIT? found in NXT Programming/Structures:

localvariable menu

We right-click the local variable and select HIT? from the list:


We have to change this local variable to read. Right-click and select Change To Read:

HIT Change to read

Since both conditions (YBullet>64 and the fact that the bullet has hit the stone) should terminate the WHILE structure, we must insert an OR command found in NXT Programming/Boolean:

OR Menu

We insert this and wire both conditions as shown below:

OR Wired

Finally we wire the output of the OR command to the red stop sign (the condition terminal):

STOP Wired

To slow down the bullet we insert a wait for msecs and set it to 15 msecs. This we have done before, it should now look like this:

wait 15msecs

Our loop is now finished!


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.