复习:图块工坊 II 5 分钟
go to cell把列和行变成 x 和 y。- 列 = floor((x + 240) ÷ 30) + 1。行 = floor((180 − y) ÷ 30) + 1。
- 光标吸附到鼠标下面的方格上,并读出它的字母。
快问快答
鼠标在 x 100。它在第几列?
看答案
第 12 列。100 + 240 = 340。340 ÷ 30 = 11.33。向下取整得 11,再加 1 是 12。
今天的主题 3 分钟
replace item (7) of [level v] with [thing]:一次换掉一整行- 改一个字母:用一个由 join 组成的循环,把这一行重新拼一遍
- 把 Tile 角色当成一支半透明的画刷,你点哪里,它就画哪里
- 一个有九个图块的调色板,用 ← 和 → 键来选
学习目标
上完这节课,你能够:
- 用一个循环,改掉一段文字里的一个字母。
- 改掉关卡里的一个方格,并且只重画这个方格。
- 做一个用鼠标画图块的编辑器。
学一学:给关卡画画 30 分钟
1 · 一次换掉一整行
列表有一块积木,能把一项换成另一项:replace item (7) of [level v] with [thing]。每一项都是一行,所以它换掉的是一整行。
when flag clicked
load level :: custom
draw level :: custom
wait (1.5) seconds
replace item (7) of [level v] with [#.*.*.*.*.*.*.*#]
draw level :: custom
怎么用: 点绿旗开始。别的都不用按。等一会儿,看第 7 行变了。
2 · 改一个字母:把这一行重新拼一遍
编辑器只改一个方格,也就是一行里的一个字母。可是问题来了:Scratch 没有能改掉文字里一个字母的积木。
所以要把这一行重新拼一遍。先准备一段叫 new row 的空文字。沿着旧的那一行,一个字母一个字母地把它们接上去。到了第 c 列,接上新的字母,而不是旧的。然后用 replace item 把 new row 放回去。
old row 7 #..............# column 9 ↓ new row 7 #.......K......#
把它放进一块自制积木 set tile,有三个输入:列 c、行 r,还有 new(新字母)。最后,它在那一个方格上盖出新的图块。每个图块都是一个完整的方格,连地板也画在里面,所以新的图章会把旧的完全盖住,不用再画一次整个关卡。
define set tile (c) (r) (new)
set [new row v] to []
set [i v] to (1)
repeat (16)
if <(i) = (c)> then
set [new row v] to (join (new row) (new))
else
set [new row v] to (join (new row) (letter (i) of (item (r) of [level v])))
end
change [i v] by (1)
wait (0.2) seconds
end
replace item (r) of [level v] with (new row)
go to cell (c) (r) :: custom
switch costume to (new)
stamp
when flag clicked
load level :: custom
draw level :: custom
wait (1) seconds
set tile (9) (7) [K] :: custom
new row 慢慢变长。基础任务里的版本两样都没有。怎么用: 点绿旗开始。别的都不用按。看 new row 显示框慢慢变长,然后出现一把钥匙。
3 · Tile 角色变成画刷
上节课,Cursor 跟着鼠标走。现在由 Tile 角色来做。它穿着自己马上要画的那个图块,而且是半透明的,所以你能看到它下面的方格。
set [ghost v] effect to (40)让它变成半透明。- 图章也会复制虚像(ghost)特效,图块就会变淡。所以
draw level和set tile盖图章时要把 ghost 设为 0,盖完再设回 40。 - 只有当这个方格还不是这个图块时才画。不然,按住鼠标键时,同一行每秒还是会被重新拼三十次。
when flag clicked
load level :: custom
set [brush v] to [#]
draw level :: custom
go to [front v] layer
forever
set [mouse col v] to (([floor v] of (((mouse x) + (240)) / (30))) + (1))
set [mouse row v] to (([floor v] of (((180) - (mouse y)) / (30))) + (1))
go to cell (mouse col) (mouse row) :: custom
switch costume to (brush)
if <mouse down?> then
if <not <(letter (mouse col) of (item (mouse row) of [level v])) = (brush)>> then
set tile (mouse col) (mouse row) (brush) :: custom
end
end
end
怎么用: 点绿旗,然后在一个地板方格上按住鼠标键,拖动来画砖块。
4 · 调色板
九个字母按顺序排好,就是一段文字:.#^*KDCSE。变量 pick 从 1 数到 9,(letter (pick) of [.#^*KDCSE]) 就是你拿着的那个图块。
→ 给 pick 加 1,← 减 1。超过 9 就回到 1;小于 1 就转回 9。
基础任务:点一下就能画的编辑器 25 分钟
打开你的图块工坊作品。上节课的 Cursor 角色可以删掉了,现在 Tile 角色就是光标。
第 1 步:set tile
- 建变量
new row、i、brush和pick。保留mouse col和mouse row。 - 在 Tile 角色里,做一块叫
set tile的积木,有三个输入:c、r和new。勾选 Run without screen refresh(不刷新屏幕运行)。 - 照着下面的那组积木搭出来,循环里不要放 wait。
第 2 步:半透明的画刷
- 在
draw level里,把最上面的show换成set [ghost v] effect to (0)。 - 把最下面的
hide换成set [ghost v] effect to (40)。
第 3 步:画刷循环
- 把 Cursor 的 forever 循环复制到 Tile 角色的绿旗脚本里,放在
draw level后面。 - 加上
switch costume to (brush),还有“学一学”第 3 部分的两块if积木。
第 4 步:调色板
- 在 forever 前面,把
pick设为 2,把brush设为(letter (pick) of [.#^*KDCSE])。 - 加上两个方向键脚本。勾上
brush,让它显示在舞台上。 - 点绿旗。选一个图块,然后开始画。
when flag clicked
load level :: custom
set [pick v] to (2)
set [brush v] to (letter (pick) of [.#^*KDCSE])
draw level :: custom
go to [front v] layer
forever
set [mouse col v] to (([floor v] of (((mouse x) + (240)) / (30))) + (1))
set [mouse row v] to (([floor v] of (((180) - (mouse y)) / (30))) + (1))
go to cell (mouse col) (mouse row) :: custom
switch costume to (brush)
if <mouse down?> then
if <not <(letter (mouse col) of (item (mouse row) of [level v])) = (brush)>> then
set tile (mouse col) (mouse row) (brush) :: custom
end
end
end
when [right arrow v] key pressed
change [pick v] by (1)
if <(pick) > (9)> then
set [pick v] to (1)
end
set [brush v] to (letter (pick) of [.#^*KDCSE])
when [left arrow v] key pressed
change [pick v] by (-1)
if <(pick) < (1)> then
set [pick v] to (9)
end
set [brush v] to (letter (pick) of [.#^*KDCSE])
define set tile (c) (r) (new)
set [new row v] to []
set [i v] to (1)
repeat (16)
if <(i) = (c)> then
set [new row v] to (join (new row) (new))
else
set [new row v] to (join (new row) (letter (i) of (item (r) of [level v])))
end
change [i v] by (1)
end
replace item (r) of [level v] with (new row)
go to cell (c) (r) :: custom
switch costume to (new)
set [ghost v] effect to (0)
stamp
set [ghost v] effect to (40)
怎么用: 点绿旗,然后点一下舞台。按 ← 或 → 选一个图块,然后在一个方格上按住鼠标键来画。
画出来的图块是淡的。
盖图章的时候,ghost 特效还是 40。在 stamp 前面把它设为 0,盖完马上设回 40。
图块落在了对的列,可是错的行。
检查 set tile 的输入顺序是不是先列、后行,和 go to cell 的顺序一样。
画一下,整行都变成了新图块。
if / else 的两半放反了。新字母要放在上面那一半,就是 <(i) = (c)> 成立的那一半。
调色板跳到了一支空白的画刷。
pick 变成了 0 或 10。检查那两块 if 积木有没有让它转回来:超过 9 回到 1,小于 1 回到 9。
一个新的空房间
每次都从关卡 A 开始做关卡,很快就没意思了。给编辑器一张白纸。
- 按 N,关卡变成一个空房间:四周是砖块,里面是地板。
- 然后你像平常一样在上面画。
怎么用: 点绿旗,然后点一下舞台,按 N 得到一个空房间。用 ← 和 → 选图块,按住鼠标键来画。
Teacher reveal
define empty room
delete all of [level v]
add [################] to [level v]
repeat (10)
add [#..............#] to [level v]
end
add [################] to [level v]
when [n v] key pressed
empty room :: custom
draw level :: custom
The ten middle rows are the same, so a loop adds them.
只能有一个起点
一个关卡有两个起点,就说不通了。让编辑器只保留一个。
- 画一个 S,关卡里原来的 S 就会变成地板。
- 别的图块还是照常画。
怎么用: 点绿旗,然后在任何一个地板方格上按住鼠标键,在那里画一个起点。看原来的起点消失。
Teacher reveal
define clear old start
set [row v] to (1)
repeat (12)
set [col v] to (1)
repeat (16)
if <(letter (col) of (item (row) of [level v])) = [S]> then
set tile (col) (row) [.] :: custom
end
change [col v] by (1)
end
change [row v] by (1)
end
Run without screen refresh ticked. In the brush loop, just before set tile:
if <(brush) = [S]> then
clear old start :: custom
end
撤销
每个人有时都会画错方格。给编辑器一个撤销键。
- 按 Z,你最后画的那个图块就会变回原来的样子。
- 别的图块都保持不变。
怎么用: 点绿旗,然后点一下舞台。按住鼠标键画几个方格,然后按 Z 把最后一个撤回。
Teacher reveal
A second list, undo. Just before each tile is painted, the level is copied into it; Z copies it back.
define save undo
delete all of [undo v]
set [row v] to (1)
repeat (12)
add (item (row) of [level v]) to [undo v]
change [row v] by (1)
end
define undo
delete all of [level v]
set [row v] to (1)
repeat (12)
add (item (row) of [undo v]) to [level v]
change [row v] by (1)
end
when [z v] key pressed
if <(length of [undo v]) = (12)> then
undo :: custom
draw level :: custom
end
And save undo :: custom just before set tile in the brush loop. The length check matters: press Z before painting anything and an empty undo list would be copied over the level, wiping it. A drag paints many tiles, so Z takes back only the last of them — a good thing to ask a fast pupil to improve.
镜像绘画
画板里有一支镜像画刷。也给编辑器一支:你画的每个图块都会出现两次,左右各一次,这样关卡左右两边一模一样。
怎么用: 点绿旗,然后点一下舞台。用 ← 或 → 选一个图块,然后在一个方格上按住鼠标键,两边都会画上。
Teacher reveal
In the brush loop, paint the mirror square first, then the square itself:
if <mouse down?> then
if <not <(letter (mouse col) of (item (mouse row) of [level v])) = (brush)>> then
set tile ((17) - (mouse col)) (mouse row) (brush) :: custom
set tile (mouse col) (mouse row) (brush) :: custom
end
end
Mirror square first, because set tile moves the brush. Painted second, the brush would end on the far side for a frame.
总结 5 分钟
replace item换掉一整行。Scratch 没办法只改文字里的一个字母。- 要改一个字母,就用 join 把这一行重新拼一遍,在那一列放进新字母。
- 先改列表,再在那一个方格上盖图章。每个图块都是完整的方格,所以图章会盖住原来的东西。
- 半透明的画刷会显示你要画什么。盖图章时要把虚像关掉。
- 编辑器
- 用来做东西、改东西的程序,在这里是用来做关卡的。
- 调色板
- 你可以从中选择的一组图块。
- 拼接(join)
- 把两段文字首尾连在一起。
- 撤销
- 把最后一次改动退回去。
交作业
保存你的作品,然后点绿旗:你画的关卡不见了,又回到了关卡 A。下节课会用存档码解决这个问题,还会有一只瓢虫来试玩你做的关卡。