复习:做完的射击游戏 5 分钟
前面四节课,我们做了一个射击游戏。
- 每个僵尸都是一个克隆体,有自己的
(hp)。 - 一波比一波难,每打倒一个都能得到金币。
- 商店卖东西之前会先检查:
<not <(coins) < (cost)>>。
快问快答
你有 100 金币。升级要 100 金币。
<(coins) < (cost)> 是 true(真)还是 false(假)?
看答案
False(假)。100 不小于 100。所以 <not <>> 把它变成 true,你就能买。
今天学的就是这样的问题。
今天的主题 3 分钟
- 跟着鼠标走的角色
(distance to [Treasure v]):一样东西离你有多远- 用
<() < ()>和<() > ()>比较两个数字 if / then和if / then / else<mouse down?>:用鼠标键来挖
学习目标
上完这节课,你能够:
- 量出一个角色离另一个角色有多远。
- 比较两个数字,问一个回答“是”或“不是”的问题。
- 用一串 if / else,在三个答案里选一个。
学一学:热、暖还是冷 28 分钟
先在纸上想好游戏
- 一个宝箱藏在沙子里一个随机的地方。
- 你用鼠标移动一个金属探测器。
- 探测器会说 Hot!、Warm 或 Cold。
- 它说 Hot! 的时候,你按下鼠标键来挖。
你需要两个角色:一个 Detector 和一个 Treasure。
1 · 跟着鼠标走的探测器
你在第 1 级学过 go to [mouse-pointer v]。把它放在 forever 里面,角色就会粘在鼠标上。
- 打开 Motion(运动)。拖出
go to [random position v]。 - 点那个小箭头。选
mouse-pointer。 - 把它放进
forever里面,放在when flag clicked下面。 - 点绿旗。移动鼠标。探测器就跟着走。
Sensing(侦测)里还有两块鼠标积木。(mouse x) 是鼠标在左边还是右边多远。(mouse y) 是在上面还是下面多远。
go to [mouse-pointer v]
go to x: (mouse x) y: (mouse y)
长的写法看起来多费事。在挑战 2 里,你会知道它为什么有用。
2 · 有多远?distance to
(distance to [Treasure v]) 是一块圆形的 Sensing 积木。它里面是一个数字:Treasure 离这里有多少步。
- 打开 Sensing。找到
(distance to [mouse-pointer v])。 - 点它的箭头。选
Treasure。 - 点一下这块积木。一个小气泡会显示这个数字。
- 把 Treasure 拖到别的地方。再点一下这块积木。数字变了。
when flag clicked
forever
go to [mouse-pointer v]
say (round (distance to [Treasure v]))
end
(round ()) 会把它变成整数。怎么用: 点绿旗,然后在沙漠里移动鼠标。探测器跟着鼠标,并说出宝箱有多远。不用再按别的。
3 · 回答“是”或“不是”的问题
尖头的积木是一个问题。它的答案只会是 true 或 false。你已经见过一些了,比如 <mouse down?>。
Operators(运算)里有三块积木,用来比较两个数字:
<(distance to [Treasure v]) < (60)>
<(distance to [Treasure v]) > (150)>
第三块是 <() = ()>,意思是“完全一样”。下节课你会用到它。
| 距离 | <(distance to [Treasure v]) < (60)> | <(distance to [Treasure v]) > (150)> |
|---|---|---|
| 40 | true | false |
| 100 | false | false |
| 300 | false | true |
4 · if / then,或者 if / then / else
尖头的问题正好放进 if 积木里尖头的空格。
if / then在答案是“是”的时候做一件事。答案是“不是”的时候,它什么都不做。if / then / else总是在两件事里做一件。“是”就运行上面的部分,“不是”就运行下面的部分。
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
else
say [Cold]
end
end
怎么用: 点绿旗,然后把鼠标移向宝箱,再移开。看探测器说的词怎么变。不用再按别的。
5 · 三个答案:一串 if / else
两个词不够。我们想在中间加一个 Warm。在第一个 if / else 的 else 里面,再放一个 if / else。
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
怎么用: 点绿旗,然后把鼠标慢慢移向宝箱。探测器先说 Cold,然后 Warm,然后 Hot!。不用再按别的。
6 · 埋起来,再挖出来
现在宝箱要藏起来了。点绿旗时,它做三件事:
hide:谁都看不见它。switch costume to [chest v]:关上盖子,准备好这一局。go to x: (pick random (-180) to (200)) y: (pick random (-150) to (-40)):沙子上一个随机的地方。
挖就是 <mouse down?>。按住鼠标键的时候,它是 true。我们只在 Hot! 的时候挖,所以这个检查要放在 Hot! 的部分里面。
when flag clicked
hide
switch costume to [chest v]
go to x: (pick random (-180) to (200)) y: (pick random (-150) to (-40))
when I receive [found v]
switch costume to [open v]
show
start sound [Coin v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
... :: grey
end
if / then:不挖,就什么都不做。基础任务:做出寻宝第一版 24 分钟
新建一个作品。一步一步地做,每做完一步就玩一玩。
怎么用: 点绿旗,然后在沙子上移动鼠标,来回扫探测器。它说 Hot! 时,按下鼠标键来挖。再点一次绿旗,宝箱就会藏到新的地方。
第 1 步:沙漠
- 点舞台,再点背景按钮。
- 从背景库里选
Desert。
第 2 步:两个角色
- 画一个叫
Detector的角色:一个灰色的圆,加一根棍子当把手。或者选Magic Wand。 - 画一个叫
Treasure的角色,有两个造型:chest(关着)和open(打开)。一个带盖子的盒子就够了。 - 给 Treasure 加上
Coin声音。
第 3 步:跟着鼠标
- 在 Detector 上:
when flag clicked,然后forever。 - 在它里面:
go to [mouse-pointer v]。 - 玩一玩。探测器粘在鼠标上了。
第 4 步:三个词
- 在 go to 下面加一个
if / else。在它的空格里放<(distance to [Treasure v]) < (60)>。 - 上面的部分:
say [Hot!]。 - 下面的部分:第二个
if / else,问<(distance to [Treasure v]) < (150)>。 - 它上面的部分说
Warm。它下面的部分说Cold。 - 让宝箱先显示出来,玩一玩。三个词都检查一下。
第 5 步:埋宝箱
- 在 Treasure 上拼好绿旗脚本:藏起来、换造型、随机地点。
- 玩一玩。宝箱不见了。三个词还是有用。
- 多按几次绿旗。Hot! 的地方每次都会换。
第 6 步:挖
- 在 Hot! 的部分里加一个
if / then。在它的空格里放<mouse down?>。 - 在它里面:
broadcast [found v]。新建一个叫found的消息。 - 在 Treasure 上:
when I receive [found v],换成打开的造型,显示,start sound [Coin v]。 - 在 Detector 上:
when I receive [found v],然后stop [other scripts in sprite v]和say [You found it!]。
完整的 Detector
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
when I receive [found v]
stop [other scripts in sprite v]
say [You found it!]
它总是说 Warm,就算在宝箱正上方也一样。
你的第一个问题是 <(distance to [Treasure v]) < (150)>。把两个数字换过来:60 必须在前面。
数字一直不变。
下拉菜单里还是 mouse-pointer。探测器离鼠标永远是 0 步!把它改成 Treasure。
找到宝箱后,探测器还一直跟着鼠标。
少了 stop [other scripts in sprite v]。这块积木会结束 forever 循环。
游戏一开始,宝箱就已经是打开的。
在它的绿旗脚本里加上 switch costume to [chest v]。
Boiling!(超烫!)
加第四个词,用在你几乎就在宝箱正上方的时候。
- 小于 30 步时,探测器说 Boiling!。
- Hot!、Warm 和 Cold 还是有用。
- 它说 Boiling! 时,你也能挖。
Teacher reveal — the four-word chain
if <(distance to [Treasure v]) < (30)> then
say [Boiling!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
The trap is the dig. Most pupils add Boiling! and forget to copy the <mouse down?> check into it. Then digging on Boiling! does nothing. Let them find it by playing.
留在沙地上
金属探测器不会飞。让它留在地面上。
- 鼠标在沙子上时,探测器跟着它。
- 鼠标移到天上时,探测器停在原地。
- 沙地大约在 y = -30 的地方结束。
怎么用: 点绿旗,然后在沙子上移动鼠标,再移到天上。探测器停在沙地的边上。它说 Hot! 时,按下鼠标键来挖。
Teacher reveal — the long way pays off
when flag clicked
forever
if <(mouse y) < (-30)> then
go to x: (mouse x) y: (mouse y)
end
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
go to [mouse-pointer v] would work here too. The long version is worth showing, because it leads to a better one: keep following left and right with set x to (mouse x), and only stop the up and down. Ask who can build that.
Nothing here!(这里没有!)
现在只有在 Hot! 的时候才能挖。让玩家在哪里都能挖。
- 在 Hot! 的时候挖,宝箱会像以前一样打开。
- 在其他地方挖,会说 Nothing here! 一秒钟。
- 它说这句话的时候,探测器不动。挖是要花时间的!
怎么用: 点绿旗,然后在沙子上移动鼠标。在哪里按下鼠标键都能挖。离得远时它说 Nothing here!;够近时宝箱就会打开。
Teacher reveal — the dig gets its own if / else
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
if <mouse down?> then
if <(distance to [Treasure v]) < (60)> then
broadcast [found v]
else
say [Nothing here!] for (1) seconds
end
end
end
The dig moves out of the chain and sits below it. Now it is an if / then / else, because a dig always gets an answer. This is the clearest example of the two shapes side by side — worth pointing at.
The say [Nothing here!] for (1) seconds pauses the loop. So the detector freezes for that second. It feels right, so we keep it.
会变大的探测器
用词很好,用图更快。让探测器显示它有多近。
- 离宝箱很远时,探测器很小。
- 离得越近,它变得越大。
- 就在宝箱正上方时,它最大。
怎么用: 点绿旗,然后在沙子上移动鼠标。你靠近时探测器会变大。它说 Hot! 时,按下鼠标键来挖。
Teacher reveal — size from distance
when flag clicked
forever
go to [mouse-pointer v]
set size to ((120) - ((distance to [Treasure v]) / (5))) %
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
At 0 steps the size is 120%. At 400 steps it is 40%. Dividing by 5 keeps it from going below zero. Pupils who try ((200) - (distance to [Treasure v])) find the detector vanish when it is far away — a good bug to explain.
总结 5 分钟
- 把
go to [mouse-pointer v]放在 forever 里,角色就会跟着鼠标走。(mouse x)和(mouse y)给出鼠标的两个数字。 (distance to [Treasure v])是一个角色离这里有多少步。就算那个角色藏起来了,它也能用。<() < ()>和<() > ()>比较两个数字。答案是 true 或 false。if / then可能什么都不做。if / then / else总是在两件事里做一件。- 在 else 里面再放一个 if / else,就成了一串。先问最小的问题。
- 距离
- 一个角色离另一个角色有多少步。
- 布尔值(Boolean)
- 一块尖头的积木,它的答案只会是 true 或 false。
- 比较
- 问一个数字是小于、大于,还是等于另一个数字。
- 一串 if / else
- 在 else 里面放一个 if / else,这样就有两个以上的答案。
交作业
保存你的寻宝游戏。下节课你只能挖五次,而且你会输。