34 lines
2.2 KiB
Plaintext
34 lines
2.2 KiB
Plaintext
\b;Objective
|
|
As a defense against ants attacking from all sides, adapt the program \c;Spider2\n; in order to fly at a given altitude.
|
|
|
|
\t;Program
|
|
The program necessary for this task is somewhat similar to the program \c;Spider2\n; that looks for the closest ant, turns towards it and fires:
|
|
\c;
|
|
\s;extern void object::Spider2()
|
|
\s;{
|
|
\s; object item;
|
|
\s;
|
|
\s; while (true)
|
|
\s; {
|
|
\s; item = radar(AlienSpider);
|
|
\s; turn(direction(item.position));
|
|
\s; fire(1);
|
|
\s; }
|
|
\s;}
|
|
\n;
|
|
You can copy-paste it to the editor, and adapt it. Replace \c;radar(AlienSpider);\n; by \c;radar(AlienAnt);\n;, and write before the \c;while\n; loop another loop that activates the jet beneath the \l;winged shooter\u object\botfj; until the bot reaches an altitude of 20 meters.
|
|
|
|
The instruction \c;\l;jet\u cbot\jet;()\n; controls the jet beneath winged bots. The number in brackets must range between \c;-1\n; and \c;1\n;.\c; jet(1);\n; moves the bot upward with maximum speed.\c; jet(-1);\n; moves the bot downward with maximum speed, and \c;jet(0);\n; stabilizes the altitude. As the bot must reach a precise altitude, move it upward slowly with \c;jet(0.2);\n;, wait until it has reached the desired height, then stabilize the altitude with \c;jet(0);\n;.
|
|
|
|
In order to "wait until it has reached the desired height", we need a conditional loop: the instructions inside the loop will be repeated only as long as a certain condition is true. The altitude of the bot is given by \c;position.z\n;. We already saw that \c;position\n; gives the position of the bot. And a position is given by the three coordinates x, y and z: the x-axis is the axis west-east, the y-axis is the axis south-north, and the z-axis is the vertical axis. Therefore we must wait while \c;position.z\n; is smaller than 20:
|
|
\c;
|
|
\s; while (position.z < 20)
|
|
\s; {
|
|
\s; wait(0.2);
|
|
\s; }
|
|
\n;
|
|
After waiting 0.2 seconds, the program will check if the altitude is still below 20 meters. If this is the case, it will wait a little more, if not, the program continues after the loop, i.e. after the closing brace \c;}\n;. Do not forget after the loop to stabilize the altitude with \c;jet(0);\n;.
|
|
|
|
\t;See also
|
|
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|