LibreLogo

LibreLogo 是一個簡單的、本地化的、像 Logo 語言的程式設計環境,帶有海龜向量圖繪,針對計算 (程式設計與文書處理)、DTP 與圖形設計的教學。參見 http://www.numbertext.org/logo/librelogo.pdf.

LibreLogo 工具列

The LibreLogo toolbar (View - Toolbars - Logo) contains turtle moving, program start, stop, home, clear screen, program editor/syntax highlighting/translating icons and an input bar (command line).

移動海龜的幾個圖示

它們相當於 Logo 指令 “FORWARD 10”, “BACK 10”, “LEFT 15”, “RIGHT 15”。按其中一圖示,捲動頁面到位外,也聚焦到海龜圖形。

啟動 Logo 程式

按「啟動 Logo 程式」圖示,會執行 Writer 文件中作為 LibreLogo 程式碼的文字或所選文字。於空白文件,會插入範例程式且執行之。

按「停止」圖示,就會停止執行程式。

重設

按「重設」圖示,會重新設定海龜的位置與設定。

清除螢幕

按「清除螢幕」圖示,會移除文件中的繪圖物件。

程式編輯器/語法標明/翻譯

The “magic wand” icon sets 2-page layout for program editing, expands and converts to uppercase the abbreviated, lowercase Logo commands in the Writer document. Change the language of the document (Tools - Options - Language Settings - Languages - Western) and click on this icon to translate the Logo program to the selected language.

指令列

於指令列,按 enter,就會執行指令列內的指令。按「停止」圖示就會停止程式。

按 enter 不放,就會重複執行指令列內的指令。譬如以下指令就會重複執行:

 FORWARD 200 LEFT 89

在指令列中連按三下,會重設命令;按 Ctrl+A 選取指令,並輸入新指令。

海龜基本設定的圖形化使用者介面

LibreLogo 的海龜形狀是個一般固定大小的繪圖物件。用標準方法就可定位它與旋轉它,也就是用滑鼠或繪圖物件屬性工具列中的「旋轉」圖示,就可辦到。要設定 LibreLogo 中的 PENSIZE (畫筆尺寸)、PENCOLOR (畫筆顏色)及 FILLCOLOR (填入顏色) 的屬形,就修改海龜形狀物件設定的線條寬度、線條顏色及填入色彩設定。

程式編輯

LibreLogo 在同一個 Writer 文件上繪圖及寫程式。LibreLogo 的畫紙就是 Writer 文件的第一頁。您可以在 LibreLogo 程式前插入一個頁面分隔,用 Logo 工具列上的「魔法棒」圖示縮放頁面並修改字型大小,做出一個適合程式設計的雙頁版面:左頁(第一頁)是畫紙,右頁(第二頁)是程式編輯器。

LibreLogo 程式語言

LibreLogo 是一個容易本地化的、像 Logo 的程式語言,LibreOffice 本地語言社群已經本地化數個版本。如要使用教育用的簡單 Logo 程式,LibreLogo 也能相容、接受於較早的 Logo 系統。譬如,

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

triangle 10 triangle 100 triangle 200

與 Logo 程式語言之差異

LibreLogo 的其他特點

LibreLogo 指令

基本語法

大小寫

Commands, color constants are case insensitive:

 PRINT “Hello, World!”
print “Hello, World, again!”

Variable names are case sensitive:

 a = 5
A = 7
PRINT a
PRINT A

程式行

LibreLogo 程式行是 LibreOffice Writer 文件中的段落。一行程式可寫多重指令:

 PRINT “Hello, World!” PRINT “LibreLogo”

註解

註解是從分號至行末的一行或部份行(段落):

 ; 一些註解
PRINT 5 * 5 ; 一些註解

程式一行分割成多行

於行末用波浪號,就能分割一行程式成多行:

 PRINT “This is a very long ” + ~
“warning message”

Turtle 移動

FORWARD (fd)

 FORWARD 10 ; 往前移動 10pt (1pt = 1/72 inch)
FORWARD 10pt ; 見上
FORWARD 0.5in ; 往前移動 0.5 inch (1 inch = 2.54 cm)
FORWARD 1" ; 見上
FD 1mm
FD 1cm

BACK (bk)

 BACK 10 ; 往後移動 10pt

LEFT (lt)

 LEFT 90 ; 逆時鐘轉 90 度
LEFT 90° ; 見上
LT 3h ; 見上 (至時刻位置)
LT any ; 轉至任一方位

RIGHT (rt)

 RIGHT 90 ; 順時鐘轉 90 度

