复习:这个单元到现在 5 分钟
state变量说出现在的情况,每个脚本都要问它。- 生命值,还有一个
hurt标志,让碰一下不会变成六十次。每个重置都放在拥有它的脚本里。 - 每个循环的最上面放
wait until <(state) = [play]>,暂停就能用。
快问快答
你想要一个再玩一次也还在的最高分。哪个脚本把它设回 0?绿旗,还是一局的开头?
看答案
绿旗。最高分比一局活得更久。所以它属于绿旗负责设定的东西。把它放进一局里,你就会遇到挑战 1。
今天的主题 3 分钟
- 飞过克隆体来得分。
best,还有保住它的两行积木。- 用
join做的结果卡。 - 一个菜单状态,还有它带进游戏里的一个设定。
学习目标
上完这节课,你能够:
- 记下这一局的得分,也记下整次游戏的纪录。
- 用玩家想看的方式,显示一局的结果。
- 给一个已经能用的状态机加一个状态,而且不把它弄坏。
学一学:得分,还有纪录 28 分钟
1 · 值得飞过去的东西
只是躲来躲去,算不上什么游戏。星星从右边飘进来。飞过一颗就得一分。它们是克隆体,就像上个单元的泡泡一样:
when I start as a clone
show
set size to (55) %
repeat until <(x position) < (-225)>
wait until <(state) = [play]>
change x by (-11)
if <touching (Parrot v)?> then
change [score v] by (1)
delete this clone
end
end
change [missed v] by (1)
delete this clone
missed 那一行,因为删除先发生了。怎么用: 点绿旗,用方向键穿过星星。
2 · 最高分
有两个变量。唯一的不同是每一个在哪里被重置:
when flag clicked
set [best v] to (0)
set [rounds v] to (0)
when I receive [start v]
set [state v] to [play]
set [score v] to (0)
set [lives v] to (3)
然后在一局结束时,用上你已经见过三次的样子:
set [state v] to [over]
if <(score) > (best)> then
set [best v] to (score)
say [A new best!] for (0.8) seconds
end
怎么用: 点绿旗,让三局自己玩完。看着 best 和 score 比一比。
基础任务:完整的游戏 24 分钟
打开上节课的风暴飞行。所有东西都有了,只差一个让人在乎得分的理由。
第 1 步:变量
- 建
score和best,两个都选 For all sprites(适用于所有角色)。 best在绿旗下面设为 0,别的地方都不设。score在start脚本里和生命值一起设为 0。
第 2 步:星星会得分
- 在星星克隆体飘的循环里,加上碰到检查和
change [score v] by (1)。 - 马上删掉这个克隆体。不然一颗星星会一直不停地得分。这和第 3-22 课是同一个每一帧都算的问题。
第 3 步:闪电也做成克隆体
- 给 Bolt 和 Star 一样的生成器加克隆体的样子(一个专门做克隆体的角色)。
- 闪电飘得比星星快。碰到时不会被删掉。它们会扣一条命。
- 两个克隆体循环都需要
wait until <(state) = [play]>。
第 4 步:结局
- 生命值到 0 时,一局的循环就结束:
repeat until <(lives) = (0)>。 - 把状态设为 over,然后做最高分检查。
- 把鹦鹉移到中间,把它变大,报告得分和最高分。
set [state v] to [over]
if <(score) > (best)> then
set [best v] to (score)
end
go to x: (0) y: (0)
set size to (70) %
say (join [GAME OVER - ] (join (score) (join [ stars. Best ] (best)))) for (2.5) seconds
say [Space for the title.] for (1.5) seconds
join 里面套 join。三段文字和两个数字,放在一句话里。怎么用: 点绿旗,按 space 键,飞去吃星星,躲开闪电。按 P 键暂停。
一颗星星得了几十分。
被吃到时,克隆体没有被删掉。所以它碰着鹦鹉的每一帧,碰到检查都是真。
游戏结束以后,得分还在往上涨。
上一局的星星克隆体还在飘。状态离开 play 时把它们删掉。或者让一局结束时把它们删掉。
最高分永远和得分一样。
set [best v] to (0) 放在了一局的脚本里。这就是挑战 1。
对话框显示了文字,可是没有数字。
你把变量的名字打进了文字里。要把橙色的椭圆积木拖进去。你应该看到一块圆圆的橙色积木放在 join 里面。
暂停时所有东西都冻住了,只有得分没有。
很好,这是对的。得分是一个数字,不是一个动作。暂停的时候它没有事要做。
被清掉的最高分
把 set [best v] to (0) 放进一局的脚本里。然后玩三局。
- 连着玩三局。
- 每局结束后看着
best。 - 写一句话:现在最高分量的是什么?
怎么用: 点绿旗,在三局里的每一局结束时看着 best。
Teacher note
The answer to “what is it measuring?” is this round’s score, which is the reason the bug is so quiet: the number is never absurd, just useless. Contrast it with the negative lives in 3-22, which announce themselves. Some bugs shout and some whisper, and the whispering ones need this kind of deliberate test.
值得一看的卡片
一个数字是一个结果。四个数字是一个故事。把整局报告出来。
- 吃到的星星,还有漏掉的星星。
- 你撑了多久,用真实的秒数。
- 到现在为止的最高分,用来比较。
- 全部放在两行里,用
join做出来。
怎么用: 点绿旗,飞几秒,然后读最后那张两行的卡片。
Teacher reveal — making the seconds honest
repeat (24)
wait until <(state) = [play]>
next costume
wait (0.25) seconds
change [my time v] by (0.25)
end
The wait is what makes the number true. Without it the loop runs at full speed and adds a quarter each time, so a one-second run reports six seconds — a number that looks like a time and is not one. Quarters also avoid the 0.1 problem from last lesson.
难度菜单
在标题和游戏之间加第五个状态。让玩家来选。
- 一个
menu状态。按 1、2、3 键会把difficulty设为一个词。 - 每个选择也会把
bolt speed设为一个数字。 - 按 space 键离开菜单,开始这一局。
- 闪电在整局里都读这个数字。
怎么用: 点绿旗,按 1、2 或 3 来选,然后按 space 键起飞。
Teacher reveal — why the number and the word are separate
when [3 v] key pressed
if <(state) = [menu]> then
set [difficulty v] to [hard]
set [bolt speed v] to (11)
end
The word is for the player and the card; the number is for the bolts. This is the other half of Lesson 3-21’s argument — words where people read them, numbers where the code needs to calculate. And the if guard is why pressing 3 mid-flight cannot change the difficulty under you.
发布它
把整个单元放进一个游戏里。让它好到可以给别人玩。
- 先是标题,然后是菜单,然后是游戏。
- 三条命、一个 hurt 标志,还有一个在哪里都能用的暂停。
- 要吃的星星和要躲的闪电,都做成克隆体。
- 一张得分卡,还有一个每一局都保留的最高分。
怎么用: 点绿旗,选一个难度,然后开始飞。按 P 键暂停,按 space 键重新开始。
Teacher note
The paper diagram is the deliverable, not the game. A pupil who can draw their own state machine has the idea; one who cannot has a working game by accident. Lesson 3-45 comes back to exactly this with a planning sheet.
总结 5 分钟
- 要收集的克隆体,一被吃到就马上删掉。不然它每一帧都会得分。
score由一局来重置。best由绿旗来重置。就是这一个不同,让最高分能用。if <(score) > (best)> then是你在三个单元里都用过的同一个样子。join里面套join,就能把好几个数字放进一句话里。- 如果每个脚本都已经会问状态,加一个新状态就很安全。这就是为什么菜单很简单,暂停却很难。
- 最高分
- 整次游戏里最好的结果,在每一局之间保留着。
- 菜单状态
- 一个只负责让玩家选择设定的状态。
- 设定
- 选一次、整个游戏都会用到的数值,比如闪电速度。
- 结果卡
- 游戏在你刚玩完一局后显示的报告。
交作业
保存你发布的游戏。游戏状态单元你已经学完了。从现在开始,你做的每个游戏都会有标题、暂停和游戏结束,因为你已经知道怎么做了。