复习:城堡攀登 III 5 分钟
clear tile :: custom把一行重新做一遍,在骑士所在的地方放一个点,然后地图重新画一次。- 硬币和钥匙会被计数并清掉。你带着钥匙,门就会打开。
- 检查点会改变
start x和start y。
快问快答
第 11 行是 #S.*..^...#.*.E#。骑士拿走了第 4 列的硬币。现在第 11 行是什么?
看答案
#S....^...#.*.E#:只有第 4 个字母变了。第 13 列的硬币还在。
今天的主题 3 分钟
- 生命值和游戏结束
- 每通过一层时的庆祝时刻
- 一个标题画面,还有一个让游戏停下和开始的
playing开关 - 发布游戏:操作说明、备注与致谢、冷测试
学习目标
上完这节课,你能够:
- 给游戏加上生命值、游戏结束和胜利。
- 让游戏暂停一会儿,但不停止整个作品。
- 把游戏准备好,让从没看过你代码的人也能玩。
学一学:一座能爬到顶的城堡 28 分钟
1 · 生命值
到现在为止,尖刺只会把骑士送回去。现在它要扣一条命。一个 lose a life :: custom 自制积木,代替 check tiles :: custom 里的 go to。
define lose a life
change [lives v] by (-1)
start sound [Oops v]
if <(lives) = (0)> then
set [playing v] to (0)
say [Game over! Click the green flag to try again.] for (3) seconds
stop [all v]
else
go to x: (start x) y: (start y)
set [y velocity v] to (0)
end
怎么用: 点绿旗开始。别的都不用按:骑士会走进尖刺三次。
2 · 通关时刻
上节课,你一碰到出口,下一层马上就出现了。玩家需要一点时间,感受一下“我做到了”。
- 让游戏停下:把
playing设成 0。 - 播放一个声音,说出通过了第几层,说两秒。
- 然后是下一层;如果是最后一层,就是胜利。
define floor cleared
set [playing v] to (0)
start sound [Tada v]
say (join [Floor ] (join (level number) [ cleared!])) for (2) seconds
change [level number v] by (1)
if <(level number) > (length of [levels v])> then
say (join [You reached the top with ] (join (coins) [ coins!]))
else
start level :: custom
set [playing v] to (1)
end
next level :: custom。怎么用: 点绿旗开始。别的都不用按:骑士会自己爬完第 1 层。
3 · 标题画面和开关
playing 是游戏的开关。只有它是 1 的时候,forever 循环才会让骑士移动。标题画面先把它设成 0,等玩家按 space 键,再把它设成 1。
when flag clicked
set rotation style [left-right v]
set size to (18) %
point in direction (90)
set [playing v] to (0)
set [lives v] to (3)
set [coins v] to (0)
set [level number v] to (1)
start level :: custom
say [CASTLE CLIMB. Arrows walk, space jumps. Press the space bar to start.]
wait until <key (space v) pressed?>
say []
set [playing v] to (1)
forever
if <(playing) = (1)> then
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
end
怎么用: 点绿旗,然后在舞台上点一下,按 space 键开始。按住 ← 或 → 让骑士走,按 space 键跳。
4 · 发布游戏
别人不用你在旁边也能玩,你的作品才算是一个游戏。分享之前,检查四件事。
| 检查 | 是什么意思 |
|---|---|
| 操作说明 | 标题画面和 Instructions(操作说明)框里都要写:方向键走路,space 键跳,走到出口。 |
| 备注与致谢 | 写出你借用了什么:Knight 角色和声音来自 Scratch 素材库;写上图块是谁画的。 |
| 没有死路 | 每一层都能通过。每扇门前面都有一把钥匙。 |
| 冷测试 | 保存,去做别的事十分钟,然后把所有脚本都收起来,再玩一遍。 |
基础任务:完成的城堡攀登 25 分钟
接着做你的钥匙关。今天它要变成三层楼,每层什么都有。
levels 1 #################..............##..............##..............##..............##..............##..............##..............##.........*....##.........#....##S.*..^...#.*.E################# 2 #################............####............DE##...........#####........K.....##.......###....##...C*.........##...###........##..............##.##...........##S##...^^^^^^^.################# 3 #################E.............######..........##......**......##.....####..*..##..........###.##.......C......##......###.....##..............##...##.........##S..##^^^^^^^^.#################
第 1 步:楼层
- 把
levels里的存档码换成这三个,或者换成你自己的三个。 - 做两个适用于所有角色的变量:
lives和playing。 - 给骑士加上 Oops 和 Tada 这两个声音。
第 2 步:生命值和通关时刻
- 做
lose a life :: custom和floor cleared :: custom,两个都不要勾选。 - 在
check tiles :: custom里,尖刺现在调用lose a life :: custom,出口调用floor cleared :: custom。
第 3 步:标题和开关
- 按“学一学”第 3 部分,做出绿旗脚本。
- 把整座城堡玩一遍。故意输一次。
define check tiles
tile at (x position) ((y position) - (6)) :: custom
if <<(tile) = [^]> or <(y position) < (-170)>> then
lose a life :: custom
end
tile at (x position) (y position) :: custom
pick up :: custom
if <(tile) = [E]> then
floor cleared :: custom
end
怎么用: 点绿旗,然后在舞台上点一下,按 space 键开始。按住 ← 或 → 让骑士走,按 space 键跳。
游戏结束以后,下一局开始时一条命都没有。
绿旗脚本里少了 set [lives v] to (3),或者它放在了 forever 后面。
说 “Floor 1 cleared!” 的时候,骑士还在走。
移动的积木没有放在 <(playing) = (1)> 里面,或者 floor cleared :: custom 没有先把 playing 设成 0。
游戏卡在标题画面。
按 space 键之前,先在舞台上点一下:舞台被点过以后,按键才会传给它。
爱心
用爱心来显示生命值,不用数字。
- 左上角有三颗爱心。
- 少一条命,就少一颗爱心。
怎么用: 点绿旗开始。别的都不用按:骑士会走进尖刺三次。
Teacher reveal
when flag clicked
hide
set [slot v] to (1)
repeat (3)
create clone of [myself v]
change [slot v] by (1)
end
when I start as a clone
go to x: ((-238) + ((24) * (slot))) y: (165)
forever
if <(lives) < (slot)> then
hide
else
show
end
end
A Heart sprite at size 16, with slot for this sprite only. Stamping hearts does not work here: the next erase all would wipe them with the map.
得分
给胜利一个可以去打破的数字。
- 每个硬币值 10 分。每条剩下的命值 50 分。
- 胜利画面显示总分,还有它是怎么算出来的。
怎么用: 点绿旗开始。别的都不用按:骑士会跑完三个短短的楼层,然后赢。
Teacher reveal
set [score v] to (((coins) * (10)) + ((lives) * (50)))
say (join [You win! Score: ] (score))
In place of the win line in floor cleared :: custom. The demo prints the whole sum with three more joins.
最好成绩
你爬完的城堡,值得再爬一次,而且要更快。
- 在标题画面按 space 键时,开始计时。
- 胜利画面显示你用的时间(取整秒)。
- 它还显示最好成绩,这个成绩会从这一次保留到下一次。
怎么用: 点绿旗开始。别的都不用按:骑士会跑完三个短短的楼层,然后显示用的时间。
Teacher reveal
set [time v] to (round ((timer) - (climb start)))
if <<(best) = (0)> or <(time) < (best)>> then
set [best v] to (time)
end
say (join (join [Time: ] (time)) (join [ s. Best: ] (join (best) [ s.])))
set [climb start v] to (timer) goes after the wait until <key (space v) pressed?>. best must not be reset by the green flag — that is the whole point. A best of 0 means “no climb yet”.
会读网格的守卫
加几个在楼层里巡逻的守卫。它们用的地图计算和骑士一样。
- 守卫沿着地板或者架子走。
- 它碰到墙会掉头,走到悬崖边也会掉头,永远不会掉下去。
- 碰到守卫就扣一条命。
怎么用: 点绿旗开始。别的都不用按:两个守卫在巡逻,骑士跳下去碰上其中一个。
Teacher reveal
define patrol
change x by ((2) * (dir))
tile at ((x position) + ((10) * (dir))) (y position) :: custom
set [ahead v] to (tile)
tile at ((x position) + ((10) * (dir))) ((y position) - (20)) :: custom
if <<(ahead) = [#]> or <not <(tile) = [#]>>> then
set [dir v] to ((0) - (dir))
point in direction ((90) * (dir))
end
dir is 1 or −1, for this sprite only, so a clone patrols on its own. The knight checks <touching [Guard v]?> in its loop, then waits half a second, so one touch is one life.
总结 5 分钟
- 生命值让犯错要付出代价;游戏结束会在停下之前把话说清楚。
playing开关能让游戏暂停,而作品还在继续运行。- 通关时刻、标题画面和胜利,让它感觉像一个真正的游戏。
- 发布了,就是别人只看屏幕也能玩。
- 生命值
- 游戏结束之前,玩家可以犯几次错。
- 游戏状态
- 游戏现在在做什么:标题画面、正在玩、暂停,还是结束了。
- 标题画面
- 第一个画面:游戏的名字和怎么玩。
- 致谢
- 写出你的作品借用了哪些人和哪些东西。
交作业
分享你的城堡攀登:这个单元完成了。下节课你会到达城堡的顶端:一条龙在那里等着你,它分阶段战斗,你可以学着看懂它的每个阶段。