- 学一学30 分钟 · 存档码的两个方向、一个会走的角色、准备好分享
- 基础任务25 分钟 · 给你的编辑器加上保存和载入
- 挑战 1–327 分钟 · 试玩、图块的作用、完成的图块工坊
- 加分任务加分 · 保存前先检查关卡
复习:图块工坊 III 5 分钟
replace item换掉一整行;没有积木能只改一个字母。set tile用 join 把这一行重新拼一遍,然后在那一个方格上盖图章。- Tile 角色是一支半透明的画刷,用 ← → 从调色板里选。
快问快答
你在第 6 行的第 4 列画画。set tile 里的循环要拼接几个字母?
看答案
全部 16 个:十五个旧字母和一个新字母。今天你要拼接更长的东西:整个关卡。
今天的主题 3 分钟
- 存档码:把 12 行拼成一段有 192 个字母的文字
- 把存档码拆回一行一行,还有把存档码粘贴进来
- 一个试玩关卡的角色,每按一次键走一个方格
- 让编辑器准备好分享
学习目标
上完这节课,你能够:
- 把关卡变成存档码,再把存档码变回关卡。
- 让一个角色在网格上移动,每走一步以前都先读地图。
- 分享一个完成的编辑器,附上说明和一个你自己做的关卡。
学一学:存档码和会走的角色 30 分钟
1 · 为什么要存档码?
画好一个关卡,点绿旗,它就不见了。load level 每次都把关卡 A 放回去。存档码能解决这个问题:整个关卡变成一行文字,你可以复制出来、保存好,再粘贴回去。
12 行、每行 16 个字母,首尾接起来就是 12 × 16 = 192 个字母。这是关卡 A:
#################S.....#.....*.##.###..#..##...# #...#.....#..*.##.*.#..^^.#....##...####..####.# #..............##.##..*..C..##.##.#..####...#..# #.#K.#..#.^^#.*##....#E.D......#################
(为了放得下,这里分成了四行。真正的存档码只有一行,行和行之间什么都没有。)
2 · 做出存档码
先准备一个空的 code,然后从上到下,把每一行接到它的后面。把做好的存档码放进一个列表 save code,因为舞台上列表里的项可以复制出来。
define make code
set [code v] to []
set [row v] to (1)
repeat (12)
set [code v] to (join (code) (item (row) of [level v]))
change [row v] by (1)
end
delete all of [save code v]
add (code) to [save code v]
when flag clicked
load level :: custom
draw level :: custom
wait (1) seconds
make code :: custom
go to x: (15) y: (-15)
switch costume to [* v]
show
say [Saved! The whole level is one line in the list now.] for (3) seconds
怎么用: 点绿旗开始。别的都不用按。存档码会出现在上面的列表里。
3 · 把存档码拆回一行一行
第 1 行是存档码的第 1 到 16 个字母。第 2 行是第 17 到 32 个字母。所以第 row 行的第 col 列,是第 (row − 1) × 16 + col 个字母。
| 行 | 列 | 第几个字母 |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 16 | 16 |
| 2 | 1 | 17 |
| 7 | 9 | 105 |
| 12 | 16 | 192 |
两个嵌套循环,就像 draw level。内层循环把 16 个字母拼进 new row;外层循环把每一行拼好的加进列表。
define unpack code
delete all of [level v]
set [row v] to (1)
repeat (12)
set [new row v] to []
set [col v] to (1)
repeat (16)
set [new row v] to (join (new row) (letter ((((row) - (1)) * (16)) + (col)) of (code)))
change [col v] by (1)
end
add (new row) to [level v]
change [row v] by (1)
end
when flag clicked
unpack code :: custom
draw level :: custom
code 里已经放好了第二个房间的存档码,就像有人粘贴进去了一样。怎么用: 点绿旗开始。别的都不用按。存档码变成了一个关卡。
4 · 把存档码粘贴进来
ask [Paste a save code, then press Enter] and wait 会打开一个可以粘贴的框。先检查回答:如果第 192 个字母是空的,这个存档码就太短了,不能当关卡,可能复制的时候漏了一点。
when I receive [ask v]
ask [Paste a save code, then press Enter] and wait
if <(letter (192) of (answer)) = []> then
say [That code is too short. A code is 192 letters.] for (3) seconds
else
set [code v] to (answer)
broadcast [unpack v] and wait
say [Level loaded!] for (2) seconds
end
unpack 时负责拆开存档码。怎么用: 点绿旗。把一个存档码粘贴到框里,或者随便打点什么,然后按 Enter。
用第二个房间的存档码试一试:
#################S..#......#..E##...#..**..#...##.#.#......#.#.##.#...####...#.##.#*..#..#..*#.##.#####..#####.##......^^......##.###.####.###.##...#..K...#...##*..#..D...#..*#################
5 · 一个测试关卡的角色
一个关卡要有人玩过,才算真正完成。Ladybug 在网格上走,每按一次键走一个方格,而且她从来不看图。每走一步以前,她都会读出她马上要走进去的那个方格里的字母:
- 是
#,或者什么都没有(在地图外面):她待在原地。 - 是别的:她走进去,然后由这个图块来决定会发生什么。
硬币会被数上,然后它必须消失:关卡的列表在那里要写成地板。只有 Tile 角色能改关卡,所以 Ladybug 用 broadcast [pick up v] and wait 请它来改,Tile 角色就运行 set tile (hero col) (hero row) [.] :: custom。
define step (dc) (dr)
set [next col v] to ((hero col) + (dc))
set [next row v] to ((hero row) + (dr))
set [next v] to (letter (next col) of (item (next row) of [level v]))
if <not <<(next) = [#]> or <(next) = []>>> then
set [hero col v] to (next col)
set [hero row v] to (next row)
go to cell (hero col) (hero row) :: custom
if <(next) = [*]> then
change [coins v] by (1)
broadcast [pick up v] and wait
end
end
dc 和 dr 表示要移动多远:(1) (0) 是往右一格,(0) (−1) 是往上一格。要不碰键盘就测试,可以给她一条用字母写的路线(D 下、R 右、U 上、L 左),然后一次读一个字母:
when I receive [go v]
set [hero col v] to (start col)
set [hero row v] to (start row)
set [coins v] to (0)
go to cell (hero col) (hero row) :: custom
go to [front v] layer
set [route v] to [DDDRRRDDRRRDU]
set [n v] to (1)
repeat until <(letter (n) of (route)) = []>
set [move v] to (letter (n) of (route))
if <(move) = [R]> then
step (1) (0) :: custom
end
if <(move) = [L]> then
step (-1) (0) :: custom
end
if <(move) = [U]> then
step (0) (-1) :: custom
end
if <(move) = [D]> then
step (0) (1) :: custom
end
wait (0.4) seconds
change [n v] by (1)
end
say (join (join [Route done: ] (coins)) [ coins.]) for (3) seconds
start col 和 start row 来自 draw level,它会记住自己在哪里盖了 S,就像图块工坊 I 的加分任务那样。怎么用: 点绿旗开始。别的都不用按。看 Ladybug 沿着路线走,捡起两枚硬币。
6 · 准备好分享
给别人用的工具,要能自己说明白。分享以前,在作品页面上写好这些:
- Instructions(操作说明):每个键都要写:← → 选图块,按住鼠标键画画,T 用方向键试玩,C 保存存档码,L 载入存档码。
- Notes and Credits(备注与致谢):你最好的关卡的存档码,这样谁都能用 L 载入它。写上图块是谁做的。
基础任务:给你的编辑器加上保存和载入 25 分钟
打开你上节课的图块工坊编辑器。你要给它加两个键。
第 1 步:make code
- 建一个变量
code和一个列表save code。取消勾选这个列表,让它隐藏起来。 - 在 Tile 角色里,做一块叫
make code的积木(勾选 Run without screen refresh),按照“学一学”第 2 部分搭出来。
第 2 步:按 C 保存
when [c v] key pressed,然后make code :: custom。- 然后
show list [save code v],再加一句话,告诉玩家从哪里复制。 - 在画刷循环里,在
set tile前面加hide list [save code v],这样你再画的时候,列表就会让开。
第 3 步:按 L 载入
- 按照“学一学”第 3 部分,做一块叫
unpack code的积木(勾选 Run without screen refresh)。 when [l v] key pressed:隐藏列表,要一个存档码,检查第 192 个字母,然后拆开并画出来。
第 4 步:来回试一次
- 画一个关卡。按 C,复制存档码。
- 点绿旗:关卡 A 回来了。
- 按 L,粘贴你的存档码,按 Enter。你的关卡回来了。
when [c v] key pressed
make code :: custom
show list [save code v]
say [Saved! Copy the code from the list.] for (2) seconds
when [l v] key pressed
hide list [save code v]
ask [Paste a save code, then press Enter] and wait
if <(letter (192) of (answer)) = []> then
say [That code is too short. A code is 192 letters.] for (2.5) seconds
else
set [code v] to (answer)
unpack code :: custom
draw level :: custom
say [Level loaded!] for (1.5) seconds
end
怎么用: 点绿旗,然后点一下舞台。用 ← → 和鼠标键画画。按 C 做出存档码,按 L 载入一个:把存档码粘贴到框里,或者随便打点什么,然后按 Enter。
载入的关卡乱了,越往下的行乱得越厉害。
字母的编号算错了。它是 (row − 1) × 16 + col。检查 − 1 是不是在括号里面、在 × 16 前面。
列表里有很多个存档码,不是一个。
make code 最后那部分的开头,少了 delete all of [save code v]。
存档码看起来没问题,却说“太短”。
做存档码的时候,有一行只有 15 个字母。画出关卡,找一找有没有哪一行最后的图块被盖了两次。
试玩
把 Ladybug 放进你的编辑器。
- 按 T 在编辑和试玩之间切换。变量
mode说明现在是哪一种。 - 试玩时,画刷隐藏起来,Ladybug 出现在 S 上。
- 方向键让她一次走一格。墙会挡住她。
- 再按 T:Ladybug 隐藏起来,画刷回来了。
怎么用: 点绿旗,然后点一下舞台,按 T 来试玩。用方向键让 Ladybug 走,再按 T 回到编辑。
Teacher reveal
On the Tile sprite:
when [t v] key pressed
if <(mode) = [edit]> then
set [mode v] to [play]
draw level :: custom
hide
broadcast [play v]
else
set [mode v] to [edit]
show
broadcast [edit v]
end
Wrap the brush loop’s painting, and the palette keys, in an if that checks <(mode) = [edit]>. Drawing the level first finds the S, so the Ladybug is told to play only after start col is right. On the Ladybug:
when I receive [play v]
set [hero col v] to (start col)
set [hero row v] to (start row)
set [coins v] to (0)
go to cell (hero col) (hero row) :: custom
go to [front v] layer
show
when I receive [edit v]
hide
when [right arrow v] key pressed
if <(mode) = [play]> then
step (1) (0) :: custom
end
Plus the other three arrows, and step without the coin part.
图块的作用
让关卡变成一个游戏。
- 硬币让
coins加 1,然后消失。 - 尖刺说 “Ouch!”(哎哟!),把她送回起点。
- 检查点旗子会变成新的送回位置。
- 出口说 “You made it!”(你成功了!)
怎么用: 点绿旗,然后点一下舞台,按 T 来试玩。用方向键让 Ladybug 走:拿一枚硬币,碰一下旗子,再试试尖刺。
Teacher reveal
Inside step, after the coin rule:
if <(next) = [^]> then
say [Ouch!] for (0.5) seconds
set [hero col v] to (start col)
set [hero row v] to (start row)
go to cell (hero col) (hero row) :: custom
end
if <(next) = [C]> then
set [start col v] to (hero col)
set [start row v] to (hero row)
say [Checkpoint!] for (0.8) seconds
end
if <(next) = [E]> then
say [You made it!] for (2) seconds
end
The checkpoint simply moves the start. The next T redraws the level, which puts start col back on the S. Pupils will notice the coins stay gone when they go back to edit — that is Challenge 3.
完成的图块工坊
最后两条规则,让编辑器变成可以分享的作品。
- 门是锁着的。她只有捡到钥匙以后才能走过去,然后门就一直开着。
- 回到编辑模式时,每枚硬币、每把钥匙和每扇门都回到你画的地方。试玩永远不能改变你的关卡。
- C 和 L 还是能保存和载入。
怎么用: 点绿旗,然后点一下舞台。用 ← → 和鼠标键编辑。按 T 用方向键试玩,按 C 保存存档码,按 L 载入一个:把它粘贴到框里,然后按 Enter。
Teacher reveal
A backup list. T copies the level into it before play, and back out after:
when [t v] key pressed
if <(mode) = [edit]> then
set [mode v] to [play]
hide list [save code v]
copy to backup :: custom
draw level :: custom
hide
broadcast [play v]
else
set [mode v] to [edit]
copy from backup :: custom
draw level :: custom
show
broadcast [edit v]
end
The two copy blocks are Tile Maker III’s undo blocks with a different list. In step, a locked door is treated as a wall, and the key and the opened door are picked up like coins:
if <<(next) = [D]> and <(has key) = (0)>> then
say [Locked! Find the key.] for (1) seconds
set [next v] to [#]
end
if <<(next) = [*]> or <<(next) = [K]> or <(next) = [D]>>> then
broadcast [pick up v] and wait
end
if <(next) = [K]> then
set [has key v] to (1)
say [Got the key!] for (0.8) seconds
end
The first goes straight after next is set; the second inside the “not a wall” part. has key goes back to 0 whenever play starts.
保存前先检查关卡
没有起点或者没有出口的关卡,是没法玩的。不让 C 保存这样的关卡。
- 在显示存档码以前,数一数里面有几个起点、几个出口。
- 正好一个起点,并且至少一个出口:照常保存。
- 别的情况:说出哪里不对,不显示存档码。
怎么用: 点绿旗,然后点一下舞台,按 C 保存。然后用 ← 或 → 选起点图块,按住鼠标键再画一个起点,再按一次 C。
Teacher reveal
define check level
set [starts v] to (0)
set [exits v] to (0)
set [i v] to (1)
repeat (192)
if <(letter (i) of (code)) = [S]> then
change [starts v] by (1)
end
if <(letter (i) of (code)) = [E]> then
change [exits v] by (1)
end
change [i v] by (1)
end
when [c v] key pressed
make code :: custom
check level :: custom
if <(starts) = (1)> then
if <(exits) > (0)> then
show list [save code v]
say [Saved! Copy the code from the list.] for (2) seconds
else
say [Not saved: the level needs an exit.] for (2) seconds
end
else
say (join (join [Not saved: a level needs one start, not ] (starts)) [.]) for (2) seconds
end
总结 5 分钟
- 存档码就是把 12 行拼成一段有 192 个字母的文字。
- 第
row行的第col列,是存档码的第 (row − 1) × 16 + col 个字母。 - 会走的角色在移动以前先读地图:做决定的是字母,不是图。
- 让一个角色负责关卡;别的角色用消息请它来做。
- 分享出去的工具需要说明,还需要一个可以试的关卡。
- 存档码
- 把整个关卡写成一行文字,可以复制下来,再粘贴回去。
- 拆开(unpack)
- 把存档码拆回一行一行。
- 试玩
- 玩一玩你自己做的关卡,检查它能不能用。
- 备份
- 留下来的一份副本,可以用它把原来的放回去。
交作业
分享你的图块工坊,在 Instructions 里写好操作说明,在 Notes and Credits 里放上你最好的关卡的存档码。然后做一次“冷测试”:放下它十分钟,回来以后,只看屏幕上告诉你的东西来做一个关卡。下节课开始城堡攀登:你的 Tile 角色画出关卡,一个骑士读着它的字母,在上面走、掉下去、落地。再下一节课,城堡的每一层都是像你这样的存档码,骑士用你的 unpack code :: custom 把它们拆开。