PENUP (pu)

 PENUP ; 海龜將不繪圖移動

PENDOWN (pd)

 PENDOWN ; 海龜將繪圖移動

POSITION (pos)

 POSITION [0, 0] ; 轉對準畫紙左上角移動過去
POSITION PAGESIZE ; 轉對準畫紙右下角移動過去
POSITION [PAGESIZE[0], 0] ; 轉對準畫紙右上角移動過去
POSITION ANY ; 轉對準畫紙任一位置移動過去

HEADING (seth)

 HEADING 0 ; 轉向北
HEADING 12h ; 見上
HEADING [0, 0] ; 轉向畫紙左上角
HEADING ANY ; 轉向任一方向

其他海龜指令

HIDETURTLE (ht)

 HIDETURTLE ; 隱藏海龜(除非收到「顯示海龜」的指令)

SHOWTURTLE (st)

 SHOWTURTLE ; 顯示海龜

HOME

 HOME ; reset initial turtle position

CLEARSCREEN (cs)

 CLEARSCREEN ; 移除文件中的繪圖物件

FILL 與 CLOSE

 FILL ; 封閉實際線條形狀或諸點,並填上顏色
CLOSE ; 封閉實際線條形狀或連結諸點

例如:劃正三角形並上色:

 FORWARD 50 LEFT 120 FORWARD 50 FILL

例如:劃正三角形:

 FORWARD 50 LEFT 120 FORWARD 50 CLOSE

畫筆設定

PENSIZE (ps)

 PENSIZE 100 ; 線寬為100點
PENSIZE ANY ; 相當於 PENSIZE RANDOM 10

PENCOLOR/PENCOLOUR (pc)

 PENCOLOR “red” ; set red pen color (by color name, see color constants)
PENCOLOR [255, 255, 0] ; set yellow color (RGB list)
PENCOLOR 0xffff00 ; set yellow color (hexa code)
PENCOLOR 0 ; set black color (0x000000)
PENCOLOR ANY ; random color
PENCOLOR [5] ; set red color (by color identifier, see color constants)
PENCOLOR “invisible” ; invisible pen color for shapes without visible outline
PENCOLOR “~red” ; set random red color

PENTRANSPARENCY

 PENTRANSPARENCY 80 ; set the transparency of the actual pen color to 80%

PENCAP/LINECAP

 PENCAP “none” ; without extra line end (default)
PENCAP “round” ; rounded line end
PENCAP “square” ; square line end

PENJOINT/LINEJOINT

 PENJOINT “rounded” ; rounded line joint (default)
PENJOINT “miter” ; sharp line joint
PENJOINT “bevel” ; bevel line joint
PENJOINT “none” ; without line joint

PENSTYLE

 PENSTYLE “solid” ; solid line (default)
PENSTYLE “dotted” ; dotted line
PENSTYLE “dashed” ; dashed line

; custom dot–dash pattern specified by a list with the following arguments:
; – number of the neighbouring dots
; – length of a dot
; – number of the neighbouring dashes
; – length of a dash
; – distance of the dots/dashes
; – type (optional):
; 0 = dots are rectangles (default)
; 2 = dots are squares (lengths and distances are relative to the pensize)

PENSTYLE [3, 1mm, 2, 4mm, 2mm, 2] ; ...––...––...––

Fill settings

FILLCOLOR/FILLCOLOUR (fc)

 FILLCOLOR “blue” ; fill with blue color, see also PENCOLOR
FILLCOLOR “invisible” CIRCLE 10 ; unfilled circle
FILLCOLOR [“blue”, “red”] ; gradient between red and blue
FILLCOLOR [[255, 255, 255], [255, 128, 0]] ; between white and orange
FILLCOLOR [“blue”, “red”, 1, 0, 0] ; set axial gradient (with the required rotation and border settings), possible values: 0-5 = linear, axial, radial, elliptical, square and rectangle gradients
FILLCOLOR [“red”, “blue”, 0, 90, 20] ; linear with 20% border, rotated with 90 degrees from the actual heading of the turtle
FILLCOLOR [“red”, “blue”, 0, 90, 20, 0, 0, 200, 50] ; from 200% to 50% intensity
FILLCOLOR [ANY, ANY, 2, 0, 0, 50, 50] ; radial gradient with random colors and 50-50% horizontal and vertical positions of the center

FILLTRANSPARENCY

 FILLTRANSPARENCY 80 ; set the transparency of the actual fill color to 80%
