复习:跳跃 5 分钟
- 跳跃就是
set [y velocity v] to (12),它会升高 66。 on ground每一轮都回到 0。只有真正落地才会把它设成 1。- 助跑跳在空中待 24 轮。所以它的距离是速度 × 24。
快问快答
上一课,掉在平台上方的星星落在了平台上。没有人为平台写过一行代码。为什么?
看答案
因为它是绿色的。land 从来不问是哪一块绿色。地面和平台是同一种颜色,所以对这块积木来说,它们是同一样东西。平台只需要这些。接着,今天有三个意外。
今天的主题 3 分钟
- 平台只是更多的地面颜色
- 碰撞区:负责去碰的那个造型
- 一个
walkMy Block,用上奶酪迷宫的走、问、退回 - 撞头,还有玩家能通关的关卡
学习目标
上完这节课,你能够:
- 做一个 Pico 能爬上去的台阶关卡。
- 解释为什么去碰的形状,不一定是你看到的形状。
- 不让 Pico 走进台阶的侧面,也不让它从台阶底下往上穿过去。
学一学:台阶 28 分钟
1 · 平台不需要新的代码
用地面的颜色画四个台阶,每个比上一个高 50。然后运行上一课的 Pico,什么都不要改。一次跳跃升高 66,所以每个台阶都够得着。每次落地都直接就能用,因为每个台阶都是绿色的。
怎么用: 点绿旗。看 Pico 自己一跳一跳地爬上四个台阶,到达星星。
2 · 去碰的不是你看到的
现在让 Pico 慢慢走出一个台阶的边,然后停下。Pico 是一个大大的圆头,下面两条小腿。腿走出去了,可头还在台阶上面。Pico 往下掉时,它的下巴卡住了台阶的角。它就挂在那里,低了 20 像素。
每个平台游戏都用碰撞区(hitbox)来修这个问题。它是一个简单的方框造型,和 Pico 一样高,比它的头窄一点。land 先换成它,最后再换回 Pico。这块积木是不刷新屏幕运行的,所以方框从来不会被画出来,舞台上只会显示 Pico。
define land
switch costume to (hitbox v)
set [on ground v] to (0)
if <touching color [#3FA34D]?> then
repeat until <not <touching color [#3FA34D]?>>
change y by (1)
end
set [y velocity v] to (0)
set [on ground v] to (1)
end
switch costume to (pico v)
land。怎么用: 点绿旗。Pico 两次走到第 1 个台阶的边上。看它每次停在哪里。
3 · 墙,还有自动扶梯
让 Pico 直直地走进第 1 个台阶的侧面。没有东西阻止它走进绿色里。然后 land 看到绿色,就把 Pico 往上推,直到离开绿色。那就是台阶的顶上。Pico 被抬高了 50 像素,就像坐自动扶梯一样。
怎么用: 点绿旗。Pico 只往右走,从来不跳。
修法是奶酪迷宫的走、问、退回,只不过是横着的。走路有了自己的 My Block,它也用碰撞区:
define walk (speed)
switch costume to (hitbox v)
change x by (speed)
if <touching color [#3FA34D]?> then
change x by ((0) - (speed))
end
switch costume to (pico v)
walk (4) :: custom 和 walk (-4) :: custom。怎么用: 点绿旗。Pico 走进第 1 个台阶,被挡住,然后跳了上去。
基础任务:爬台阶 24 分钟
从你的草地作品开始。把草地变成一个关卡。
第 1 步:画关卡
- 保留地面,但在中间留一个缺口。
- 把缺口涂成蓝色。下一课它会变成水。
- 用地面的绿色画四个台阶。每个都比前一个高大约 50。让它们靠得近一点。
- 画一个短短的方块,低到可以从下面走过去,但又高到跳不上去。
第 2 步:碰撞区
- 在 Pico 的 Costumes(造型)标签里,复制造型,把复制品叫作
hitbox。 - 画一个填满的长方形,从头顶到脚。删掉其他部分。
- 在
land里,最上面加switch costume to (hitbox v),最下面加switch costume to (pico v)。
第 3 步:walk 积木
- 做一块叫
walk的积木,带一个叫speed的数字输入。勾上 Run without screen refresh(不刷新屏幕运行)。 - 照着“学一学”第 3 部分把它搭出来。
- 在循环里,把每个
change x by (4)换成walk (4) :: custom。把往左的那个换成walk (-4) :: custom。
第 4 步:回来的路,还有一个目标
- 如果 Pico 掉进缺口,就把它送回起点:
if <(y position) < (-150)> then。 - 在最高的台阶上放一颗 Star(星星)。碰到它,Pico 就会变开心,还会说出来。
when flag clicked
set rotation style [left-right v]
go to x: (-215) y: (-104)
set [y velocity v] to (0)
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
change [y velocity v] by (-1)
change y by (y velocity)
land :: custom
if <<key (space v) pressed?> and <(on ground) = (1)>> then
set [y velocity v] to (12)
end
if <(y position) < (-150)> then
go to x: (-215) y: (-104)
set [y velocity v] to (0)
end
if <touching (Star v)?> then
switch costume to (pico happy v)
say [I made it!]
end
end
walk 和用方框的 land。怎么用: 点绿旗。用方向键走路,用 space 键跳。爬上四个台阶,到达星星。试着走进台阶的侧面。
Pico 一闪一闪地变成灰色方框。
walk 或 land 的最后少了 switch costume to (pico)。或者其中一块做的时候没有勾上 Run without screen refresh(不刷新屏幕运行)。
Pico 完全走不了。
碰撞区比 Pico 的脚还低。所以它已经碰到地板了,每一步都被退回。把它的底边抬高一点。
Pico 不用跳,还是会被抬到台阶上。
方向键还在用 change x by (4)。它们必须调用 walk。不然侧面检查从来不会运行。
我的某个台阶够不着。
它比下面那个高了超过 66。或者它比助跑跳能跳的距离还远。这就是挑战 3。
在低方块下面跳,Pico 跑到了它上面。
对。这就是挑战 1。
撞头
让 Pico 站在低方块下面,直直往上跳。然后改一改 land:往上走时碰到绿色,必须算撞头,而不是落地。
- 往上撞进绿色:把 Pico 往下推,直到离开。
- 往下掉进绿色:像以前一样往上推。只有这种才算 on ground。
- 不管哪种,速度都变成 0。
怎么用: 点绿旗。Pico 在方块下面跳两次。第一次没有天花板检查,第二次有。
Teacher reveal — which way to push
if <touching color [#3FA34D]?> then
if <(y velocity) > (0)> then
repeat until <not <touching color [#3FA34D]?>>
change y by (-1)
end
else
repeat until <not <touching color [#3FA34D]?>>
change y by (1)
end
set [on ground v] to (1)
end
set [y velocity v] to (0)
end
The speed says which way Pico was going when it hit the green, so it says which way is out. Positive: it hit a ceiling, push down. Otherwise it hit a floor, push up. Only the floor sets on ground — otherwise a bump would let Pico jump again from the ceiling.
在半空中起跳
很多人第一次做 on ground 是这样的:落地时设成 1,只有 Pico 跳的时候才回到 0。做出这个版本。然后走出第 1 个台阶的侧边,在往下掉的时候按跳。
- 加一个
mid-air jumps计数。 - 先用“只在跳的时候”的版本运行一次,再用“每一轮”的版本运行一次。
- 用一句话解释它们的不同。
怎么用: 点绿旗。Pico 两次走出第 1 个台阶。每一次,它都在往下掉时按跳。
Teacher note
Walking off an edge is the one way to leave the ground without jumping, so it is the one case the “clear it when you jump” version never handles. This is the reason Lesson 3-30 insisted on clearing on ground at the top of land — a flag that describes the world has to be re-asked every tick, not changed only when you remember to.
够得着吗?
Pico 必须能到达关卡里的每一个台阶。有些东西它不应该够得着。用 Pico 真正的数字把两种都测一测。
- 从低方块旁边往上跳。它有 90 高。
- 从第 3 个台阶跳到第 4 个。它高 50。
- 用 66 这个数字,给你自己的关卡写一条规则。
怎么用: 点绿旗。Pico 先试 90 像素高的方块,再试 50 像素高的台阶。
Teacher note
Leaving a margin matters: a step exactly 66 up is reachable only from the one tick at the top of the jump, which feels impossible to a player. Around 50 with a jump of 66 gives a comfortable window. The same thinking applies to gaps and Lesson 3-30’s running-jump lengths.
升降台
加一个在水面上来回滑动的绿色平台。Pico 必须能坐着它一路过去,不会掉下来。
- 一个
Lift角色,涂成同一种绿色。它每一轮移动 2,来回走。 - 一个
lift speed变量。升降台会一直更新它。 - Pico 落在升降台上时,也按照升降台的速度移动。
怎么用: 点绿旗。Pico 坐两次升降台。第一次没有东西带着它走,第二次有。
Teacher note
The good moment is the first ride. Pupils expect Pico to move with the lift because that is what would happen in real life, and they have to notice that nothing in their code says so. A game world only does what a script makes it do.
总结 5 分钟
- 平台就是更多的地面颜色。落在上面不需要新的代码。
- 由一个碰撞区造型负责去碰。这样宽宽的头就不会卡在角上。
walk是 x 方向的走、问、退回。land是 y 方向的推出来。把它们分开。- 往上走时碰到绿色是撞头。往下推,不要把它算作地面。
- Pico 必须能到达每一个台阶。让每个台阶都比一次跳跃的高度低不少。
- 平台
- 角色能站上去、跳上去、也能撞到的东西。
- 碰撞区
- 负责去碰的那个看不见的形状。它比你看到的图片简单。
- 撞头
- 往上走时,从下面撞到平台。
- 够得着
- 够近、也够低,玩家跳一下就能到。
交作业
保存你的关卡。下一课,水会变得危险,还有一只螃蟹在地面上走来走去。还有一颗值得你通关去拿的星星。