- 学一学27 分钟 · 方法、五个常见嫌疑、变量显示框、侦察
- 基础任务25 分钟 · 修好坏掉的 Star Catcher
- 挑战 1–327 分钟 · 嫌疑检查、你的变量显示框、错误记录
- 加分任务加分 · 调试开关
复习:我们学到哪里了 5 分钟
- 一份计划回答六个问题,还要画出舞台。
- 核心循环就是你一遍又一遍在玩的那部分。
- 接东西、躲东西、点东西的游戏,都用克隆体:出现、移动、被删除。
快问快答
玩了几分钟以后,星星不再出现了。别的都没问题。错误是什么?
看答案
克隆体一直没被删除。Scratch 一次最多允许 300 个克隆体。用完以后,create clone of [myself v] 就悄悄地什么也不做。
今天的主题 3 分钟
- 什么是错误:游戏做了计划里没说的事
- 五个常见嫌疑
- 变量显示框:玩的时候能看见的数字
- 一次只运行一个脚本,用
say []来侦察
学习目标
上完这节课,你能够:
- 用两行话说清一个错误:应该发生什么,实际发生了什么。
- 检查作品里的每个角色,看有没有五个常见嫌疑。
- 用变量显示框和侦察,找出错误藏在哪个脚本里。
学一学:怎样抓错误 27 分钟
1 · 什么是错误
游戏做了你的计划里没说的事,这就是错误。每个错误都用两行话写下来。
SHOULD A star that reaches the bottom disappears. DOES Stars pile up along the bottom.
然后每次都按同样的五步来做。
- 说出应该发生什么。
- 说出实际发生了什么。
- 找到负责这件事的那一个脚本。
- 只改一个地方。
- 再试一次,用和上次一样的方法。
2 · 五个常见嫌疑
第 2 级游戏里的错误,大多数都是这五种之一。先检查它们。
| # | 嫌疑 | 你看到什么 | 要找什么 |
|---|---|---|---|
| 1 | 缺少帽子积木 | 一个脚本从来不运行。 | 一组积木的顶上没有帽子积木。 |
| 2 | 消息名字不对 | 广播发出去,没有人收到。 | 两个名字几乎一样。 |
| 3 | 变量没有重置 | 新游戏开始时还是旧的数字。 | 开头没有 set [score v] to (0)。 |
| 4 | 克隆体没被删除 | 东西越堆越多,然后不再出现。 | 克隆体的脚本最后没有 delete this clone。 |
| 5 | 循环挡路 | 循环后面的积木从来不运行。 | 一个 forever 里有两件事。 |
嫌疑 1 · 缺少帽子积木
没有帽子积木的一组积木,没有东西能让它开始。它会一直待在那里。
hide
forever
create clone of [myself v]
wait (1) seconds
end
when flag clicked
hide
forever
create clone of [myself v]
wait (1) seconds
end
嫌疑 2 · 消息名字不对
Scratch 要求消息名字一模一样。game over 和 gameover 是两个不同的消息。
broadcast [game over v]
when I receive [gameover v]
say [Game over!] for (2) seconds
broadcast [game over v]
when I receive [game over v]
say [Game over!] for (2) seconds
嫌疑 5 · 循环挡路
forever 永远不会结束,所以它下面什么也放不了。它里面的 wait until <> 会让循环里其他的积木都等着。同时做两件事,就要两个脚本。
when flag clicked
forever
wait until <(lives) < (1)>
broadcast [game over v]
if <key [right arrow v] pressed?> then
change x by (10)
end
end
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (10)
end
end
when flag clicked
wait until <(lives) < (1)>
broadcast [game over v]
3 · 变量显示框
变量显示框就是舞台上那个橙色的小框,它显示一个变量。玩的时候,你能看到它在变。
- 打开 Variables(变量)分类。
- 勾上变量旁边的小方框。它的显示框就会出现在舞台上。
- 玩游戏,一只眼睛一直看着显示框。
- 问自己:它是在计划说的时候变的吗?只在那个时候变吗?
你也可以专门做一个变量,只是用来看。(stars falling) 数的是现在还在的星星克隆体。它应该一直在三个左右。
when flag clicked
hide
set [stars falling v] to (0)
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
change [stars falling v] by (1)
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-4)
if <touching [Cat Flying v]?> then
change [score v] by (1)
change [stars falling v] by (-1)
delete this clone
end
end
change [stars falling v] by (-1)
delete this clone
delete this clone 之前减 1。怎么用: 点绿旗,再点一下舞台。按住 ← 或 → 让猫移动,看着角落里的三个显示框。
4 · 一次只运行一个脚本
在编辑器里,你可以单独运行一个脚本。这就是检查嫌疑的方法。
- 按红色的停止按钮,让游戏停下来。
- 在脚本区的空白地方点右键。选 Clean up Blocks(整理积木)。
- 找找有没有没连在一组积木上的积木。单独的积木从来不会运行。
- 点一个脚本。它运行时会发出黄光。看看它自己会做什么。
- 一块一块地大声读出来。你解释不了的那块积木,就是你的嫌疑。
5 · 用 say 来侦察
侦察积木是你只为了找错误才加的积木。say [] 让角色告诉你它知道什么。
when I start as a clone
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-6)
say (round (y position))
if <touching [Cat Flying v]?> then
say [hit!]
change [lives v] by (-1)
wait (0.5) seconds
delete this clone
end
end
delete this clone
怎么用: 点绿旗,再点一下舞台。按住 ← 或 → 让猫碰到往下掉的石头。每块石头都会说出它在哪里,打中你时会说 hit!。
基础任务:修好坏掉的 Star Catcher 25 分钟
这个 Star Catcher(接星星)里藏了五个错误,每种嫌疑一个。先把它搭出来,再把五个错误都找出来、修好。
坏掉的游戏
照着下面印的积木,一模一样地搭。这些错误是故意的。
when flag clicked
set [lives v] to (3)
go to x: (0) y: (-130)
set rotation style [left-right v]
forever
wait until <(lives) < (1)>
broadcast [game over v]
if <key [right arrow v] pressed?> then
point in direction (90)
change x by (10)
end
if <key [left arrow v] pressed?> then
point in direction (-90)
change x by (-10)
end
end
when I receive [gameover v]
say [Game over!] for (2) seconds
stop [all v]
when flag clicked
hide
forever
create clone of [myself v]
wait (1) seconds
end
when I start as a clone
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-4)
if <touching [Cat Flying v]?> then
change [score v] by (1)
delete this clone
end
end
hide
forever
wait (pick random (2) to (4)) seconds
create clone of [myself v]
end
when I start as a clone
go to x: (pick random (-210) to (210)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-6)
if <touching [Cat Flying v]?> then
change [lives v] by (-1)
delete this clone
end
end
delete this clone
玩家看到什么
五个错误,五种表现。它们大概按这个顺序出现。
- 不管你按什么,猫都不动。
- 一块石头也不掉下来。
- 掉到底的星星一直留在那里,越堆越多。
- 生命值变成 0,然后变成 −1,游戏一直不结束。
- 新游戏开始时,还是上一局的得分。
怎么用: 点绿旗,再点一下舞台。按住 ← 或 → 让猫移动。把三条命都输掉,就能看到游戏结束。
第 1 步:搭出来
- 照着印的积木,一模一样地搭出三个角色和它们的积木。
- 新建
(score)和(lives),选“适用于所有角色”。
第 2 步:玩一玩,写下来
- 玩三十秒。然后按绿旗,再玩一次。
- 你看到的每个问题,都写一组“应该 / 实际”。
第 3 步:打开变量显示框
- 在 Variables(变量)分类里勾上 score 和 lives。
- 再玩一次。哪个显示框做了你的计划里没说的事?
第 4 步:检查嫌疑
- 在每个角色上都选 Clean up Blocks(整理积木)。
- 每个角色都把五个嫌疑检查一遍,一次查一个。
- 把你觉得错的积木记下来。
第 5 步:修一个,试一试,再重复
- 只修一个错误。
- 按绿旗,用和上次一样的方法试一试。
- 在清单上把这个错误打勾。接着做下一个。
第 6 步:比一比
- 先玩你的游戏,再玩上面修好的游戏。
- 它们玩起来应该一样。如果不一样,就还有错误没修。
检查你的修法(自己试过以后再看)
when flag clicked
set [score v] to (0)
go to x: (0) y: (-130)
set rotation style [left-right v]
forever
if <key [right arrow v] pressed?> then
point in direction (90)
change x by (10)
end
if <key [left arrow v] pressed?> then
point in direction (-90)
change x by (-10)
end
end
when flag clicked
set [lives v] to (3)
wait until <(lives) < (1)>
broadcast [game over v]
when I receive [game over v]
say [Game over!] for (2) seconds
stop [all v]
Star 的 repeat until 后面要加一个 delete this clone。Rock 的第一组积木要把 when flag clicked 放回去。
我修好了猫,可是游戏一开始就结束了。
wait until <> 那个脚本在还没有人设好 lives 的时候就检查了。把 set [lives v] to (3) 放在同一个脚本的最上面。
我找不到第 5 个错误,就是得分那个。
它只在第二局才出现。先玩一局,然后再按一次绿旗,看着 score 显示框。
嫌疑检查
现在轮到你自己的游戏了。检查每个角色,五个嫌疑都要查。
- 写一张表:左边一列是你的角色,上面一行是五个嫌疑。
- 检查过、没问题的格子打一个勾。
- 找到错误的格子打一个叉,每个叉都要修好。
看着你的数字
让你游戏里的每个数字,在玩的时候都看得见。
- 游戏里的每个变量都有一个显示框。
- 每个会做克隆体的角色,都有一个克隆体计数器。
- 玩一分钟,眼睛看着数字,不看角色。
Teacher note — the counter that climbs
A clone counter that climbs slowly is the most common find. The usual cause is a second way out of the clone’s loop with no delete after it — a clone that leaves by the side, say, when the pupil only checked the bottom.
错误记录
在你自己的游戏里找出三个真正的错误,每一个都好好地写下来。
- 三条记录。每条都有:应该、实际、在哪里、修法。
- 每次修只改一个地方,试过以后再修下一个。
- 然后做“冷静测试”:先放十分钟不管它。
- 关上所有脚本窗口来玩,只照着屏幕上的提示做。
Teacher note — finding a bug is a success
Pupils often hide bugs, as if they were mistakes. Treat every log entry as a win. A pupil who says “I have no bugs” has not looked yet.
Where two pupils happen to be at this point together, swapping games for the cold test is a nice bonus. It is never the requirement.
调试开关
真正的游戏会留着它们的工具,只是藏起来。按一个键,显示框就出现,侦察积木也打开。再按同一个键,它们又藏起来。
怎么用: 点绿旗,再点一下舞台。按住 ← 或 → 让猫移动。按 D 显示变量显示框和石头的侦察,再按一次 D 把它们藏起来。
Teacher notes — the switch, on the Stage
when [d v] key pressed
if <(debug) = (0)> then
set [debug v] to (1)
show variable [score v]
show variable [lives v]
show variable [stars falling v]
else
set [debug v] to (0)
hide variable [score v]
hide variable [lives v]
hide variable [stars falling v]
end
In the rock’s loop, the probe becomes say (round (y position)) inside an if <(debug) = (1)> then.
Going further: in Level 3 a list can keep a diary of what happened, with add (join [lives: ] (lives)) to [log v].
总结 4 分钟
有错误是很正常的。找错误是一种本领,现在你有工具了。
- 用“应该”和“实际”写下一个错误。
- 检查五个嫌疑:帽子积木、名字、重置、删除、循环。
- 变量显示框让你在玩的时候看到数字在变。
- 一次只运行一个脚本,并且大声读出来。
- 侦察积木让角色说出它知道的东西。
- 只改一个地方,然后再试一次。
- 错误(bug)
- 游戏做了计划里没说的事。
- 调试(debugging)
- 找到错误并修好它,一次只改一个地方。
- 变量显示框
- 舞台上显示一个变量的小框。
- 侦察
- 只为了找错误才加的积木,比如一个 say。
下一课:你的游戏能玩了。现在我们让它感觉像一个真正的游戏:标题画面、声音,还有最高分。