复习:城堡攀登 I 5 分钟
tile at (x) (y) :: custom把一个点变成col、row,再把那里的字母放进tile。- 四个角告诉你骑士是不是在砖块里面。
- 落地时,骑士被放在它那一行顶边的上面 12。第
r行的顶边是210 − 30 × r。
快问快答
骑士的脚落在第 9 行。骑士站在哪个 y?
看答案
y −48。第 9 行的顶边是 210 − 270 = −60。再往上 12 就是 −48,也就是上节课那个突出的砖台。
今天的主题 3 分钟
- 三个关卡,用三个存档码存在一个列表里
unpack code :: custom:图块工坊里的积木,现在由骑士来运行find start :: custom:在地图上找出 SE载入下一关。^把你送回起点。
学习目标
上完这节课,你能够:
- 把好几个关卡存在一个列表里,并按编号载入其中任何一个。
- 扫描整张地图,找出一个字母在哪里。
- 做出骑士走到时会发生事情的图块。
学一学:三层楼 30 分钟
1 · 三个关卡,一个列表
一个存档码就是写成一行的一整个关卡:12 行连起来,192 个字母。所以三个关卡可以放进一个有三项的列表,叫 levels。再用一个变量 level number 说明我们在第几关。
levels 1 #################..............##..............##..............##..............##..............##..............##..............##..............##.........#....##S....^...#...E################# 2 #################..............##.............E##...........#####..............##.......###....##..............##...###........##..............##.##...........##S##...^^^^^^^.################# 3 #################E.............######..........##..............##.....####.....##..........###.##..............##......###.....##..............##...##.........##S..##^^^^^^^^.#################
- 做一个适用于所有角色的列表,叫
levels。 - 在舞台上它的显示框里,点三次 +。
- 每一项里粘贴一个存档码。拖动上面的框把它们复制下来,或者用你自己的:在图块工坊里按 C,再从
save code列表里把存档码复制出来。
2 · 拆开存档码
游戏还是一行一行地读 level 列表。所以存档码要切回 12 行,每行 16 个字母。这个积木你已经在图块工坊 IV 里写过了。给骑士一份它自己的,计数的变量叫 r 和 c(骑士的 col 和 row 是给 tile at (x) (y) :: custom 用的)。
define unpack code
delete all of [level v]
set [r v] to (1)
repeat (12)
set [new row v] to []
set [c v] to (1)
repeat (16)
set [new row v] to (join (new row) (letter ((((r) - (1)) * (16)) + (c)) of (code)))
change [c v] by (1)
end
add (new row) to [level v]
change [r v] by (1)
end
code,所以要先把这一关的存档码放进去。3 · 找到起点
每张地图都有一个 S,但位置不一样。把每个格子都读一遍。读到字母 S 时,把那个格子的中心记在 start x 和 start y 里。
define find start
set [r v] to (1)
repeat (12)
set [c v] to (1)
repeat (16)
if <(letter (c) of (item (r) of [level v])) = [S]> then
set [start x v] to ((-225) + ((30) * ((c) - (1))))
set [start y v] to ((165) - ((30) * ((r) - (1))))
end
change [c v] by (1)
end
change [r v] by (1)
end
define start level
set [code v] to (item (level number) of [levels v])
unpack code :: custom
find start :: custom
go to x: (start x) y: (start y)
set [y velocity v] to (0)
broadcast [redraw v]
redraw 时才画,不是点绿旗时画。骑士身上的 r 和 c 是仅适用于当前角色的。Tile 有它自己的一对,所以两个角色的循环永远不会把对方的计数弄乱。
怎么用: 点绿旗开始。别的都不用按:每一层会轮流被拆开、画出来。
4 · 出口
每次掉落以后,都问一问骑士的中心那里是什么。如果是 E,就进入下一关,除非这已经是最后一关。
define check tiles
tile at (x position) ((y position) - (6)) :: custom
if <<(tile) = [^]> or <(y position) < (-170)>> then
go to x: (start x) y: (start y)
set [y velocity v] to (0)
end
tile at (x position) (y position) :: custom
if <(tile) = [E]> then
next level :: custom
end
define next level
change [level number v] by (1)
if <(level number) > (length of [levels v])> then
say [You climbed the castle!] for (3) seconds
stop [all v]
end
start level :: custom
(length of [levels v]) 知道一共有几层。怎么用: 点绿旗开始。别的都不用按:骑士会自己爬完第 1 层。
5 · 尖刺
尖刺的图画占满格子的下半部分,所以检查点在骑士中心下面 6。从舞台底部掉出去也算:<(y position) < (-170)>。
怎么用: 点绿旗开始。别的都不用按:骑士先走进尖刺一次,然后跳过去。
基础任务:爬三层楼 25 分钟
接着做你的城堡攀登 I 作品。
第 1 步:存档码
- 做
levels列表,把三个存档码粘贴进去(或者用你自己的三个)。 - 做几个适用于所有角色的变量:
level number、start x、start y、code、new row。 - 在骑士身上,做仅适用于当前角色的
r和c。
第 2 步:Tile 按要求来画
- 把
load level :: custom和draw level :: custom从 Tile 的绿旗脚本里拿出来,因为现在由骑士来载入关卡。hide留在那里。 - 加上
when I receive [redraw v],下面放draw level :: custom。
第 3 步:骑士的新自制积木
- 做
unpack code :: custom、find start :: custom和start level :: custom。 - 做
check tiles :: custom和next level :: custom。这两个不要勾选:其中一个要等三秒。
第 4 步:绿旗
- 把骑士的帽子积木换成
when flag clicked。 - 把 go to 换掉:先把关卡设成第 1 关,再用 start level。
- 在 forever 里面、
fall :: custom后面,加上check tiles :: custom。 - 爬完全部三层。
when flag clicked
set rotation style [left-right v]
set size to (18) %
point in direction (90)
set [level number v] to (1)
start level :: custom
forever
if <key (right arrow v) pressed?> then
point in direction (90)
walk (4) :: custom
end
if <key (left arrow v) pressed?> then
point in direction (-90)
walk (-4) :: custom
end
if <<key (space v) pressed?> and <(on ground) = (1)>> then
set [y velocity v] to (12)
end
fall :: custom
check tiles :: custom
end
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
地图画出来了,可是骑士从地图里掉下去了。
骑士在 level 载入之前就去读它了。start level :: custom 必须放在 forever 前面。
新的一层画错了位置,或者每一行都歪了。
有个存档码不是刚好 192 个字母。每一行取接下来的 16 个,所以少一个字母,下面每一行都会歪。重新粘贴一次存档码。
在第 2 层,骑士从错的地方出发。
find start :: custom 在 unpack code :: custom 之前运行了,所以找到的是旧那一层的 S。
楼层提示
告诉玩家他们到了第几层。
- 每一层开始时,骑士说
Floor 1、Floor 2或Floor 3,说两秒。 - 提示显示的时候,玩家可以走动。
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
Teacher reveal
when I receive [banner v]
say (join [Floor ] (level number)) for (2) seconds
Plus broadcast [banner v] at the end of start level :: custom. Saying it inside the game loop instead freezes the knight for two seconds — the point of the broadcast is a second script.
第四层
加一个你自己的关卡。游戏要自己发现,不用别人告诉它。
- 在
levels里加第四个存档码:用你在图块工坊里做的,或者用下面这个。 - 提示说
Floor 1 of 4(第 1 层,共 4 层),以此类推。 - 最后一层之后游戏结束。代码里没有任何一个数字写着 4。
#################..............##..............##..............##..............##..............##..............##..............##.........##...##.....##..##...##S.^..##..##.^E#################
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
Teacher reveal
when I receive [banner v]
say (join (join [Floor ] (level number)) (join [ of ] (length of [levels v]))) for (2) seconds
next level :: custom already compares with (length of [levels v]), so the ending needs nothing new. Pupils who typed 3 there last time find out now.
测试用的选关
想测试第 3 层,不应该每次都要先爬第 1 层和第 2 层。
- 按 1、2 或 3,直接跳到那一层:地图画好,骑士站在它的 S 上。
- 按 N 跳到下一层。
- 跳过去以后,骑士永远不会还在往下掉,也不会卡在墙里。
怎么用: 点绿旗,然后在舞台上点一下。按 1、2 或 3 跳到某一层,或者按 N 跳过一层。按住 ← 或 → 走,按 space 键跳。
Teacher reveal
when [3 v] key pressed
set [level number v] to (3)
start level :: custom
when [n v] key pressed
next level :: custom
The mid-jump case works because start level :: custom sets y velocity to 0. Without that line the knight arrives on floor 3 still rising.
每一层都计时
速通玩家想知道是哪一层拖慢了他们。
- 一个叫
times的列表,点绿旗时是空的。 - 每完成一层,就把这一层用的时间(取整秒)加进列表。
- 一层的计时从这一层开始时算起,不是从点绿旗时算起。
怎么用: 点绿旗开始。别的都不用按:骑士会自己爬完全部三层。
Teacher reveal
define next level
add (round ((timer) - (floor start))) to [times v]
set [floor start v] to (timer)
change [level number v] by (1)
if <(level number) > (length of [levels v])> then
say [Three floors, three times.] for (3) seconds
stop [all v]
end
start level :: custom
Plus delete all of [times v] and set [floor start v] to (timer) under the green flag.
总结 5 分钟
- 一整个关卡可以放进一个存档码,所以很多关卡可以放进一个列表。
unpack code :: custom把存档码切回 12 行,每行 16 个字母。- 扫描每个格子就能找到一个字母;格子又能告诉你它的中心在哪里。
- 字母可以做事情:
E载入下一层,^把你送回去。
- 存档码
- 写成一行字母的一整个关卡。
- 拆开(unpack)
- 把打包好的样子,变回程序要用的样子。
- 扫描
- 一行一行地,把每个格子轮流检查一遍。
- 重生(respawn)
- 失败以后回到起点。
交作业
保存你的三层城堡。下节课,地图会在你玩的时候变化:硬币会消失,钥匙能开门,旗子会改变你重新开始的位置。