FILLTRANSPARENCY [80] ; set linear transparency gradient from 80% to 0%
FILLTRANSPARENCY [80, 20] ; set linear transparency gradient from 80% to 20%
FILLTRANSPARENCY [80, 20, 1, 90] ; set axial transparency gradient rotated with 90 degrees from the actual heading of the turtle
FILLTRANSPARENCY [80, 20, 2, 0, 20, 50, 50] ; set radial transparency gradient from outer 80% to inner 20% transparency with 20% border and with 50-50% horizontal and vertical positions of the center

FILLSTYLE

 FILLSTYLE 0 ; fill without hatches (default)
FILLSTYLE 1 ; black single hatches (horizontal)
FILLSTYLE 2 ; black single hatches (45 degrees)
FILLSTYLE 3 ; black single hatches (-45 degrees)
FILLSTYLE 4 ; black single hatches (vertical)
FILLSTYLE 5 ; red crossed hatches (45 degrees)
FILLSTYLE 6 ; red crossed hatches (0 degrees)
FILLSTYLE 7 ; blue crossed hatches (45 degrees)
FILLSTYLE 8 ; blue crossed hatches (0 degrees)
FILLSTYLE 9 ; blue triple crossed
FILLSTYLE 10 ; black wide single hatches (45 degrees)

; custom hatches specified by a list with the following arguments:
; – style (1 = single, 2 = double, 3 = triple hatching)
; – color
; – distance
; – degree

FILLSTYLE [2, “green”, 3pt, 15°] ; green crossed hatches (15 degrees)

繪圖物件

CIRCLE

 CIRCLE 100 ; draw a circle shape (diameter = 100pt)

ELLIPSE

 ELLIPSE [50, 100] ; draw an ellipse with 50 and 100 diameters
ELLIPSE [50, 100, 2h, 12h] ; draw an elliptical sector (from 2h clock position to 12h)
ELLIPSE [50, 100, 2h, 12h, 2] ; draw an elliptical segment
ELLIPSE [50, 100, 2h, 12h, 3] ; draw an elliptical arc

SQUARE

 SQUARE 100 ; draw a square shape (size = 100pt)

RECTANGLE

 RECTANGLE [50, 100] ; draw a rectangle shape (50×100pt)
RECTANGLE [50, 100, 10] ; draw a rectangle with rounded corners

POINT

 POINT ; draw a point with size and color of the pen

CLOSE can join the last points, FILL can fill the shape defined by points. For example, it’s easy to draw a “flat” star starting from its center:

 PENUP
REPEAT 5 [
FORWARD 80
POINT
BACK 80
RIGHT 36
FORWARD 50
POINT
BACK 50
RIGHT 120
] FILL

LABEL

 LABEL “text” ; print text in the turtle position
LABEL 'text' ; see above
LABEL "text ; see above (only for single words)

TEXT

 CIRCLE 10 TEXT “text” ; set text of the actual drawing object

Font settings

FONTCOLOR/FONTCOLOUR

 FONTCOLOR “green” ; set font color

FONTFAMILY

 FONTFAMILY “Linux Libertine G” ; set font (family)
FONTFAMILY “Linux Libertine G:smcp=1” ; set also font feature (small caps)
FONTFAMILY “Linux Libertine G:smcp=1&onum=1” ; small caps + old figures

FONTSIZE

 FONTSIZE 12 ; set 12pt

FONTWEIGHT

 FONTWEIGHT “bold” ; set bold font
FONTWEIGHT “normal” ; set normal weight

FONTSTYLE

 FONTSTYLE “italic” ; set italic variant
FONTSTYLE “normal” ; set normal variant

PICTURE (pic)

PICTURE is for

Shape grouping

 ; PICTURE [ LibreLogo_commands ]
PICTURE [ FORWARD 100 CIRCLE 100 ] ; tree-like grouped shape

See also “Group” in LibreOffice Writer Help.

 TO tree location
PENUP POSITION location HEADING 0 PENDOWN
PICTURE [ FORWARD 100 CIRCLE 100 ] ; tree-like grouped shape
END

PICTURE [ tree [230, 400] tree [300, 400] ] ; grouped shapes in a grouped shape

Starting new line shapes

 PICTURE ; start a new line shape
FORWARD 10 PICTURE FORWARD 10 ; two line shapes

Saving SVG images

 PICTURE “example.svg” [ CIRCLE 5 ] ; save the picture as an SVG image file in the user folder
