- 学一学28 分钟 · timer、round、repeat until、生命值、or
- 基础任务25 分钟 · 一轮六十秒
- 挑战 1–327 分钟 · 短跑、快一点、两种结局
- 加分任务加分 · 答得快,分数翻倍
复习:我们学到哪里了 5 分钟
(pick random (1) to (10))存在a和b里,每次都能出一道新题。(join (a) (join [ × ] (b)))把题目拼起来。<(answer) = ((a) * (b))>让 Scratch 来检查答案。
快问快答
你的这一轮会问五道题。怎样才能让它刚好问满一分钟呢?
看答案
它需要一个时钟,还需要一个听时钟的话、到时间就停下来的循环。这两样 Scratch 都有。接下来十分钟你就会认识它们。
今天的主题 3 分钟
(timer)和reset timer:Scratch 自带的秒表(round ((60) - (timer))):用整数秒来倒数repeat until:会自己停下来的循环- 三条命,时间到了“或者”命没了,这一轮就结束
学习目标
上完这节课,你能够:
- 用 timer 计时,并显示还剩多少时间。
- 让循环一直重复,直到某件事成立,而不是重复固定的次数。
- 时间用完或命用完时,结束这一轮。
学一学:一个时钟和三条命 28 分钟
1 · Scratch 自带的秒表
秒表从 0 开始往上数。Scratch 里面就有一个,而且它一直在走。
- 点 Sensing(侦测)。它是浅蓝色的那一类。
- 在下面找到
(timer)。勾选它旁边的小方框。 - 舞台上出现一个框。看它一秒一秒地往上数。
- 点它上面的
reset timer。框里的数会一下子回到 0。
2 · 还剩多少时间(整数秒)
玩家想看的是还剩多少时间,不是已经过了多少时间。这是一道减法。
((60) - (timer))
(round ((60) - (timer)))
(round ()) 把它整理成 57。(round ()) 在 Operators 里。它把 56.73 变成 57,把 56.2 变成 56。整数在快的时候更容易看清楚。
3 · 会自己停下来的循环
repeat (5) 数到 5 就停。我们不知道一分钟能做几道题。所以我们需要另一种循环。
Control(控制)里的 repeat until 会一次又一次地运行里面的积木。每次运行之前,它都会检查自己的菱形格子。菱形里的条件成立时,它就停下来。
when flag clicked
reset timer
repeat until <(timer) > (10)>
set [time left v] to (round ((10) - (timer)))
end
set [time left v] to (0)
say [Time's up!] for (2) seconds
怎么用: 点绿旗开始。不用再按别的,看它运行完,然后点 ↺ Reset 再运行一次。
4 · 三条命
一个不会输的游戏不太好玩。给玩家三条命。答错一题,就少一条。
- 新建一个叫
lives的变量。 - 在开头放
set [lives v] to (3)。 - 在 else 那一半,加上
change [lives v] by (-1)。 - 把循环改成
repeat until <(lives) = (0)>。
when flag clicked
set [score v] to (0)
set [lives v] to (3)
repeat until <(lives) = (0)>
set [a v] to (pick random (1) to (10))
set [b v] to (pick random (1) to (10))
ask (join (a) (join [ × ] (b))) and wait
if <(answer) = ((a) * (b))> then
change [score v] by (1)
else
change [lives v] by (-1)
say (join [It is ] ((a) * (b))) for (1) seconds
end
end
say (join [Out of lives! Score: ] (score)) for (4) seconds
怎么用: 点绿旗,然后在框里输入你的答案,按 Enter。
5 · 时间“或者”生命值
时间到了,或者命没了,我们这一轮就应该结束。Operators 里的 <<> or <>> 积木能把两个菱形连在一起。只要有一边成立,它就成立。
repeat until <<(timer) > (60)> or <(lives) = (0)>>
end
| 时间超过 60? | 生命值到 0? | 这一轮结束? |
|---|---|---|
| 否 | 否 | 否,继续问 |
| 是 | 否 | 是 |
| 否 | 是 | 是 |
6 · 时钟需要自己的脚本
ask [] and wait 会让它所在的脚本停下来,直到你按 Enter。如果 time left 也放在同一个循环里,你一边想,它就一边卡住不动。
所以时钟要有自己的一个脚本。两个脚本同时运行,你在第 1 级就做过。
when flag clicked
set [score v] to (0)
set [lives v] to (3)
reset timer
broadcast [clock v]
repeat until <<(timer) > (60)> or <(lives) = (0)>>
set [a v] to (pick random (1) to (10))
set [b v] to (pick random (1) to (10))
ask (join (a) (join [ × ] (b))) and wait
if <(answer) = ((a) * (b))> then
change [score v] by (1)
else
change [lives v] by (-1)
say (join [It is ] ((a) * (b))) for (1) seconds
end
end
say (join [Round over! Score: ] (score)) for (4) seconds
when I receive [clock v]
repeat until <<(timer) > (60)> or <(lives) = (0)>>
set [time left v] to (round ((60) - (timer)))
end
怎么用: 点绿旗,然后在框里输入你的答案,按 Enter。一直做到这一轮结束。
基础任务:一轮六十秒 25 分钟
打开你上节课做的游戏。一次只改一样东西,每改一次就试一试。
第 1 步:两个新变量
- 新建
lives和time left。两个都保持勾选。 - 变量名里有空格没关系。
time left比tl更好懂。
第 2 步:换掉循环
- 把你的积木从
repeat (5)里拖出来,然后把 repeat 扔掉。 - 从 Control 里拿出
repeat until。 - 搭好
<<(timer) > (60)> or <(lives) = (0)>>,放进菱形格子里。 - 把出题的积木放回里面。
第 3 步:一轮的开头
- 在循环上面:先放
set [lives v] to (3),再放reset timer,然后放broadcast [clock v]。 - 在 else 那一半:
change [lives v] by (-1)。
第 4 步:时钟
- 用
when I receive [clock v]开始一个新脚本。 - 在里面放同样的 repeat until。右键点菱形,选 Duplicate(复制)。
- 在里面放:
set [time left v] to (round ((60) - (timer)))。
第 5 步:结尾
- 在循环下面,用 join 说出得分。
- 好好玩一轮。再玩一轮,很快地答错三题。
你的两个脚本应该和“学一学”第 6 部分的那两个一样。那里的作品就是完整的一轮。
这一轮一开始就结束了。
看看菱形格子。它必须是 <(timer) > (60)>。如果箭头朝另一边,循环还没开始就已经结束了。
time left 显示 60,一直不动。
时钟脚本根本没有开始。检查广播和帽子积木是不是都写着 clock,拼写一模一样。
我的命用完了,它还在继续问。
看看菱形格子。它必须写 lives,不是 score;而且要用 or,不是 and。
time left 显示很长的数,比如 41.7333。
减法外面少了 (round ()) 积木。
30 秒短跑
让这一轮只有 30 秒,而不是 60 秒。
- time left 从 30 开始。
- 到 30 秒,这一轮会自己结束。
- 命用完了,也会提前结束。
Teacher note — the number lives in three places
60 appears in the round’s diamond, the clock’s diamond and the clock’s take-away. Change only one and time left runs below zero, or the round stops early. A variable called round length, set once, fixes this for good. Let them discover the need first.
快一点!
让最后十秒变得不一样,这样玩家就知道时间不多了。
- 只剩 10 秒或更少时,巫师会变颜色。
- 他一边等,一边说“Hurry!”(快一点!)。
- 在这之前,他看起来和平常一样。
怎么用: 点绿旗开始。不用再按别的,看它运行完,然后点 ↺ Reset 再运行一次。
Teacher reveal — the clock script does the work
The round script is stuck in its ask block, so the change belongs in the clock. The demo above is 20 seconds long; the game uses the same blocks with 60.
when flag clicked
clear graphic effects
reset timer
repeat until <(timer) > (20)>
set [time left v] to (round ((20) - (timer)))
if <(time left) < (11)> then
set [color v] effect to (120)
say [Hurry!]
end
end
clear graphic effects
say [Time's up!] for (2) seconds
Watch for pupils who put the clear graphic effects only at the end. The next round then starts in the hurry colour.
两种结局
玩家应该知道这一轮为什么结束了。
- 时间用完时,说“Time’s up!”(时间到!)和得分。
- 命用完时,说“Out of lives!”(命用完了!)和答对了几题。
- 每次只会出现一种结局,绝不会两种都出现。
- 每次答错,也要说还剩几条命。
怎么用: 点绿旗,然后在框里输入你的答案,按 Enter。一直做到这一轮结束。
Teacher reveal — one if / else after the loop
if <(lives) = (0)> then
say (join [Out of lives! You got ] (join (score) [ right.])) for (4) seconds
else
say (join [Time's up! You scored ] (score)) for (4) seconds
end
Test lives first. A player can lose their last life at 60.2 seconds, and then both are true. Asking about lives first gives the fairer ending.
The wrong-answer line becomes three joins:
say (join [It is ] (join ((a) * (b)) (join [. Lives left: ] (lives)))) for (2) seconds
The change-lives block must come before this say, or it reports one life too many.
答得快,分数翻倍
3 秒内答对,得 2 分。答得慢一点,得 1 分。
- 答得快时,巫师说“Fast! Double points.”(真快!分数翻倍。)
- 答得慢但答对了,还是得 1 分。
- 答错了,不管快还是慢,都会少一条命。
Teacher reveal
set [asked at v] to (timer)
ask (join (a) (join [ × ] (b))) and wait
if <(answer) = ((a) * (b))> then
if <((timer) - (asked at)) < (3)> then
change [score v] by (2)
say [Fast! Double points.] for (1) seconds
else
change [score v] by (1)
say [Correct!] for (1) seconds
end
else
change [lives v] by (-1)
end
This is the stopwatch trick every racing game uses: never reset the clock, just remember when something started.
总结 4 分钟
今天,游戏有了时钟,也有了输的可能。
(timer)会自己往上数。reset timer让它回到 0。(round ((60) - (timer)))就是还剩的时间,用整数秒表示。repeat until会一直重复,直到它的菱形条件成立。<<> or <>>让这一轮在时间到了“或者”命没了的时候结束。- 一个必须一直走的时钟,要有自己的脚本。
- 计时器(timer)
- Scratch 的秒表。它按秒来数,自己永远不会停。
- 四舍五入(round)
- 把一个数整理成最接近的整数。56.7 变成 57。
- repeat until(重复直到)
- 一个循环,某件事成立时就停下来。
- or(或者)
- 只要有一边成立,它就成立。
下一课:连对可以让答案更值钱,还有简单和困难两种难度。