Turtle LibreLogo
Tilbake til startsida
 

 

How to write programs in LibreLogo

In Logo and also in LibreLogo, it is possible to write program code over several lines. Open LibreLogo and hold down the Ctrl key while you press the Enter key. This will add an extra page that can be used to write the program. The drawing takes place on the first page, while the program is written on the other side, or multiple pages if necessary. It is a good practice to set Writer to display these two pages side by side on the screen.

You run your application by pressing the start button on the menu bar at the top of the page. If you have more widgets on the program page, you can select one off them in the same way as you select text in Writer. Only the selected program will be executed when you press the start button.

We starts with an example from a program we previously wrote on the command line. Write

firkant

FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90
FORWARD 100
RIGHT 90
FORWARD 100

Press the start button on the menu line to execute the program.

You see that the sequence FORWARD 100 RIGHT 90 is used three times in the program. We can simplify the writing by using the command REPEAT. Write
REPEAT 3 [ FORWARD 100 RIGHT 90 ]
FORWARD 100
and press the start button. The commands in the square brackets are run three times. The sequence is doing exactly the same as the first program.

The number 3 tells how many passes the commands inside the square brackets should be performed. Notice the space before and after the brackets. In LibreLogo square brackets are used both for lists (see later) and to group series of commands. In the latter case, the first bracket must always be followed by a space or a newline and the last bracket must have a space or a newline before it. The program could also be written as

REPEAT 3 [
  FORWARD 100 RIGHT 90
]
FORWARD 100

In this way you can make it easier to see what the program is doing and what different procedures does. Naturally you can use all the commands used from the command line in the first chapters.

Customizing functions

If you often use a figure but with slightly different values, you can define your own function which can be drawn in the size or color you define. Customized procedures always starts with the command TO and the name of the procedure and are closed with the command END. Write:

TO triangle :size
  REPEAT 3 [
    FORWARD :size
    LEFT 120
  ]
END

You call this function from triangle 100 where 100 can be any number.

A full program could be

TO triangle :size
  REPEAT 3 [
    FORWARD :size
    LEFT 120
  ]
END
triangle 50

When you define your own functions, there are some rules that must be followed. They must be called with the name exactly as written in the definition. The function “triangle” therefore must be called “triangle”, not “Triangle” or something else. The same applies for the variable ":size”. Use the same sequence of upper-case and lower-case letters you used when the variable was defined. In LibreLogo it is not necessary to mark a variable with colon, but it’s a good policy to distinguish it from function names. The word “TO” at the start of the function tells, LibreLogo, as I wrote previously, that this is a function definition. The definition is closed, as also said before, with the “END”.

Indents before the commands are not necessary for the program, but makes understanding easier.

A customized function can accept several arguments. If you needs triangles in different colors and sizes you can enter:

TO triangle :size :color
  PENCOLOR :color
  REPEAT 3 [
    FORWARD :size
    LEFT 120
  ]
END

You call this function for example with these parameters: triangle 66 "blue".

Libre Logo can also handle some mathematics, so if you wish to extend the function to draw various polygons in different sizes and colors, this is also possible. As the function now allows more than drawing triangles, I have changed its name to “polygon”. It is a good habit to give functions sensible names.

8-kant
TO polygon :sides :size :color :line
  PENCOLOR :color
  PENWIDTH :line
  REPEAT :sides [
    FORWARD :size
    LEFT 360 / :sides
  ]
END

The variable :sides get the number of sides in the polygon. The call polygon 8 100 "blue" 5 will result in a figure with 8 sides, each 100 pixels long in blue color and 5 pixels wide.

It is perhaps needless to say it, but values ​​in the call must be in the same order as they are in the definition. The first value is thus the number of sides, the second one is the size of the figure, i.e. how long each side will be, the third value sets the color and the last is the width of the pen. Mathematics is done in the line LEFT 360 / :sides. In LibreLogo a full circle is 360 degrees. To revert to the starting point inversion (LEFT) must be 360 ​​degrees divided by the count of pages. LibreLogo uses + for addition; - for subtraction; * for multiplication and / for division. Find more about this under “Mathematics”.

Long program lines

It is possible to break a program line using the character tilde (~) (or more correct: swung dash) at the end of the line:
PRINT "This is a very long " + ~
"text message"
.
This is often used to enhance the readability of program code.

Saving the program

The program code is written in LibreOffice Writer, and could then be stored in the usual way from the menu bar at the top of the window.


© Context and design: Kolbjørn StuestølStuestøl homesite (in Norwegian)Modified February 11 2016