PICTURE “Desktop/example.svg” [ FORWARD 100 CIRCLE 5 ] ; as above, with a relative path
PICTURE “/home/user/example.svg” [ CIRCLE 5 ] ; absolute path for Unix/Linux
PICTURE “C:\example.svg” [ CIRCLE 5 ] ; absolute path for Windows

Saving SVG/SMIL animations (drawings with SLEEP commands)

 PICTURE “animation.svg” [ CIRCLE 5 SLEEP 1000 CIRCLE 99 ] ; save as an SVG/SMIL animation (see also SLEEP)
PICTURE “animation2.svg” [ CIRCLE 5 SLEEP 1000 CIRCLE 99 SLEEP 2000 ] ; as above, but using SLEEP after the last object will result looping: after 2 seconds the SVG animation restarts in SMIL-conformant browsers

Consistency at the left border

Use PICTURE to keep the consistency of positions and line shapes at the left border of Writer:

 PICTURE [ CIRCLE 20 POSITION [-100, 100] CIRCLE 20 ]

迴圈

REPEAT

 ; REPEAT number [ commands ]

REPEAT 10 [ FORWARD 10 LEFT 45 CIRCLE 10 ] ; repeat 10 times
 ; number is optional

REPEAT [ POSITION ANY ] ; endless loop

REPCOUNT

Loop variable (also in the FOR and WHILE loops).

 REPEAT 100 [ FORWARD REPCOUNT LEFT 90 ]

FOR IN

Loop for the list elements:

 FOR i IN [1, 5, 7, 9, 11] [
FORWARD i
LEFT 90
]

Loop for the characters of a character sequence:

 FOR i IN “text” [
LABEL i
FORWARD 10
]

WHILE

 WHILE TRUE [ POSITION ANY ] ; endless loop
WHILE REPCOUNT <= 10 [ FORWARD 50 LEFT 36 ] ; as REPEAT 10 [ ... ]

BREAK

Stop the loop.

 REPEAT [ ; endless loop
POSITION ANY
IF REPCOUNT = 100 [ BREAK ] ; equivalent of the REPEAT 100 [ ... ]
]

CONTINUE

Jump into the next iteration of the loop.

 REPEAT 100 [
POSITION ANY
IF REPCOUNT % 2 = 0 [ CONTINUE ]
CIRCLE 10 ; draw circles on every 2nd positions
]

條件

IF

 ; IF condition [ true block ]
; IF condition [ true block ] [ false block ]

IF a < 10 [ PRINT “Small” ]
IF a < 10 [ PRINT “Small” ] [ PRINT “Big” ]

AND, OR, NOT

Logical operators.

 IF a < 10 AND NOT a = 5 [ PRINT “0, 1, 2, 3, 4, 6, 7, 8 or 9” ]
IF a < 10 AND a != 5 [ PRINT “0, 1, 2, 3, 4, 6, 7, 8 or 9” ] ; as above

Subroutines

TO, END

New word (or procedure).

 TO triangle
REPEAT 2 [ FORWARD 100 RIGHT 120 ] FILL
END

REPEAT 10 [ triangle PENUP POSITION ANY PENDOWN ]

OUTPUT

Return value of the function.

 TO randomletter
OUTPUT RANDOM “qwertzuiopasdfghjklyxcvbnm”
END

PRINT randomletter + randomletter + randomletter ; print 3-letter random character sequence

STOP

Return from the procedure.

 TO example number
IF number < 0 [ STOP ]
PRINT SQRT number ; print square root
]

example 100
example -1 ; without output and error
example 25

Default variables

ANY

Default random value of colors, etc.

 PENCOLOR ANY ; random pen color

TRUE

Logical value.

 WHILE TRUE [ POSITION ANY ] ; endless loop
PRINT TRUE ; print true

FALSE

Logical value.

 WHILE NOT FALSE [ POSITION ANY ] ; endless loop
PRINT FALSE ; print false

PAGESIZE

 PRINT PAGESIZE ; print list of the page sizes in points, eg. [595.30, 841.89]

PI/π

 PRINT PI ; print 3.14159265359

Input/Output

PRINT

 PRINT “text” ; print “text” in a dialog box
PRINT 5 + 10 ; print 15

INPUT

 PRINT INPUT “Input value?” ; ask and print a string by a query dialog box
PRINT FLOAT (INPUT “First number?”) + FLOAT (INPUT “Second number?”) ; simple calculator

SLEEP

 SLEEP 1000 ; wait for 1000 ms (1 sec)

GLOBAL

Set global variables used in procedures.

 GLOBAL about
about = “LibreLogo”

