复习:城堡攀登 II 5 分钟
- 每个关卡都是
levels列表里的一个存档码。unpack code :: custom把它切开,放进level列表。 find start :: custom扫描找出 S,并记下它的中心。E载入下一关;^把骑士送回起点。
快问快答
骑士走进第 6 列、第 11 行。如果那个格子是检查点,start x 和 start y 会变成多少?
看答案
x −75,y −135,也就是那个格子的中心。−225 + 30 × 5 = −75,165 − 30 × 10 = −135。这就是今天的检查点,已经帮你算好了。
今天的主题 3 分钟
- 一张在游戏运行时会变化的地图
clear tile :: custom:把一行重新做一遍,里面放一个点- 可以收集的硬币
*和钥匙K;一扇用钥匙打开的门D - 一个检查点
C,它会改变你回来的位置
学习目标
上完这节课,你能够:
- 在游戏运行时改掉关卡里的一个字母,再重新画地图。
- 做出可以收集的图块,还有一个只有某些时候才挡路的图块。
- 玩家到达检查点时,移动重新开始的位置。
学一学:一张会变化的地图 30 分钟
1 · 改一个字母
你拿走的硬币就不能再是硬币了。在列表里,它的 * 要变成 .。你在图块工坊 III 里遇到过这个问题:Scratch 没有改一个字母的积木,所以 set tile 把那一行重新做了一遍。clear tile :: custom 是骑士的版本。它总是写一个点,写在 tile at (x) (y) :: custom 刚刚找到的 col 和 row 那里。它不盖图章,因为只有 Tile 角色才盖图章。
- 先准备一个空的
new row。 - 沿着这一行走,从第 1 个字母到第 16 个字母。
- 到了骑士所在的那一列,加一个
.。别的地方,就加原来在那里的字母。 - 把新的一行放回列表,然后让 Tile 重新画。
define clear tile
set [new row v] to []
set [i v] to (1)
repeat (16)
if <(i) = (col)> then
set [new row v] to (join (new row) [.])
else
set [new row v] to (join (new row) (letter (i) of (item (row) of [level v])))
end
change [i v] by (1)
end
replace item (row) of [level v] with (new row)
tile at (x) (y) :: custom 得到的 col 和 row。要紧接着调用它。2 · 硬币
pick up :: custom 在检查骑士中心之后运行,所以 tile 里已经是那里的字母了。
define pick up
if <(tile) = [*]> then
clear tile :: custom
change [coins v] by (1)
start sound [Coin v]
broadcast [redraw v]
end
if <(tile) = [K]> then
clear tile :: custom
change [keys v] by (1)
start sound [Collect v]
broadcast [redraw v]
end
if <(tile) = [C]> then
set [start x v] to ((-225) + ((30) * ((col) - (1))))
set [start y v] to ((165) - ((30) * ((row) - (1))))
end
怎么用: 点绿旗开始。别的都不用按:骑士会沿着那一排硬币走过去。
3 · 钥匙和门
门是一块会改变主意的砖。它住在 corner (dx) (dy) :: custom 里,骑士就是在那里问:“我是不是在硬的东西里面?”
- 没有钥匙:算撞到一次,和砖块一样。
- 有钥匙:把门清掉,用掉钥匙,不算撞到。骑士就走进去了。
define corner (dx) (dy)
tile at ((x position) + (dx)) ((y position) + (dy)) :: custom
if <(tile) = [#]> then
change [hits v] by (1)
end
if <(tile) = [D]> then
if <(keys) > (0)> then
clear tile :: custom
change [keys v] by (-1)
broadcast [redraw v]
else
change [hits v] by (1)
end
end
怎么用: 点绿旗开始。别的都不用按:骑士先试一试门,去拿钥匙,再试一次。
4 · 检查点
尖刺已经会把骑士送回 start x、start y。所以检查点只要把这两个数字改成它自己的中心就行了。
怎么用: 点绿旗开始。别的都不用按:骑士会走过旗子,然后碰到尖刺。
基础任务:钥匙关 25 分钟
接着做你的三层城堡。今天它要玩一个新关卡,里面什么都有。
#################..............##...*.*.*K.....##..########....##..............##...........##.##.....C.**.....##....######....##..............##.##.......######S...^^^.*.D..E#################
第 1 步:关卡
- 把上面的存档码粘贴进
levels,作为第 1 项(或者自己做一个,里面要有一个 K、一个 D、一些硬币和一个 C)。 - 做几个适用于所有角色的变量:
coins、keys、new row。 - 给骑士加上 Coin 和 Collect 这两个声音。
第 2 步:新的自制积木
- 做
clear tile :: custom和pick up :: custom。两个都勾上 Run without screen refresh(不刷新屏幕运行)。 - 把门加进
corner (dx) (dy) :: custom。
第 3 步:把它们连起来
- 在
check tiles :: custom里,检查完中心以后,马上调用pick up :: custom。 - 在
start level :: custom里,把keys设成 0。 - 在绿旗下面,把
coins设成 0。 - 从最上面的架子拿到钥匙,再从门出去。
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
pick up :: custom
if <(tile) = [E]> then
next level :: custom
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)
set [keys v] to (0)
broadcast [redraw v]
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
硬币拿走了,可是还在屏幕上。
列表变了,可是没有人重新画。在 clear tile :: custom 后面加 broadcast [redraw v]。
消失的是错的图块。
在检查和 clear tile :: custom 之间,又运行了一次 tile at (x) (y) :: custom,把 col 和 row 改掉了。
一个硬币算了 3 分或 4 分。
硬币算了分,可是那一行从来没被改过。检查 clear tile :: custom 最后的 replace item (row) of [level v] with (new row)。
剩下的硬币
显示外面还有几个硬币。
- 一个
coins left显示框,在关卡载入时从关卡里数出来。 - 每拿走一个硬币,它就减一。
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
Teacher reveal
define count coins
set [coins left v] to (0)
set [i v] to (1)
repeat (192)
if <(letter (i) of (code)) = [*]> then
change [coins left v] by (1)
end
change [i v] by (1)
end
Called at the end of start level :: custom, plus change [coins left v] by (-1) in the coin part of pick up :: custom. Counting the code is simpler than counting 12 rows — Tile Maker IV’s level check counted its starts and exits the same way.
变成金色的旗子
检查点应该让人看出它起作用了。
- 给 Tile 角色加一个叫
reached的造型:就是检查点图块,把旗子涂成金色。 - 你到过的检查点画成金色。其他的还是绿色。
- 不要改字母:它还是
C,这样存档格式还能用。
怎么用: 点绿旗开始。别的都不用按:骑士会走过旗子。
Teacher reveal
set [letter v] to (letter (c) of (item (r) of [level v]))
if <<(letter) = [C]> and <<(c) = (flag col)> and <(r) = (flag row)>>> then
switch costume to (reached v)
else
switch costume to (letter)
end
Inside the Tile sprite’s draw level :: custom, in place of its switch costume. The knight sets flag col and flag row from col and row in the C part of pick up :: custom, and broadcasts redraw — but only when they change, or it redraws 30 times a second while standing on the flag.
硬币关卡门
让玩家把所有东西都捡完才能离开。
- 只要还剩一个硬币,出口就什么也不做。
- 还有硬币没捡时站在出口上,骑士会说还剩几个:
2 coins to go!(还差 2 个硬币!) - 所有硬币都拿完了,出口就和以前一样能用。
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
Teacher reveal
if <(tile) = [E]> then
if <(coins left) > (0)> then
say (join (coins left) [ coins to go!]) for (1) seconds
else
next level :: custom
end
end
It needs Challenge 1’s coins left. The one-second say pauses the check, so the knight is not told 30 times a second.
跟着你走的钥匙
显示框里的一个数字很容易看漏。把骑士带着的钥匙显示出来。
- 你带着的每一把钥匙,都排成一行跟在骑士后面。
- 在门上用掉一把钥匙,跟着的就少一把。
- 最多三把钥匙。
怎么用: 点绿旗开始。别的都不用按:骑士会捡起两把钥匙,然后打开一扇门。
Teacher reveal
when flag clicked
hide
set [place v] to (1)
repeat (3)
create clone of [myself v]
change [place v] by (1)
end
when I start as a clone
forever
go to x: ((knight x) - ((16) * (place))) y: ((knight y) + (6))
if <(keys) < (place)> then
hide
else
show
end
end
place is for this sprite only, so each clone keeps its own. The knight sets knight x and knight y at the end of every tick. The demo trails the keys to the left; a strong pupil flips them with the knight’s direction.
总结 5 分钟
- 要改一个字母,就做一行新的,替换掉旧的那一行。
- 先改列表,再重新画:图跟着列表走。
- 只有你没有钥匙的时候,门才是一块砖。
- 检查点就是两个数字:骑士回来的地方。
- 改写
- 在游戏运行时修改关卡列表。
- 重画
- 根据列表,把整张地图重新盖一遍图章。
- 检查点
- 一个地方,你一到达,它就变成你新的起点。
- 收集品
- 你碰到时会被计数、然后被拿走的图块。
交作业
保存你的钥匙关。下节课,爬城堡会有生命值、每层通关时的庆祝时刻、一个标题画面,然后你要发布整个游戏。