复习:奶酪迷宫单元 5 分钟
- 墙是一种平平的颜色。角色会问自己是不是碰到了那种颜色。
- 走、问、退回。检查在走之后,绝不在走之前。
- 如果一步比墙还厚,角色就会一步跨过去。
快问快答
在迷宫里,老鼠走 4,就正好退回 4。Pico 掉到草地上的时候,它要往回走多远?
看答案
在它发生之前,谁也不知道。这要看 Pico 掉得有多快。所以 Pico 不是退回一个固定的数字,而是一个像素一个像素地往回退,直到离开草地。这就是今天的落地。
今天的主题 3 分钟
y velocity:存在变量里的速度。- 重力:每一轮都
change [y velocity v] by (-1)。 - 落地:从地面里推回来。
- 为什么落地要放在一个关掉屏幕刷新的 My Block 里。
学习目标
上完这节课,你能够:
- 让角色越掉越快,就像真的东西一样。
- 让它正好落在地面上。不陷进去,也不浮在上面。
- 解释为什么推出来必须在两帧之间做完。
学一学:往下掉 28 分钟
1 · 匀速下落看起来不对
大多数人做的第一个下落是这样的:每一轮往下移动 5,直到碰到草地。
repeat until <touching color [#3FA34D]?>
change y by (-5)
end
怎么用: 点绿旗,看 Pico 一路用同一个速度下来。
2 · 一个可以改变的速度
会掉下来的东西,开始时很慢,然后越来越快。要模仿这一点,速度就不能是打在积木里的一个数字。它必须是一个每一轮都会变的变量。我们叫它 y velocity(y 速度):Pico 现在往上移动有多快。
set [y velocity v] to (0)
repeat until <touching color [#3FA34D]?>
change [y velocity v] by (-1)
change y by (y velocity)
end
| 第几轮 | y velocity | Pico 移动 | 一共掉了 |
|---|---|---|---|
| 1 | −1 | 向下 1 | 1 |
| 2 | −2 | 向下 2 | 3 |
| 3 | −3 | 向下 3 | 6 |
| 10 | −10 | 向下 10 | 55 |
| 22 | −22 | 向下 22 | 253 |
怎么用: 点绿旗。看 y velocity 显示框往下数,Pico 越来越快。
3 · 落地:从草里退出来
仔细看那次下落的最后。最后一轮让 Pico 移动了 22 像素,可草地比这更近。所以 Pico 最后陷进草里 9 像素。修法很像迷宫里的退回,只改了一点:我们不知道要退多远,所以一次退一个像素,直到 Pico 离开草地。
define land
if <touching color [#3FA34D]?> then
repeat until <not <touching color [#3FA34D]?>>
change y by (1)
end
set [y velocity v] to (0)
end
land 的 My Block,做的时候勾上 Run without screen refresh(不刷新屏幕运行)。挑战 2 会告诉你为什么这个勾很重要。怎么用: 点绿旗。Pico 落地后,会在草里停一会儿,让你看清楚。然后它被推出来。
基础任务:草地上的 Pico 24 分钟
新建一个作品。你要画一片草地,让 Pico 掉进去。然后你让它走下一个平台,再看它掉下来。
第 1 步:画草地
- 画一个浅色的天空背景。
- 用矩形工具,在底部画一条横跨的地面。只用一种绿色。开填充,关轮廓线。
- 在右边高处,用同一种绿色画一个短短的、薄薄的平台。
第 2 步:Pico
- 添加
Pico角色,把大小设成 40。 - 加上
set rotation style [left-right v]。这样它转身时永远不会倒过来。 - 建一个叫
y velocity的变量。
第 3 步:land 积木
- 在 My Blocks 里,点 Make a Block(制作新的积木),叫它
land。 - 按 OK 之前,勾上 Run without screen refresh(不刷新屏幕运行)。
- 在 define 帽子积木下面,搭出“学一学”第 3 部分里的推出来。
第 4 步:循环
- 点绿旗时,到
x: 150, y: 150,就在平台上方。把y velocity设成 0。 - 在
forever里:先重力,然后change y by (y velocity),然后land :: custom。 - 试一试。Pico 应该掉到平台上,然后待在那里。
第 5 步:走路,还有掉下去
- 在循环里,加上右方向键和左方向键的检查。面朝正确的方向,然后
change x by (4)或者change x by (-4)。 - 让 Pico 走出平台的边。它掉向地面时应该越来越快。
- 加上
when [space v] key pressed→go to x: (x position) y: (150),再把y velocity设成 0。现在你可以再让它掉一次了。
when flag clicked
set rotation style [left-right v]
go to x: (150) y: (150)
set [y velocity v] to (0)
forever
change [y velocity v] by (-1)
change y by (y velocity)
land :: custom
if <key (right arrow v) pressed?> then
point in direction (90)
change x by (4)
end
if <key (left arrow v) pressed?> then
point in direction (-90)
change x by (-4)
end
end
when [space v] key pressed
go to x: (x position) y: (150)
set [y velocity v] to (0)
怎么用: 点绿旗。用左右方向键走路,走出平台,按 space 键让 Pico 再从天上掉下来。
Pico 陷进地里,然后慢慢爬出来。
没有勾上 Run without screen refresh(不刷新屏幕运行)。右键点 define 帽子积木,选 Edit(编辑),把它勾上。挑战 2 讲的就是这个。
Pico 直接穿过了地面。
touching color 里的颜色不是地面的颜色。点颜色框,用吸管在舞台上选地面。
Pico 一落地,就从舞台顶上飞出去了。
推出来写成了 change y by (-1)。或者少了 not,所以它在没有碰到的时候一直走。
Pico 每次都陷得更深一点。
land 里面从来没有把 y velocity 设回 0。所以每一轮都从它落地时的速度开始。
如果我正好停在平台边上,Pico 会用头挂在那里。
观察得好。Pico 的头比脚宽很多,而头还在平台上面。继续走,它就会掉下去。第 3-31 课会真正修好它。
三种重力
让三个 Pico 从同一个高度一起掉下来。一个在月球上,一个在地球上,一个在一颗巨大的行星上。
- 它们的重力是 0.5、1 和 2。
- 每一个都数一数自己落地前用了几轮。
- 运行之前,先猜这三个数字。
怎么用: 点绿旗。三个一起掉下来。看哪个先落地,再读显示框。
Teacher note
Nearly everyone guesses 44 and 11 — double and half of 22. The real answers are 31 and 16, because the distance fallen grows with the square of the time: 22 × 1.41 ≈ 31 and 22 ÷ 1.41 ≈ 16. Nobody needs the maths, but the gap between the guess and the result is the lesson: speed that builds up behaves differently from speed that is fixed.
慢慢爬出来
做两个 Pico。给它们同样的 land 积木。只给其中一个勾上 Run without screen refresh(不刷新屏幕运行)。
- 让两个从同一个高度掉下来。
- 每个都有第二个脚本。它会数 Pico 碰到草地的每一帧。
- 比一比两个计数。
怎么用: 点绿旗,仔细看右边那个 Pico 落地。
Teacher reveal — the counting script
when flag clicked
wait (1) seconds
forever
if <touching color [#3FA34D]?> then
change [frames in the grass v] by (1)
end
end
Both Picos climb out the same number of pixels. The difference is when the Stage is drawn. Without the tick, Scratch redraws after every pass of the repeat until, so each pixel of the climb is a frame you can see. With it, the whole climb happens between two frames and the watcher never catches Pico in the grass. This is the Firework Show tickbox from Lesson 3-15, doing a job that actually matters.
弹跳的球
把 Pico 换成一个球。它落地时,要弹起来,而不是停下。每次弹得都低一点。然后它停稳。
- 落地时把速度反过来,只留下十分之七。
- 一个弹跳次数的计数。
- 当弹跳会变得很小时,球就彻底停下。
怎么用: 点绿旗,用显示框数一数弹了几次。
Teacher reveal — the bounce block
define bounce
if <touching color [#3FA34D]?> then
repeat until <not <touching color [#3FA34D]?>>
change y by (1)
end
set [y velocity v] to (round ((y velocity) * (-0.7)))
change [bounces v] by (1)
if <(y velocity) < (3)> then
set [y velocity v] to (0)
set [resting v] to (1)
end
end
Two details that trip people up. The round is there because 0.7 of a whole number is rarely whole, and without it the speed never settles. The resting test is there because a bounce of 1 bounces for ever — the ball twitches on the grass until you stop the project.
星星雨
十颗星星从随机的高度、随机的地方掉下来。每一颗都像 Pico 那样往下掉,落在它下面任何绿色的东西上。
- 一个 Star 角色,十个克隆体。
- 每个克隆体都有自己的
y velocity。把它设成仅适用于当前角色(for this sprite only),就像戳泡泡里那样。 - 它们全都共用一个
land积木。 - 一个
landed计数,到 10 的时候 Pico 宣布出来。
怎么用: 点绿旗,看十颗星星掉下来。最后一颗落地时,Pico 会说话。
Teacher note
The stars that land on the ledge are the best moment in the lesson: nobody wrote anything about the ledge. It is green, so it is ground. Next lesson the same idea turns into platforms you can jump onto.
总结 5 分钟
- 看起来真实的下落,需要一个会变化的速度。所以速度存在一个变量里:
y velocity。 - 重力是
change [y velocity v] by (-1)。移动是change y by (y velocity)。 - 落地会走过头。所以一次推回一个像素,直到离开地面。
- 然后把
y velocity设成 0。不然下一轮又会一下子冲回去。 - 推出来要放在一个勾上 Run without screen refresh(不刷新屏幕运行) 的 My Block 里,这样它会在两帧之间做完。
- 速度(velocity)
- 东西移动有多快,往哪个方向。负的 y velocity 表示向下。
- 重力
- 一直往下拉的力,每一轮都让速度减少一点。
- 一轮(tick)
- forever 循环转一圈。也就是游戏的一帧。
- 推出来
- 角色陷进地面太深以后,把它移回来。
交作业
保存你的草地。下一课,Pico 会跳。原来跳跃只要一块积木。