TO example
PRINT about
GLOBAL about ; when we want to add a new value
about = “new value for the global variable”
END

example
PRINT about

函式

RANDOM

 PRINT RANDOM 100 ; random float number (0 <= x < 100)
PRINT RANDOM “text” ; random letter of the “text”
PRINT RANDOM [1, 2] ; random list element (1 or 2)

INT

 PRINT INT 3.8 ; print 3 (integer part of 3.8)
PRINT INT RANDOM 100 ; random integer number (0 <= x < 100)
PRINT INT “7” ; convert the string parameter to integer

FLOAT

 ; convert the string parameter to float number
PRINT 2 * FLOAT “5.5” ; print 11.0

STR

 ; convert the number parameter to string
PRINT “Result: ” + STR 5 ; print “Result: 5”
PRINT 10 * STR 5 ; print 5555555555

SQRT

 PRINT SQRT 100 ; print 10, square root of 100

SIN

 PRINT SIN 90 * PI/180 ; print 1.0 (sinus of 90° in radians)

COS

 PRINT COS 0 * PI/180 ; print 1.0 (cosinus of 0° in radians)

LOG10

 PRINT LOG10 100 ; print 2.0 (common logarithm of 100)

ROUND

 PRINT ROUND 3.8 ; print 4 (rounding 3.8)
PRINT ROUND RANDOM 100 ; random integer number (0 <= x <= 100)

ABS

 PRINT ABS -10 ; print 10, absolute value of -10

COUNT

 PRINT COUNT “text” ; print 4, character count of “text”
PRINT COUNT [1, 2, 3] ; print 3, size of the list

SET

 ; Convert list to Python set
PRINT SET [4, 5, 6, 6] ; print {4, 5, 6}
PRINT SET [4, 5, 6, 6] | SET [4, 1, 9] ; print {1, 4, 5, 6, 9}, union
PRINT SET [4, 5, 6, 6] & SET [4, 1, 9] ; print {4}, intersection
PRINT SET ([4, 5, 6, 6]) - SET [4, 1, 9] ; print {5, 6}, difference
PRINT SET [4, 5, 6, 6] ^ SET [4, 1, 9] ; print {1, 5, 6, 9}, symmetric difference

RANGE

 ; Python-like list generation
PRINT RANGE 10 ; print [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
PRINT RANGE 3 10 ; print [3, 4, 5, 6, 7, 8, 9]
PRINT RANGE 3 10 3 ; print [3, 6, 9]

FOR i IN RANGE 10 50 10 [ ; loop for [10, 20, 30, 40]
FORWARD i
LEFT 90
]

LIST

 ; remove the repeating elements of a list using set and list conversion
PRINT LIST (SET [1, 3, 5, 5, 2, 1]) ; print [1, 3, 5, 2]

TUPLE

Conversion to Python tuple (non-modifiable list)

 PRINT TUPLE [4, 5]

SORTED

It returns with a sorted list.

 PRINT SORTED [5, 1, 3, 4] ; print [1, 3, 4, 5]

SUB

Substitute character sequences using regex (regular expression) patterns.

 PRINT SUB (“t”, “T”, “text”) ; print “Text”, replacing “t” with “T”
PRINT SUB (“(.)”, “\\1\\1”, “text”) ; print “tteexxtt”, doubling every characters

SEARCH

Search character sequences patterns using regex patterns.

 IF SEARCH (“\w”, "word") [ PRINT “Letter in the word.” ]

FINDALL

Find all character sequences in the input string matching the given regex pattern.

 PRINT FINDALL(“\w+”, “Dogs, cats.”) ; print [“Dogs”, “cats”], the list of the words.

MIN

 PRINT MIN [1, 2, 3] ; print 1, the lowest element of the list

MAX

 PRINT MAX [1, 2, 3] ; print 3, the greatest element of the list

Color constants

 PENCOLOR “SILVER” ; set by name
PENCOLOR [1] ; set by identifiers
PENCOLOR “~SILVER” ; random silver color

Identifier

名稱

0

BLACK

1

SILVER

2

GRAY/GREY

3

WHITE

4

MAROON

5

RED

6

PURPLE

7

FUCHSIA/MAGENTA

8

GREEN

9

LIME

10

OLIVE

11

YELLOW

12

NAVY

13

BLUE

14

TEAL

15

AQUA

16

PINK

17

TOMATO

18

ORANGE

19

GOLD

20

VIOLET

21

SKYBLUE

22

CHOCOLATE

23

BROWN

24

INVISIBLE