复习:寻宝游戏完成了 5 分钟
(distance to [mouse-pointer v])是一个数字。数字小,就是离得近。- 只有两边都是真(true)的时候,
<<> and <>>才是真。 - 只要有一边是真,
<<> or <>>就是真。 <not <>>会把真变成假,把假变成真。
快问快答
这个什么时候是真?
<<(distance to [mouse-pointer v]) < (50)> and <mouse down?>>
看答案
鼠标指针离得近,而且鼠标按键按下的时候。离得近但没有按,是假。在很远的地方按,也是假。
今天的主题 3 分钟
- 一个由你来开的机器人:先转弯,再往前走
- 一个传感器:
<touching color [#23262D]?> - 传感器就是一个每一帧都问一次的问题,放在一直重复的循环(forever)里
- 地板上的垫子:绿色让你变快,红色让你回到起点
学习目标
上完这节课,你能够:
- 用“碰到颜色”找出角色下面是什么。
- 用吸管工具选出一个准确的颜色。
- 让地板改变机器人做的事情。
学一学:会说话的地板 28 分钟
1 · 认识机器人和赛道
我们从上面往下看这个机器人。它的车头朝右。
- 地板是浅色的。
- 一条粗粗的黑线从起点通到终点。
- 绿色的垫子在线上。红色的洞在转角旁边。
- 最后那个黑白格子的方块就是终点。
真正的机器人也会像这样沿着线走。仓库和工厂里的机器人整天都在这样做。
2 · 开车
你在赛车游戏里做过这个。机器人先转弯,再朝车头指的方向走。
when flag clicked
go to x: (-170) y: (-125)
point in direction (90)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (3) steps
end
end
3 · 传感器就是一个问题
传感器会告诉机器人外面世界的一些事情。我们的第一个传感器是一块Sensing(侦测)积木:
<touching color [#23262D]?>
只问一次没什么用。机器人一直在动,所以我们要一直问。
- 从 Control(控制)里拿一个
if / else积木,放进 forever 循环里。 - 把
<touching color [#23262D]?>放进它的六边形格子里。 - 在上半部分放
say [On the line!](在线上!)。 - 在下半部分放
say [Off the line](出线了)。 - 开车试试。你一离开线,对话框就马上变了。
when flag clicked
go to x: (-170) y: (-125)
point in direction (90)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (3) steps
end
if <touching color [#23262D]?> then
say [On the line!]
else
say [Off the line]
end
end
怎么用: 点绿旗,再点一下舞台。按住 ↑ 让机器人往前开,按 ← 或 → 让它转弯。
4 · 会下命令的垫子
现在地板会下命令了。我们给同一个机器人加第二个脚本。它再问两个问题。
when flag clicked
go to x: (-170) y: (-125)
point in direction (90)
set [speed v] to (3)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (speed) steps
end
end
when flag clicked
forever
if <touching color [#22C55E]?> then
set [speed v] to (6)
end
if <touching color [#EF4444]?> then
go to x: (-170) y: (-125)
point in direction (90)
set [speed v] to (3)
end
end
两个脚本同时运行。一个负责开车。一个负责看地板。谁也不用等谁。
怎么用: 点绿旗,再点一下舞台。按住 ↑ 让机器人往前开,按 ← 或 → 让它转弯。
5 · 准确的颜色
“碰到颜色”很挑剔。浅绿色不是绿色。所以我们从来不输入颜色,而是把颜色选出来。
- 点积木里面的颜色块。
- 点颜色菜单最下面的吸管工具。
- 点舞台上的垫子。颜色块就会变成那个准确的颜色。
基础任务:画地板,做垫子 24 分钟
第 1 步:地板
- 点舞台,再点 Backdrops(背景)标签。
- 把背景涂成浅色。
- 选 Line(线条)工具。把轮廓调得很粗,大约 14。
- 从左下角到右上角画一条黑线,中间拐几个弯。
- 在线的尽头画一个黑白格子的方块。
第 2 步:垫子
- 用 Rectangle(矩形)工具,在线上画两个绿色的方块。
- 在线的旁边画两个红色的方块,放在两个弯的内侧。
第 3 步:机器人
- 画一个新角色:一个圆角长方形,加两个轮子。
- 画的时候让它的车头朝右。
- 把它画得小一点,能放在线上。
第 4 步:开起来
搭好开车的脚本。把机器人移到你的线的起点。三个按键都试一试。
第 5 步:垫子的脚本
- 新建一个变量,叫
speed,选“适用于所有角色”。 - 把 move 积木改成
move (speed) steps。 - 搭好垫子的脚本。每个颜色都用吸管工具来选。
- 玩一玩。绿色应该让你变快。红色应该把你送回家。
when flag clicked
forever
if <touching color [#22C55E]?> then
set [speed v] to (6)
end
if <touching color [#EF4444]?> then
go to x: (-170) y: (-125)
point in direction (90)
set [speed v] to (3)
end
end
机器人碰到绿色也不会变快。
积木里的绿色不是地板上的绿色。用吸管工具在垫子的正中间重新选一次。
机器人每次都马上回到起点。
机器人一开始就碰到了红色,或者你的起点在红色方块里面。把起点移开,或者把红色方块移开。
就算碰到绿色,speed 也一直是 3。
找找看 forever 循环里是不是还有一个 set [speed v] to (3)。它每一帧都把速度改回去。
终点
让黑白格子的方块结束游戏。
- 机器人到达终点时,它会说 “Finished!”(完成了!)。
- 之后,方向键就没有用了。
- 按绿旗开始新的一局。
怎么用: 点绿旗,再点一下舞台。按住 ↑ 让机器人往前开,按 ← 或 → 让它转弯。
Teacher reveal
if <touching color [#FFFFFF]?> then
say [Finished!]
stop [other scripts in sprite v]
stop [this script v]
end
Inside the pads script’s forever loop. The finish is white and grey squares, and the question only needs the white. stop [all v] works too, but it clears the bubble, which is why the demo stops the driving script instead.
数一数出线次数
好司机会一直待在线上。数一数你离开线多少次。
- 一个叫
slips的变量,从 0 开始。 - 每离开线一次,正好加 1。
- 就算在线外待很久,也只算出线一次。
怎么用: 点绿旗,再点一下舞台。按住 ↑ 让机器人往前开,按 ← 或 → 让它转弯。
Teacher reveal — two waits, not one if
when flag clicked
set [slips v] to (0)
forever
wait until <not <touching color [#23262D]?>>
change [slips v] by (1)
wait until <touching color [#23262D]?>
end
Most pupils first write if <not <touching color [#23262D]?>> then inside a forever. It counts thirty slips a second. The second wait until <> is the fix: the robot must get back on the line before the next slip can count.
自动驾驶,只用一个传感器
按 space 键,机器人就自己开。
- 它碰到线的时候,会自己往前走。
- 它身上没有任何部分碰到线时,就马上停下。
- 然后它说 “Where did the line go?”(线去哪儿了?)。
怎么用: 点绿旗,再点一下舞台,然后按 space 键。机器人会自己开。你还是可以用 ↑、← 和 → 来开它。
Teacher reveal — and the question to ask
when [space v] key pressed
repeat until <not <touching color [#23262D]?>>
move (2) steps
end
say [Where did the line go?] for (2) seconds
Then ask: why did it not turn? Because “am I touching the line?” says nothing about which side the line went. To steer, a robot needs to know left from right. That is next lesson: two sensors.
会消失的加速
现在,碰到绿色后你会一直快到游戏结束。让加速只维持 1 秒。
- 绿色垫子把速度设成 6。
- 大约 1 秒后,速度自己变回 3。
- 再碰到绿色,就重新开始算 1 秒。
怎么用: 点绿旗,再点一下舞台。按住 ↑ 让机器人开过绿色垫子,按 ← 或 → 让它转弯。
Teacher reveal
when flag clicked
set [boost v] to (0)
forever
if <touching color [#22C55E]?> then
set [boost v] to (30)
end
if <(boost) > (0)> then
set [speed v] to (6)
change [boost v] by (-1)
else
set [speed v] to (3)
end
end
This replaces the green half of the pads script. The driving script is unchanged — it still reads (speed), and does not care who sets it.
总结 5 分钟
<touching color [#23262D]?>问的是:我身上有哪一部分在这个颜色上吗?- 放在 forever 循环里的传感器每一帧都问一次,大约每秒 30 次。
- 两个脚本可以同时运行:一个开车,一个看地板。
- 颜色一定要用吸管工具来选。垫子只涂一种平平的颜色。
- 一个传感器只知道在线上还是不在线上。它分不清左和右。
- 传感器
- 告诉机器人周围世界情况的东西。
- 帧
- forever 循环走一遍。每秒大约 30 帧。
- 吸管工具
- 从舞台上复制一个准确颜色的工具。
- 垫子
- 地板上的一块颜色,它会给机器人下命令。
交作业
保存你的赛道和机器人。下节课,机器人会有两个传感器小点,它会学会自己转弯。