The robot draws |
|
Drawing one side of a square | |
Drawing a square | |
Summary | |
Loops are usefull | |
* In this chapter, you are going to make the robot draw a square with a side of 5 tiles. To draw a mark on a tile, the robot uses the instruction Mark . After marking a tile, the robot has to move and turn if necessary to mark the next tile. The number of marks to be drawn is 16 : the robot has to move and draw a mark 16 times and turn 4 times at each square corner. So the program will contain at least 36 blocks. This is really too much, most of the blocks are the same because the same action has to be repeated many times. And in case you want to draw a larger square, you have to change the whole program. So the solution is to use loops to repeat a part of the program. |
|
* Before starting programming : - Open a new program and select level 5 - The program purpose is to draw a square, so you can select this purpose in the header of the program window - Change the ground : select Ground > Modify menu, then Ground > Open menu, select the ground named Ground9x9.bog in Files folder of the tutorial, then click button Use this ground |
|
Drawing one side of a square | |
Now, you are going to write a subroutine to draw one side of the square. |
|
* The loop header contains the instruction : For tile = 1 to 4 |
|
Loop header and loop end blocks are rounded because they behave like begin and end blocks : they show the begin and the end of the flowchart to be repeated. The first executed block in the loop body is the one linked to the loop header |
|
Drawing a square | |
In order to draw a square,we have to draw a side and turn, this being repeated 4 times. So we use again a loop to draw the square. |
|
|
|
- run the program and verify the robot draws a square. | |
Draw a rectangle : Rectangle sides are not all of equal length. It is possible to change the subroutine DrawSide to draw a side the length of which is stored in a variable before subroutine call. |
|
|