LibreLogo
LibreLogo is a simple, localized, Logo-like programming environment with turtle vector graphics for teaching of computing (programming and word processing), DTP and graphic design. See http://www.numbertext.org/logo/librelogo.pdf.
LibreLogo 工具栏
The LibreLogo toolbar (
) 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程序
Click on the icon “Start Logo program” to execute the text (or only the selected) text of the Writer document as a LibreLogo program. In an empty document an example program will be inserted and executed.
Click on the icon “Stop” to stop the program execution.
主页
点击“归位”图标可重置海龟的位置与设置。
清屏
点击“清屏”图标可删除文档中绘制的对象。
程序编辑器/代码高亮/翻译
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 (
) and click on this icon to translate the Logo program to the selected language.命令行
Hit Enter in the command line to execute its content. To stop the program use the icon “Stop”.
按回车键可重复命令行,如对于下述命令序列:
FORWARD 200 LEFT 89
要重置命令行,请在命令行处三击,或按 Ctrl+A 选择先前命令,再输入新命令。
基本乌龟设置的图形用户界面
Turtle shape of LibreLogo is a normal fixed size drawing object. You can positionate and rotate it on standard way, too, using the mouse and the Rotate icon of the Drawing Object Properties toolbar. Modify Line Width, Line Color and Area Color settings of the turtle shape to set PENSIZE, PENCOLOR and FILLCOLOR attributes of LibreLogo.
程序编辑
LibreLogo drawings and programs use the same Writer document. The LibreLogo canvas is on the first page of the Writer document. You can insert a page break before the LibreLogo programs and set page zoom using the “magic wand” icon of the Logo toolbar, also change the font size for a comfortable 2-page layout for LibreLogo programming: left (first) page is the canvas, right (second) page is the LibreLogo program editor.
LibreLogo 编程语言
LibreLogo is an easily localizable, Logo-like programming language, localized in several languages by LibreOffice native language communities. It is back-compatible with the older Logo systems in the case of the simple Logo programs used in education, eg.
TO triangle :size
REPEAT 3 [
FORWARD :size
LEFT 120
]
END
triangle 10 triangle 100 triangle 200
与 Logo 编程语言的不同之处
-
列表成员以逗号分隔: POSITION [0, 0]
-
程序块与列表不同
-
-
程序块需加括号,并用空格或换行分隔语句: REPEAT 10 [ FORWARD 10 LEFT 36 ]
-
列表需要闭合括号: POSITION [0, 0] 而不是 POSITION [ 0, 0 ]
-
-
不支持单行函数声明 (TO 与 END 需要独立成行)。
LibreLogo 的其他功能
-
变量名称前后的冒号可选。
TO triangle size
REPEAT 3 [ FORWARD size LEFT 120 ]
END -
字符串常量支持同样合理,采用 Python 语法。
PRINT "word ; 原 Logo 语法
PRINT “任意文本,1 2 3。” ; 正写法,Writer
PRINT '任意文本,1 2 3。' ; Python 语法 -
Python 列表与字符串处理
PRINT “text”[2] ; 输出 “x”
PRINT “text”[1:3] ; 输出 “ex” -
类 Python 的 FOR 循环
-
类 Python 的变量声明:
x = 15
PRINT x -
没有额外的查询函数:
PRINT FILLCOLOR
p = POSITION
PRINT p
REPEAT 10 [ POSITION ANY POSITION p ] -
函数调用时括号可选
TO star size color
FILLCOLOR color
REPEAT 5 [ LEFT 72 FORWARD size RIGHT 144 FORWARD size ]
FILL
END
star 100 “red”
star (100, “green”)
star(100, “blue”)
LibreLogo 命令
基本语法
大小写敏感性
命令、颜色常量不区分大小写:
PRINT “Hello, World!”
print “Hello, World, again!”
变量名区分大小写:
a = 5
A = 7
PRINT a
PRINT A
程序行
LibreLogo 程序的行就是 LibreOffice Writer 文档中的段落。程序行可包含多条命令:
PRINT “Hello, World!” PRINT “LibreLogo”
注释
分号开始直到行(段落)末尾的部分或整行为注释:
; 一些注释
PRINT 5 * 5 ; 一些注释
跨段落的程序行
可以通过行末的波浪线符号将程序行分散在多个段落中:
PRINT “这是一行非常长的 ” + ~
“警告消息”
海龟移动
FORWARD (fd)
FORWARD 10 ; 向前移动 10 磅 (1磅 = 1/72 英寸)
FORWARD 10pt ; 参见上行
FORWARD 0.5in ; 向前移动 0.5 英寸 (1 英寸 = 2.54 厘米)
FORWARD 1" ; 参见上文
FD 1mm
FD 1cm
BACK (bk)
BACK 10 ; 向后移动 10 磅
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 ; turn north
HEADING 12h ; see above
HEADING [0, 0] ; turn to the top-left corner
HEADING ANY ; turn to a random direction
其他海龟命令
HIDETURTLE (ht)
HIDETURTLE ; 隐藏海龟 (直到执行到 showturtle 命令)
SHOWTURTLE (st)
SHOWTURTLE ; 显示海龟
HOME
HOME ; reset initial turtle position
CLEARSCREEN (cs)
CLEARSCREEN ; 移除文档的绘图对象
FILL 与 CLOSE
FILL ; close and fill the actual line shape or points
CLOSE ; close the actual line shape or join the actual points
Example: filling a regular triangle:
FORWARD 50 LEFT 120 FORWARD 50 FILL
Example: drawing a regular triangle:
FORWARD 50 LEFT 120 FORWARD 50 CLOSE
笔迹设置
PENSIZE (ps)
PENSIZE 100 ; 线条宽度为 100 点
PENSIZE ANY ; 等效于 PENSIZE RANDOM 10
PENCOLOR/PENCOLOUR (pc)
PENCOLOR “red” ; 设置红色笔迹 (参数为颜色名称,参见颜色常量)
PENCOLOR [255, 255, 0] ; 设置黄色 (RGB 列表)
PENCOLOR 0xffff00 ; 设置黄色 (十六进制颜色代码)
PENCOLOR 0 ; 设置黑色 (0x000000)
PENCOLOR ANY ; random color
PENCOLOR [5] ; 设置红色 (参数为颜色 ID,参见颜色常量)
PENCOLOR “invisible” ; 透明笔迹颜色,供没有可见边缘的形状使用
PENCOLOR “~red” ; 设置随机红色
PENTRANSPARENCY
PENTRANSPARENCY 80 ; 将实际的笔颜色透明度设置为80%
PENCAP/LINECAP
PENCAP “none” ; without extra line end (default)
PENCAP “round” ; rounded line end
PENCAP “square” ; square line end
PENJOINT/LINEJOINT
PENJOINT “rounded” ; 圆形线条接头 (默认)
PENJOINT “miter” ; 锋利线条接头
PENJOINT “bevel” ; 斜面线条接头
PENJOINT “none” ; 无线条接头
PENSTYLE
PENSTYLE “solid” ; 实线 (默认)
PENSTYLE “dotted” ; 点线
PENSTYLE “dashed” ; 虚线
; 下述参数可通过列表指定自定义的点线纹路:
; – 相邻点数
; – 点长
; – 相邻线条数
; – 线长
; – 点/线距离
; – 类型 (可选):
; 0 = 矩形点 (默认)
; 2 = 方形点 (长度与距离与笔迹尺寸相关)
PENSTYLE [3, 1mm, 2, 4mm, 2mm, 2] ; ...––...––...––
填充设置
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 ; 全部涂满 (默认)
FILLSTYLE 1 ; 黑色,单方向空隙 (水平)
FILLSTYLE 2 ; 黑色,单方向空隙 (45 度)
FILLSTYLE 3 ; 黑色,单方向空隙 (-45 度)
FILLSTYLE 4 ; 黑色,单方向空隙 (垂直)
FILLSTYLE 5 ; 红色交叉空隙 (45 度)
FILLSTYLE 6 ; 红色交叉空隙 (0 度)
FILLSTYLE 7 ; 蓝色交叉空隙 (45 度)
FILLSTYLE 8 ; 蓝色交叉空隙 (0 度)
FILLSTYLE 9 ; 蓝色三重交叉
FILLSTYLE 10 ; 黑色单方向宽空隙 (45 度)
; 通过下述参数用列表指定自定义空隙:
; – 样式 (1 = 单方向, 2 = 双方向, 3 = 三方向)
; – 颜色
; – 距离
; – 角度
FILLSTYLE [2, “green”, 3pt, 15°] ; 绿色交叉空隙 (15 度)
绘制对象
CIRCLE
CIRCLE 100 ; 绘制圆圈图形 (直径 = 100pt)
ELLIPSE
ELLIPSE [50, 100] ; 绘制直径分别为 50 和 100 的椭圆
ELLIPSE [50, 100, 2h, 12h] ; 绘制椭圆扇区 (从 2h 到 12h 的时钟位置)
ELLIPSE [50, 100, 2h, 12h, 2] ; 绘制椭圆部分
ELLIPSE [50, 100, 2h, 12h, 3] ; 绘制椭圆弧线
SQUARE
SQUARE 100 ; 绘制正方形 (尺寸 = 100pt)
RECTANGLE
RECTANGLE [50, 100] ; draw a rectangle shape (50×100pt)
RECTANGLE [50, 100, 10] ; draw a rectangle with rounded corners
POINT
POINT ; 以笔迹尺寸与颜色绘制一个点
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” ; 在海龟位置输出 text
LABEL 'text' ; 参见上行
LABEL "text ; 参见上行 (仅适用于单个单词)
TEXT
CIRCLE 10 TEXT “文字” ; 设置实际绘制对象的文字内容
字体设置
FONTCOLOR/FONTCOLOUR
FONTCOLOR “green” ; 设置字体颜色
FONTFAMILY
FONTFAMILY “Linux Libertine G” ; 设置字体(族)
FONTFAMILY “Linux Libertine G:smcp=1” ; 同时设置字体功能 (小型大写)
FONTFAMILY “Linux Libertine G:smcp=1&onum=1” ; 小型大写 + 旧式字体
FONTSIZE
FONTSIZE 12 ; 设置 12pt
FONTWEIGHT
FONTWEIGHT “bold” ; 设置粗体
FONTWEIGHT “normal” ; 设置正常粗细
FONTSTYLE
FONTSTYLE “italic” ; 设置倾斜变体
FONTSTYLE “normal” ; 设置常规变体
PICTURE (pic)
PICTURE 用于
-
形状组合;
-
开始新行形状;
-
保存SVG图像和SVG/SMIL动画;
-
在左边框保持位置与线形的一致性。
形状组合
; PICTURE [ LibreLogo_commands ]
PICTURE [ FORWARD 100 CIRCLE 100 ] ; 像树一样的组合形状
请参见 LibreOffice Writer 帮助中的“组合”章节。
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
开始新行形状
PICTURE ; 开始新一行形状
FORWARD 10 PICTURE FORWARD 10 ; 两行形状
保存SVG图像
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
保存SVG/SMIL动画(使用SLEEP命令绘制)
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
左边框一致
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 数字 [ 命令 ]
REPEAT 10 [ FORWARD 10 LEFT 45 CIRCLE 10 ] ; 重复 10 次
; 数字是可选的
REPEAT [ POSITION ANY ] ; 无休止循环
REPCOUNT
循环变量 (在 FOR 与 WHILE 循环中也存在)。
REPEAT 100 [ FORWARD REPCOUNT LEFT 90 ]
FOR IN
按列表元素循环:
FOR i IN [1, 5, 7, 9, 11] [
FORWARD i
LEFT 90
]
按字符序列中的字符循环:
FOR i IN “text” [
LABEL i
FORWARD 10
]
WHILE
WHILE TRUE [ POSITION ANY ] ; 无休止循环
WHILE REPCOUNT <= 10 [ FORWARD 50 LEFT 36 ] ; 与 REPEAT 10 [ ... ] 类似
BREAK
停止循环。
REPEAT [ ; 无休止循环
POSITION ANY
IF REPCOUNT = 100 [ BREAK ] ; 等效于 REPEAT 100 [ ... ]
]
CONTINUE
跳到下一次循环。
REPEAT 100 [
POSITION ANY
IF REPCOUNT % 2 = 0 [ CONTINUE ]
CIRCLE 10 ; 在每个偶数位置绘制圆圈
]
条件
IF
; IF 条件 [ 真值块 ]
; IF 条件 [ 真值块 ] [ 假值块 ]
IF a < 10 [ PRINT “小” ]
IF a < 10 [ PRINT “小” ] [ PRINT “大” ]
AND、OR、NOT
逻辑运算符。
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” ] ; 同上
子过程
TO, END
新单词 (或过程)。
TO triangle
REPEAT 2 [ FORWARD 100 RIGHT 120 ] FILL
END
REPEAT 10 [ triangle PENUP POSITION ANY PENDOWN ]
OUTPUT
函数返回值。
TO randomletter
OUTPUT RANDOM “qwertzuiopasdfghjklyxcvbnm”
END
PRINT randomletter + randomletter + randomletter ; 输出 3 个字母长的随机字符序列
STOP
从过程返回。
TO example number
IF number < 0 [ STOP ]
PRINT SQRT number ; 输出平方根
]
example 100
example -1 ; 没有输出结果,也不会出错
example 25
默认变量
ANY
颜色等的默认变量值。
PENCOLOR ANY ; 随机笔迹颜色
TRUE
逻辑值。
WHILE TRUE [ POSITION ANY ] ; 无休止循环
PRINT TRUE ; 输出 true
FALSE
逻辑值。
WHILE NOT FALSE [ POSITION ANY ] ; 无休止循环
PRINT FALSE ; 输出 false
PAGESIZE
PRINT PAGESIZE ; 输出页面尺寸列表,单位为点,如 [595.30, 841.89]
PI/π
PRINT PI ; 输出 3.14159265359
输入/输出
PRINT “文字” ; 在对话框中输出“文字”
PRINT 5 + 10 ; 输出 15
INPUT
PRINT INPUT “输入数值?” ; 要求并通过请求对话框输出字符串
PRINT FLOAT (INPUT “第一个数字?”) + FLOAT (INPUT “第二个数字?”) ; 简单计算
SLEEP
SLEEP 1000 ; 等待 1000 ms (1 秒)
GLOBAL
设置用于过程的全局变量。
GLOBAL about
about = “LibreLogo”
TO example
PRINT about
GLOBAL about ; 希望添加新数值时
about = “全局变量的新数值”
END
example
PRINT about
函数
RANDOM
PRINT RANDOM 100 ; 随机浮点数 (0 <= x < 100)
PRINT RANDOM “text” ; “text”中的任一字母
PRINT RANDOM [1, 2] ; 随机列表元素 (1 或 2)
INT
PRINT INT 3.8 ; 输出 3 (3.8 的整数部分)
PRINT INT RANDOM 100 ; 随机整数 (0 <= x < 100)
PRINT INT “7” ; 将字符串参数转换为整数
FLOAT
; 将字符串参数转换为浮点数
PRINT 2 * FLOAT “5.5” ; 输出 11.0
STR
; 将数字参数转换为字符串
PRINT “结果: ” + STR 5 ; print “结果: 5”
PRINT 10 * STR 5 ; 输出 5555555555
SQRT
PRINT SQRT 100 ; 输出 10,即 100 的平方根
SIN
PRINT SIN 90 * PI/180 ; 输出 1.0 (90° 角的正弦值)
COS
PRINT COS 0 * PI/180 ; 输出 1.0 (0° 角的余弦值)
LOG10
PRINT LOG10 100 ; 输出 2.0 (100的常用对数)
ROUND
PRINT ROUND 3.8 ; 输出 4 (3.8 四舍五入后的结果)
PRINT ROUND RANDOM 100 ; 随机整数 (0 <= x <= 100)
ABS
PRINT ABS -10 ; 输出 10,即 -10 的绝对值
COUNT
PRINT COUNT “text” ; 输出 4,即“text”的字符个数
PRINT COUNT [1, 2, 3] ; 输出 3,即列表大小
SET
; 将列表转换为 Python 集
PRINT SET [4, 5, 6, 6] ; 输出 {4, 5, 6}
PRINT SET [4, 5, 6, 6] | SET [4, 1, 9] ; 输出 {1, 4, 5, 6, 9},并集
PRINT SET [4, 5, 6, 6] & SET [4, 1, 9] ; 输出{4},交集
PRINT SET ([4, 5, 6, 6]) - SET [4, 1, 9] ; 输出 {5, 6},差集
PRINT SET [4, 5, 6, 6] ^ SET [4, 1, 9] ; 输出 {1, 5, 6, 9}, 对称差集
RANGE
; 类 Python 的列表生成
PRINT RANGE 10 ; print [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
PRINT RANGE 3 10 ; 输出 [3, 4, 5, 6, 7, 8, 9]
PRINT RANGE 3 10 3 ; 输出 [3, 6, 9]
FOR i IN RANGE 10 50 10 [ ; 循环体 [10, 20, 30, 40]
FORWARD i
LEFT 90
]
LIST
; 通过 set 与 list 将列表重复元素移除
PRINT LIST (SET [1, 3, 5, 5, 2, 1]) ; 输出 [1, 3, 5, 2]
TUPLE
转换为 Python 元组 (不可修改列表)
PRINT TUPLE [4, 5]
SORTED
返回排序后的列表。
PRINT SORTED [5, 1, 3, 4] ; 输出 [1, 3, 4, 5]
SUB
用正则表达式匹配规则替换字符序列。
PRINT SUB (“t”, “T”, “text”) ; 输出 “Text”,将 “t” 替换为了 “T”
PRINT SUB (“(.)”, “\\1\\1”, “text”) ; 输出 “tteexxtt”,每个字符重复两遍
SEARCH
使用正则表达式匹配规则搜索字符序列。
IF SEARCH (“\w”, "word") [ PRINT “Letter in the word.” ]
FINDALL
查找与给定正则表达式匹配的所有字符序列。
PRINT FINDALL(“\w+”, “Dogs, cats.”) ; 输出 [“Dogs”, “cats”],即单词列表。
MIN
PRINT MIN [1, 2, 3] ; 输出 1,列表中最小的元素
MAX
PRINT MAX [1, 2, 3] ; 输出 3,列表的最大元素
颜色常量
PENCOLOR “SILVER” ; 按颜色名称设置
PENCOLOR [1] ; 按颜色 ID 设置
PENCOLOR “~SILVER” ; 随机银色
ID |
名称 |
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 (透明) |