home *** CD-ROM | disk | FTP | other *** search
/ Revista CD Expert 51 / PCGamer51_17Demos.iso / games / colobotdemo / colobotdemo10e.exe / help / tmove2.txt < prev    next >
Text File  |  2001-07-02  |  3KB  |  69 lines

  1. \b;Exercise
  2. This exercise is very similar to the previous one. The bot must move exactly in the same way, but when writing the program, you must use a new concept that is extremely important in programming: \l;variables\u cbot\var;.
  3. We saw that all the pads are at a distance form each other of 20 meters. And all the rotations performed consist in 90 degree angles. Instead of rewriting the same values over and over again in the program, we can store them in a variable: 
  4.  
  5. Instead of:
  6. \c;\s;    move(20);
  7. \s;    turn(-90);
  8. \s;    move(20);
  9. \s;    turn(90);
  10. \s;    ...
  11. \n;
  12. We write :
  13. \c;\s;    dist = 20;
  14. \s;    dir = 90;
  15. \s;    move(dist);
  16. \s;    turn(-dir);
  17. \s;    move(dist);
  18. \s;    turn(dir);
  19. \s;    ...
  20. \n;
  21. \b;Variables
  22. A \l;variable\u cbot\var; is composed of three elements: 
  23. 1) The name
  24. 2) The type of the content
  25. 3) The content
  26.  
  27. \t;The name
  28. Use the name to refer to a variable. For example, instead of writing \c;move(20);\n;, write \c;move(dist);\n;: "dist" is the name of the variable. You can choose almost any name for a variable, for example \c;dist\n;, \c;dir\n;, \c;p2\n;, \c;a\n;, \c;x\n;, \c;nothing_2_grab\n;, etc.
  29.  
  30. \t;The type
  31. The type of a variable determines what kind of information the variable can contain. According to the type, a variable can contain a whole number, a real number, a character string, the coordinates of a point, etc. Here is a list with the most common variable types: 
  32.   o  \c;\l;int\u cbot\int;\n; for a whole number (12, -500, etc.)
  33.   o  \c;\l;float\u cbot\float;\n; for a real number (3.14, 0.2, -99.98, etc.)
  34.   o  \c;\l;string\u cbot\string;\n; for a character string ("Hello!", "Nothing to grab", etc.)
  35.   o  \c;\l;point\u cbot\point;\n; for the x,y,z-coordinates of a point in space
  36.   o  \c;\l;object\u cbot\object;\n; for the information about an object (bot, building, etc.) 
  37.  
  38. \t;The content
  39. The content of a variable can be a number, a string, coordinates, etc., according to the type of the variable. The content of a variable can change many times during the execution of a program. 
  40.  
  41. Before you can use a varible, you have to declare it. For example, before you can use the two variables \c;dist\n; and \c;dir\n;, you must declare them with the following lines: 
  42. \c;
  43. \s;    float  dist;
  44. \s;    float  dir;
  45. \n;
  46. Now you can use the two variables. To put the value 20 into \c;dist\n; and 90 into \c;dir\n;, write:
  47. \c;
  48. \s;    dist = 20;
  49. \s;    dir = 90;
  50. \n;
  51. Now you can move and turn the bot with the instructions \c;\l;move\u cbot\move;\n; and \c;\l;turn\n;\u cbot\turn;:
  52. \c;
  53. \s;    move(dist);
  54. \s;    turn(dir);
  55. \n;
  56. You can also use a whole \l;mathematical expression\u cbot\expr; instead of just the variable:
  57. \c;
  58. \s;    move(dist+100);
  59. \s;    turn(-dir);
  60. \n;
  61. The latter instruction will be needed to turn the bot left. 
  62.  
  63. Now, rewrite the program of the previous exercice, but use a variable for the distance and another variable for the angle of the rotation. 
  64.  
  65. Push the Help button \key;\key help;\norm; to see these instructions again.
  66.  
  67. \t;See also
  68. \l;Controls\u command; and \l;programming\u cbot;.
  69.