复习:答题大师 5 分钟
(item (3) of [questions v])读出第 3 行。(length of [questions v])能让循环跑任意多次。<[accepted v] contains (answer)?>一次就查遍整个列表。
快问快答
<[names v] contains [Bat]?> 说 true。Bat 在第几行?
看答案
你说不出来。contains 只告诉你有没有。今天你会认识一块告诉你在哪里的积木。下节课,你要自己把它写出来。
今天的主题 3 分钟
- 三个一起走的列表:一张牌分在三个列表的同一行
(item # of [Bat] in [names v]):给出位置,不是值- 用 0 表示“不在列表里”
- 走一遍牌组,找出最大的,再把它们全部加起来
学习目标
上完这节课,你能够:
- 把一样东西的几条信息放在几个一起走的列表里。
- 找出一样东西在第几行,并处理好它不在列表里的情况。
- 走一遍列表,一路记住“目前最好的”和一路加起来的总数。
学一学:一副牌,怎样从里面拿东西 28 分钟
1 · 一张牌,三条信息
这个游戏里的每张卡牌都有一个名字、一个力量和一张图片。Scratch 没办法把三样东西放进列表的同一行,所以我们用三个列表,让它们一起走:
| 行 | names | powers | pics |
|---|---|---|---|
| 1 | Dragon | 9 | 1 |
| 2 | Shark | 7 | 2 |
| 3 | Dinosaur | 8 | 3 |
| 4 | Bat | 4 | 4 |
每个列表的第 4 行合起来就是一张牌。你在第 3-7 课用题目和答案见过这个办法。这里是三个列表,不是两个,而且更重要。
when flag clicked
delete all of [names v]
delete all of [powers v]
delete all of [pics v]
add [Dragon] to [names v]
add (9) to [powers v]
add (1) to [pics v]
add [Shark] to [names v]
add (7) to [powers v]
add (2) to [pics v]
怎么用: 点绿旗,看两个列表一起填满,然后卡牌变成第 4 行的样子。
2 · 发牌:一个行号,用三次
要把一张牌放到桌上,三条信息都要用到,它们都来自同一个 n:
switch costume to (item (n) of [pics v])
set [power v] to (item (n) of [powers v])
say (join (item (n) of [names v]) (join [ - power ] (power))) for (1.3) seconds
怎么用: 点绿旗,看四张牌从牌组滑到你的位置,每张都有自己的名字和力量。
3 · 它在哪里?item # of
到现在为止,每块列表积木回答的都是“第 4 行是什么?”。这一块回答相反的问题:
(item (4) of [names v])
(item # of [Bat] in [names v])
如果列表里根本没有这样东西,它会给出 0。这个答案很有用,因为没有第 0 行。
怎么用: 点绿旗,看三次查找:Bat、Dragon,还有一个不在牌组里的 Unicorn(独角兽)。
基础任务:做牌组,找一张牌 24 分钟
新建一个作品。画一个或选一个桌子背景,再加一个卡牌角色。
第 1 步:六个造型
- 从角色库里选六个动物角色,把它们都放进同一个角色,当作它的造型。
- 记下它们的顺序,pics 列表存的就是这个。
- 把角色的大小设成 55 左右,让它放得进一个位置。
第 2 步:三个列表
- 建好
names、powers和pics。 - 在绿旗下面把三个列表都清空。
- 加入你的六张牌,每张用三块积木,顺序永远一样。
- 显示 names 和 powers 两个框,检查它们是不是对齐。
第 3 步:发一张
- 建一个变量
n,把它设成 1。 switch costume to (item (n) of [pics v])。- 说出同一行的名字和力量。
- 把 n 改成 4,再运行一次。
第 4 步:发几张
- 用
repeat (4)把第 3 步包起来,在最上面放change [n v] by (1)。 - 加一个从牌组滑到你的位置的动作,让每张牌都滑过来。
第 5 步:找一张
- 加上
ask [Which card?] and wait。 set [at v] to (item # of (answer) in [names v])。- 如果
at是 0,就说它不在牌组里。不然就把它显示出来。
ask [Which card?] and wait
set [at v] to (item # of (answer) in [names v])
if <(at) = (0)> then
say (join (answer) [ is not in the deck.]) for (2) seconds
else
switch costume to (item (at) of [pics v])
say (join [Row ] (join (at) (join [, power ] (item (at) of [powers v])))) for (2) seconds
end
怎么用: 点绿旗,看完三次查找,包括什么都没找到的那一次。
图片总是和名字对不上。
pics 列表没跟上,或者造型的顺序和你记下的不一样。把三个框都显示出来,横着读第 3 行。
找一张明明有的牌,它却说没有。
大小写没关系,Scratch 比较的时候不管大小写。但是后面多一个空格就不行了。检查列表那一行有没有多出空格。
不在牌组里的牌,显示出来的却是 Dragon。
at 是 0 的时候,你还在读 (item (at) of [pics v])。用它之前先检查是不是 0。
每次按绿旗,列表都变得更长。
少了那三块 delete all of 积木。和第 3-5 课是同一个坑,只不过这次是三个。
最强的卡牌
走一遍整个牌组,找出力量最大的那张牌。
- 不可以看着牌组,把答案直接打进去。
- 你改了一个力量,或者加了一张牌以后,它还是要能用。
- 赢家的名字、力量和图片都要对。
怎么用: 点绿旗,看它走过整个牌组,每找到一张更强的牌就喊出来。
Teacher reveal — “best so far”
set [best v] to (0)
set [best row v] to (0)
repeat (length of [powers v])
change [n v] by (1)
if <(item (n) of [powers v]) > (best)> then
set [best v] to (item (n) of [powers v])
set [best row v] to (n)
end
end
Keeping the ROW as well as the value is the bit that matters — with just the power you know it is a 9 but not whose. Level 3-4’s “worst day” challenge was the same shape, which is worth pointing out.
发一副手牌
从牌组里拿出三张牌,放进手牌。
- 每发一张牌,就把它加进
hand列表。 - 再把它从牌组的三个列表里都删掉。
- 最后,手牌有 3 张,牌组也有 3 张。
怎么用: 点绿旗,看三张牌离开牌组,到手牌里去。两个框都读一读。
Teacher note
The trap is deleting from names only. Ask them to deal three and then show the powers box — if it still has six rows, the deck is broken and every card from row 1 down now has the wrong power.
这副牌值多少?
把牌组里每张牌的力量都加起来。
- 一个从 0 开始、一路加起来的总数。
- 走过每一行,就把那一行加上。
- 牌组不管多大,答案都要对。
怎么用: 点绿旗,跟着总数一路往上:9、16、24、28、31、36。
Teacher reveal
set [total v] to (0)
repeat (length of [powers v])
change [n v] by (1)
set [total v] to ((total) + (item (n) of [powers v]))
end
change [total v] by (item (n) of [powers v]) does the same job in one block, and is worth showing second — after they have seen the long way, because the long way is the one that generalises.
谁的手牌更好?
两副手牌,两个总数,一个赢家。
- 两个力量列表:你的和对手的。
- 一个循环同时算出两个总数。
- 总数大的赢,电脑说出比分。
怎么用: 点绿旗,看两个总数一起往上加,然后看结果。
Teacher reveal
repeat (length of [mine v])
change [n v] by (1)
set [my total v] to ((my total) + (item (n) of [mine v]))
set [their total v] to ((their total) + (item (n) of [theirs v]))
end
if <(my total) > (their total)> then
say (join [I win, ] (join (my total) (join [ to ] (their total)))) for (3) seconds
end
Ask what happens if the two hands are different lengths. The loop is driven by (length of [mine v]), so it quietly reads nothing off the end of the shorter one — a real bug, and a good one to find on purpose.
总结 5 分钟
- 一样东西的几条信息放在几个列表里,用行号让它们一起走。
(item (4) of [names v])是“第 4 行是什么”。(item # of [Bat] in [names v])是“Bat 在第几行”。哪里都找不到时,答案是 0。- 在一个列表里找到的行号,可以用来读其他列表。
- “目前最好的”和“一路加起来的总数”,都只要沿着列表走一趟。
- 一起走(in step)
- 几个列表的行号对得上,每样东西占一行。
- 位置
- 一样东西在第几行。
- 目前最好的
- 一个变量,你一边找,它一边存着你见过的最好的那个。
- 累计总数
- 一行一行加起来的和。
交作业
保存你的牌组。下节课,你要自己写出 item # of,看看它一直在帮你做多少事。