复习:钱 5 分钟
- 建塔之前先检查钱。在算打死的那个脚本里给钱。
- 空位是知道自己
my spot的克隆体,还记得自己已经used(用过)。 - 一波就是一起派出的一群虫子。两波之间的奖励,让玩家有时间做计划。
快问快答
升到第 2 级的塔要花 60,升到第 3 级要花 90。你会把这些数字放在哪里?老师应该不用读你的脚本就能改它们。
看答案
放在一个列表里。upgrade cost 里先是 60,然后是 90。第 1 级的塔付第 1 项,第 2 级的塔付第 2 项。改一下列表,游戏就变了。卷轴世界里的关卡顺序也是这样做的。
今天的主题 3 分钟
- 塔自己的
level,选 for this sprite only(仅适用于当前角色)。 upgrade cost和upgrade range:每一级的价格和攻击范围。- 最高等级,用列表的长度算出来。
- 赢和输,用风暴飞行里的状态。
学习目标
上完这节课,你能够:
- 升级一个克隆体。它自己的等级从列表里选出它的价格和攻击范围。
- 改数据而不改代码,让游戏更公平。
- 让游戏在赢或输的状态下结束。
学一学:升级 28 分钟
1 · 一次升级就是两个列表项
每个塔的克隆体都有自己的 level,从 1 开始。两个列表装着升级的数据。upgrade cost 是 60、90。upgrade range 是 184、240、300。这些是第 1、2、3 级时圈的大小。
define upgrade me
if <(money) < (item (level) of [upgrade cost v])> then
set [last upgrade v] to [not enough money]
else
change [money v] by ((0) - (item (level) of [upgrade cost v]))
change [level v] by (1)
switch costume to (join [tower ] (level))
set [range size v] to (item (level) of [upgrade range v])
set [build spot v] to (my spot)
set [last upgrade v] to (join [now level ] (level))
broadcast [upgrade v] and wait
end
when this sprite clicked
upgrade me :: custom
upgrade,改变自己的大小。怎么用: 点绿旗。在空位 2 买一座塔,然后升级两次。
2 · 最高级
列表里有两个价格,就是有三个等级。当一座塔的等级大于列表的长度,它就到了最高级:
if <(level) > (length of [upgrade cost v])> then
set [last upgrade v] to [already at the top]
else
upgrade me :: custom
end
怎么用: 点绿旗。空位 3 上的一座塔,钱很多,升级了三次。
基础任务:防线 24 分钟
打开你上一课的商店,让塔可以升级。
第 1 步:三种样子
- 给 Tower 再加两个造型,
tower 2和tower 3。画在同一个位置,这样什么都不会移动。 - 给它建
level,选 for this sprite only(仅适用于当前角色)。新的塔从 1 开始。
第 2 步:列表
upgrade cost:60、90。upgrade range:184、240、300。
第 3 步:upgrade me
- 在 Tower 上搭出学一学第 1 部分的自制积木。把第 2 部分的最高级检查放在它外面。
- Range:
when I receive [upgrade v]→ 如果my spot等于build spot,就把大小设为range size。
第 4 步:赢或输
- 一直重复派出一波波敌人,直到第三波结束,或者生命值到 0。
- 然后把
state设为won或over,并说出是哪一个。
怎么用: 点绿旗。点空位建塔,点塔升级。撑过三波。
一次升级扣了两倍的钱。
Tower 本体也在回应。每个只给一座塔的广播,都要加上 is clone 检查。
塔换了造型,可是圈还是小的。
Range 不知道要改哪个空位的大小。或者 range size 是在广播之后才设置的。先设置它,再广播并等待。
第 3 级的塔升到了“tower 4”,然后看不见了。
少了最高级检查。看学一学第 2 部分。
城堡倒了,游戏还在派出一波波敌人。
一波的循环只数了波数。lives 到 0 时,它也一定要停下来。
列表就是平衡
用同样的一波打一座第 2 级的塔,打两次。两次之间只改 upgrade range 的一项。
- 第一次用 240(范围 120),第二次用 300(范围 150)。
- 用
replace item (2) of [upgrade range v] with (300)。别的什么都不改。 - 每次都记下打死了多少只。
怎么用: 点绿旗。同样的一波跑两次。两次之间,列表里的一个数字变了。
Teacher note
Game designers call this tuning, and it is done in exactly this way — in tables, not code. A tuning pass where each pupil changes one list item and a partner plays it is a very good use of ten minutes.
赢和输
游戏一定要有一个结局,不是赢就是输。结束的时候,就不再派出敌人。
- 一直重复派出一波波敌人,直到
wave是 3,或者lives小于 1。 - 然后,如果还有生命,
state就是won;如果没有,就是over。 - 故意不建塔,试一试输的结局。
怎么用: 点绿旗。没有塔,有六条生命,每一波有四只虫子。看它在哪里停下来。
Teacher reveal — the loop
repeat until <<(wave) = (3)> or <(lives) < (1)>>
change [wave v] by (1)
set [bugs to send v] to (4)
broadcast [wave v] and wait
wait until <(bugs alive) = (0)>
end
if <(lives) > (0)> then
set [state v] to [won]
else
set [state v] to [over]
end
Storm Flight’s state again, used for its oldest job: telling every script that the game has finished, and how.
走得最远的一波
做一个 best wave。玩得更差的一局不能让它变小。
- 只在作品打开时把它设为 0。
- 每局结束时,如果这一局走得更远,就把它调高。
怎么用: 点绿旗。玩了两局。第二局比第一局差。
best wave 在两局之间永远不会变小。Teacher note
The fourth “best” in this level — high score, best time, best level, best wave — and by now nobody should need the pattern shown. If a pupil does, point them at their own Cheese Maze.
发布它
把四节课合成一个塔防游戏。做得好到可以交给别人玩。
- 一个标题画面,讲清楚要点空位和点塔。
- 四波越来越大的敌人,两波之间有奖励,最后有一个大头目(一只又大又强的虫子)。
- 买塔、卖塔和升级,价格都来自列表。
- won 和 over 两种结局,还有最好的波数。
怎么用: 点绿旗,读一读标题画面。点空位建塔,点塔升级。先来四波,然后是大头目。
Teacher note
Every tool in Level 3 is in this one project: clones (the bugs, the towers, the pads), lists (the path, the pads, the prices), My Blocks (buy, upgrade), sprite-only variables (health, level, my spot) and game state (title, play, won, over). It is worth saying so out loud when they finish.
总结 5 分钟
- 塔自己的
level从两个列表里选出它的价格和攻击范围。 - 最高等级是价格列表的长度加一。
- 改列表让游戏更公平。脚本不用变。
- 本体一定要忽略只给某一个克隆体的广播。检查
is clone。 - 一直重复派出一波波敌人,直到游戏赢了或输了,然后说出是哪一个。
- 升级
- 花钱让你已经有的东西变得更好。
- 调整数值(Tuning)
- 改数字让游戏更公平或更难,不改它的代码。
- 查找表
- 按位置来读的列表:第 2 级的价格就是第 2 项。
- 综合运用
- 把分开的想法放在一起,做成一个能用的东西。
交作业
保存你的游戏。你学完了塔防的四节课。你在一个作品里用上了第 3 级的每一样工具。