复习:我们学到哪里了 5 分钟
- 不管碰到哪条蛇,
<<> or <>>都回答“是”。 <not <touching [Hole v]?>>让你不能在同一个地方挖两次。- 一个看守脚本负责结束游戏,不管是谁拿走了最后一次机会。
- 哔声之间等的时间是距离除以 200。
快问快答
你按下绿旗。下面哪些应该回到一开始的值?
digs lefttimebest time
看答案
digs left 和 time,但不是 best time。每局都重新开始的纪录就不算纪录了。今天的任务就是把它留住。
今天的主题 3 分钟
(timer)和reset timer:Scratch 自带的秒表- 一局里找三个宝箱
- 绿旗永远不会重设的最好成绩,用
<<> or <>>来检查 - 开始画面:一个标题、一个 START 按钮和一个广播
学习目标
上完这节课,你能够:
- 用计时器给游戏计时。
- 保存一个只有被打破时才会变的纪录。
- 用一个按钮来开始游戏,而不是用绿旗。
学一学:时钟和大门 28 分钟
1 · Scratch 有一个秒表
(timer) 是一块圆形的 Sensing(侦测)积木。它会自己数秒。reset timer 把它设回 0。
- 打开 Sensing。勾上
(timer)旁边的框。 - 看舞台。数字一直往上涨,还有很多位小数。
- 点
reset timer。它跳回 0。 - 取消勾选。我们改用一个整齐的
time变量来显示。
计时器有小数:12.4836。(round ()) 把它变成 12。
when flag clicked
reset timer
forever
set [time v] to (round (timer))
end
when flag clicked
forever
go to [mouse-pointer v]
if <<mouse down?> and <(distance to [Treasure v]) < (60)>> then
broadcast [found v]
end
end
when I receive [found v]
stop [other scripts in sprite v]
say (join [Found in ] (join (time) [ seconds!]))
怎么用: 点绿旗,然后把鼠标移到宝箱上,在它上面按下鼠标键。时间停下来,探测器会说你用了多久。
2 · 会留下来的最好成绩
新建一个叫 best time 的变量。只有一条规则:绿旗下面的任何积木都不能碰它。
它只在一个地方改变:一局游戏结束的时候。而且只在两种情况下改变:
- 还没有最好成绩(它还是 0),或者
- 这次的时间比最好成绩小。
两种情况,有一种就够了。这就是 or。
when I receive [found v]
stop [other scripts in sprite v]
if <<(best time) = (0)> or <(time) < (best time)>> then
set [best time v] to (time)
end
say (join [Best: ] (best time))
| best time | time | 新的最好成绩? |
|---|---|---|
| 0 | 30 | 是:第一局 |
| 30 | 22 | 是:更快 |
| 22 | 27 | 不是:更慢 |
怎么用: 点绿旗,然后在宝箱上按下鼠标键。再点一次绿旗,再玩一次。只有你打破最好成绩时,它才会变。
3 · 连找三个宝箱
找一个宝箱很快。找三个就是一场比赛。每找到一次,宝箱就给自己数一下,然后跳到新的地方。
打开的宝箱会留下来,当作奖杯。它是一个克隆体,做法和坑完全一样。它来自一个新角色 Open Chest,并移到 Detector 那里。
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]
change [treasures v] by (1)
start sound [Coin v]
create clone of [Open Chest v]
go to x: (pick random (-180) to (200)) y: (pick random (-150) to (-40))
when flag clicked
hide
when I start as a clone
go to [Detector v]
show
在 Detector 上,一个看守脚本数到三:
when flag clicked
forever
if <mouse down?> then
if <(distance to [Treasure v]) < (60)> then
broadcast [found v]
end
wait until <not <mouse down?>>
end
end
when flag clicked
set [treasures v] to (0)
wait until <(treasures) = (3)>
broadcast [win v]
when I receive [win v]
stop [other scripts in sprite v]
say [All three!]
怎么用: 点绿旗,然后在沙子上移动鼠标。它说 Hot! 时,按下鼠标键来挖。找到宝箱三次。
4 · 开始画面
真正的游戏有一扇大门。绿旗只负责布置场景:
- 标题和 START 按钮显示出来;
- 探测器藏起来。
点 START 会发出 broadcast [start v]。现在所有负责玩游戏的脚本,都改成等这个消息,而不是等绿旗。
when flag clicked
show
when this sprite clicked
broadcast [start v]
when I receive [start v]
hide
when flag clicked
hide
when I receive [start v]
show
forever
go to [mouse-pointer v]
end
怎么用: 点绿旗,然后点 START。标题和按钮不见了,探测器出现了。移动鼠标来扫探测器。
5 · 一个脚本管一整局
上节课,一个看守脚本等着挖的次数用完。现在一局有两种结束方式:挖的次数用完,或者找到三个宝箱。一个脚本同时看着这两件事,等的时候还负责走时钟。
when I receive [start v]
set [digs left v] to (10)
set [treasures v] to (0)
reset timer
repeat until <<(digs left) < (1)> or <(treasures) = (3)>>
set [time v] to (round (timer))
end
if <(treasures) = (3)> then
broadcast [win v]
else
broadcast [lost v]
end
基础任务:做完的寻宝游戏 24 分钟
打开你上节课的寻宝游戏。这是最后一次大工程。每做完一步就玩一玩。
怎么用: 点绿旗,然后点 START。在沙子上移动鼠标,听哔声,按下鼠标键来挖。在十次机会用完之前找到三个宝箱。
第 1 步:新的变量
- 新建
treasures、time和best time,选适用于所有角色。 - 把这三个和 digs left 都勾上,让它们显示出来。
第 2 步:Open Chest
- 复制 Treasure。把复制出来的改名叫
Open Chest。删掉它关着的造型和所有脚本。 - 给它 Hole 的两组积木:点绿旗时藏起来,还有克隆体脚本。
第 3 步:Treasure 换地方
- 改一改 Treasure 收到 found 的脚本:数一下、
Coin声音、克隆 Open Chest、去新的随机地点。 - 把它的
when flag clicked改成when I receive [start v]。
第 4 步:标题和 START
- 画一个
Title角色,写上游戏的名字。画一个Start按钮,或者用Button2。 - 给它们两个都加上第 4 部分里的积木。
第 5 步:Detector 等着 START
- 加上
when flag clicked和hide。 - 在其他每个脚本里,把
when flag clicked换成when I receive [start v]。 - 在跟着鼠标走的脚本最上面加上
show。 - 在挖的脚本最上面加上
wait until <not <mouse down?>>。
第 6 步:一局,和赢
- 删掉上节课的看守脚本,改拼一局的脚本。
- 把 Detector 上收到 found 的脚本改成收到 win 的脚本,就像下面这样。
- 赢一次。按绿旗。再赢一次,慢一点。最好成绩一定不能变。
完整的 Detector
when flag clicked
hide
when I receive [start v]
show
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
end
if <<(distance to [Treasure v]) > (60)> and <(distance to [Treasure v]) < (150)>> then
say [Warm]
end
if <(distance to [Treasure v]) > (150)> then
say [Cold]
end
end
when I receive [start v]
wait until <not <mouse down?>>
forever
if <mouse down?> then
if <not <touching [Hole v]?>> then
if <(distance to [Treasure v]) < (60)> then
broadcast [found v]
else
change [digs left v] by (-1)
create clone of [Hole v]
end
end
wait until <not <mouse down?>>
end
end
when I receive [start v]
forever
if <<touching [Snake v]?> or <touching [Snake2 v]?>> then
change [digs left v] by (-1)
start sound [Bonk v]
wait (1) seconds
end
end
when I receive [start v]
forever
start sound [Computer Beep v]
wait ((distance to [Treasure v]) / (200)) seconds
end
when I receive [win v]
stop [other scripts in sprite v]
if <<(best time) = (0)> or <(time) < (best time)>> then
set [best time v] to (time)
end
say (join [All three in ] (join (time) [ seconds!]))
when I receive [lost v]
stop [other scripts in sprite v]
say [Out of digs!]
我第一次挖就挖到天上去了。
挖的脚本少了开头的 wait until <not <mouse down?>>。点 START 的那一下被算成了一次挖。
每局最好成绩都变回 0。
有积木在绿旗或 start 帽子积木下面设置了 best time。把它删掉。只有 win 的脚本可以改它。
最好成绩一直都不变。
or 里少了 <(best time) = (0)> 那一半。最好成绩是 0 的话,永远都打不破。
找到一个宝箱却算成了两个。
奖杯是 Treasure 的克隆体,克隆体也听到了 found。改用另外一个 Open Chest 角色。
我点 START 什么都没发生。
有一个角色挡住了按钮,或者 Detector 显示在它上面。点绿旗时 Detector 必须藏起来。
要打破的纪录
在开始画面上显示最好成绩,这样就有目标了。
- 还没玩过的时候,标题说 No best time yet(还没有最好成绩)。
- 赢了一次以后,它说 Best time: 和那个数字。
- 游戏开始时,它和标题一起藏起来。
Teacher reveal — on the Title
when flag clicked
show
if <(best time) = (0)> then
say [No best time yet]
else
say (join [Best time: ] (best time))
end
The same <(best time) = (0)> test as the win script, doing a different job. Worth pointing out that 0 means “nothing yet” here.
再玩一次
现在要玩第二局,还得按绿旗。不用绿旗,把这个循环连起来。
- 赢了或输了 3 秒以后,标题和 START 又回来了。
- 点 START 开始新的一局。
- 旧的坑和打开的宝箱都不见了。
- 最好成绩留下来。
怎么用: 点绿旗,然后点 START。找到三个宝箱,或者把挖的次数用完。一局结束后,再点 START:沙漠清干净了,最好成绩还在。
Teacher reveal — two small scripts in the right places
when I receive [win v]
wait (3) seconds
show
when I receive [lost v]
wait (3) seconds
show
On the Title and on Start. Then, on the Hole and on the Open Chest:
when I receive [start v]
delete this clone
The original ignores delete this clone, so it is safe to put on the sprite itself. The flag used to clear the clones for us. Now nothing does unless we ask.
奖牌
时间是一个数字。奖牌是一份奖励。发一枚出去吧。
- 少于 20 秒得金牌。
- 少于 40 秒得银牌。
- 再慢一点就得铜牌。
- 探测器先说时间,再说奖牌。
Teacher reveal — the chain from Lesson 13, one last time
when I receive [win v]
stop [other scripts in sprite v]
if <<(best time) = (0)> or <(time) < (best time)>> then
set [best time v] to (time)
end
say (join [All three in ] (join (time) [ seconds!])) for (2) seconds
if <(time) < (20)> then
say [Gold medal!]
else
if <(time) < (40)> then
say [Silver medal!]
else
say [Bronze medal!]
end
end
The arc ends where it started: an if / else chain, smallest question first. Ask the room why gold must be checked before silver. It is the Hot!-before-Warm answer.
越来越小的 Hot 区域
让每个宝箱都比上一个更难找。
- 第一个宝箱在 60 步以内就算找到。
- 你每找到一个宝箱,这个数字就小 15 步。
- 说的词和挖都用新的大小。
怎么用: 点绿旗,然后点 START。它说 Hot! 时,按下鼠标键来挖。每找到一个宝箱,你都要更靠近,挖才算数。
Teacher reveal — one variable, three places
Add set [hot size v] to (60) near the top of the round script. In the Treasure’s “found” script, after the count:
change [hot size v] by (-15)
Then every 60 on the Detector becomes the variable:
if <(distance to [Treasure v]) < (hot size)> then
say [Hot!]
end
if <<(distance to [Treasure v]) > (hot size)> and <(distance to [Treasure v]) < (150)>> then
say [Warm]
end
And in the dig: <(distance to [Treasure v]) < (hot size)>. A pupil who changes only one of the three finds the words and the dig disagree. That is the argument for naming a number.
总结 5 分钟
(timer)会自己数秒。reset timer把它设回 0。- 纪录只有被打破时才会变。别让绿旗碰到它。
<<(best time) = (0)> or <(time) < (best time)>>既包括第一局,也包括每一次更快的成绩。- 克隆体也会听到广播。用另外一个角色来留下奖杯。
- 开始画面就是一个广播。绿旗布置场景;START 开始游戏。
- 计时器
- Scratch 的秒表。它会自己数秒。
- 纪录
- 到现在为止最好的成绩。只有更好的成绩才能代替它。
- 开始画面
- 游戏开始之前,玩家看到的画面。
- 一局
- 一次游戏,从按 START 到赢或输。
做完的寻宝游戏
四节课以前,探测器只会说 Hot! 或 Cold。从那以后,你用过:
- 用
<() < ()>、<() > ()>和<() = ()>来比较; - 用
and、or和not组成更大的问题; - 用
if / then和if / then / else来做选择; - 用
(distance to [Treasure v])、鼠标和计时器来感知周围的世界。
交作业
分享你做完的寻宝游戏,把链接发给老师。下节课开始一个新游戏:一个沿着线走的机器人。