复习:图块工坊 5 分钟
- 一个关卡就是一个叫
level的列表:12 行,每行是一串 16 个字母。#是砖块,.是空地,S是起点。 - Tile 角色每个字母都有一个 30 × 30 的造型,造型名就是那个字母。
load level :: custom把十二行加进去;draw level :: custom用go to cell (c) (r) :: custom走到每个格子,然后盖图章。 - 存档码就是把 12 行连起来:192 个字母。
make code :: custom把它打包,unpack code :: custom把它切回一行一行。
快问快答
图块工坊 II 是这样找出鼠标下面的格子的。列是 floor((x + 240) ÷ 30) + 1。行是 floor((180 − y) ÷ 30) + 1。点 (−100, 50) 在哪个格子里?
看答案
第 5 列,第 5 行。(−100 + 240) ÷ 30 = 4.67,向下取整是 4,再加 1 是 5。(180 − 50) ÷ 30 = 4.33,向下取整是 4,再加 1 是 5。今天,这个算式要变成一个 My Block。
今天的主题 3 分钟
- 一个
tile at (x) (y) :: custom自制积木:把一个点变成一个格子,再从格子得到一个字母 - 用网格计算来做墙和地板,不靠颜色
- 四个角:骑士的脚、头和两边
- 刚好落在一个图块的上面
学习目标
上完这节课,你能够:
- 把舞台上任何一个点变成一列、一行,还有存在那里的字母。
- 让一个骑士在图章印出来的图块地图上走、掉下来、落地。
- 说出为什么读地图比碰颜色更好。
学一学:一个会读地图的骑士 30 分钟
1 · 地图就是一个列表
这是我们今天用的城堡地面。左边是列表。舞台上是 draw level :: custom 用它印出来的样子。
row 1 #..............# row 2 #..............# row 3 #..............# row 4 #..............# row 5 #..............# row 6 #..............# row 7 #..........##### row 8 #..............# row 9 #......###.....# row 10 #...........##.# row 11 #S...#......##.# row 12 ################
这就是你在图块工坊做的 Tile 角色:同样的九个造型,用字母命名,还有同样的自制积木。它先载入列表,再把它印出来:
define load level
delete all of [level v]
add [#..............#] to [level v]
add [#..............#] to [level v]
add [#..............#] to [level v]
add [#..............#] to [level v]
add [#..............#] to [level v]
add [#..............#] to [level v]
add [#..........#####] to [level v]
add [#..............#] to [level v]
add [#......###.....#] to [level v]
add [#...........##.#] to [level v]
add [#S...#......##.#] to [level v]
add [################] to [level v]
define draw level
erase all
show
set [r v] to (1)
repeat (12)
set [c v] to (1)
repeat (16)
go to cell (c) (r) :: custom
switch costume to (letter (c) of (item (r) of [level v]))
stamp
change [c v] by (1)
end
change [r v] by (1)
end
hide
when flag clicked
hide
load level :: custom
draw level :: custom
broadcast [play v]
2 · 点 → 格子 → 字母
骑士要一遍又一遍地问同一个问题:这个点上有什么?图块工坊的光标问的是鼠标所在的点。现在我们要问任何一个点,分三步。
- 列。把点移一移,让左边缘变成 0:加 240。除以 30,向下取整。再加 1,因为列是从 1 开始数的。
- 行。行是从上往下数的,所以先算 180 − y。除以 30,向下取整,再加 1。
- 字母。从列表里取出那一行。再从那一行里取出那个字母。
define tile at (x) (y)
set [col v] to (([floor v] of (((x) + (240)) / (30))) + (1))
set [row v] to (([floor v] of (((180) - (y)) / (30))) + (1))
set [tile v] to (letter (col) of (item (row) of [level v]))
x 和 y。勾上 Run without screen refresh(不刷新屏幕运行)。它用三个变量给出答案。| 点 | col | row | tile |
|---|---|---|---|
| (−100, 50) | 5 | 5 | . —— 空的 |
| (15, −80) | 9 | 9 | # —— 突出的砖台 |
| (−195, −160) | 2 | 12 | # —— 地板 |
| (−195, −135) | 2 | 11 | S —— 起点 |
怎么用: 点绿旗,然后把鼠标指针移到地图上。十字准星会说出它下面的列、行和字母。
([floor v] of (4.67)) 是向下取整,得到 4。所以一个格子里的任何一个点,都会得到同一列,就算点刚好在格子的左边缘上也一样。
3 · 为什么不用碰颜色?
在跳跃冒险里,地板是一整片绿色,落地靠的是 <touching color [#3FA34D]?>。图块地图可以做得更好。
| 碰颜色 | 网格计算 | |
|---|---|---|
| 图块可以长什么样 | 每种只能用一个固定的纯色 | 怎么样都行,砖块图块就有五种颜色 |
| 一个硬币和一把钥匙,都是金色 | 颜色一样,所以被当成同一样东西 | * 和 K:两个不同的字母 |
| 地板的顶边在哪里 | 一个像素一个像素地去摸 | 用行号算出来 |
| 修改关卡 | 重新画一个背景 | 改列表里的一个字母 |
4 · 四个角
骑士不是一个点。我们检查它的四个角:左右各 8 个像素,上下各 11 个像素。corner (dx) (dy) :: custom 检查其中一个角,如果它在砖块里,就算撞到一次。
define corner (dx) (dy)
tile at ((x position) + (dx)) ((y position) + (dy)) :: custom
if <(tile) = [#]> then
change [hits v] by (1)
end
define check box
set [hits v] to (0)
corner (-8) (-11) :: custom
corner (8) (-11) :: custom
corner (-8) (11) :: custom
corner (8) (11) :: custom
hits 是 0。5 · 掉下来,落在网格上
重力和跳跃冒险里的一样:y velocity 减 1,再按它移动。新的部分是:碰撞区跑进砖块里时要怎么办。
- 往下掉(
y velocity小于 0):脚跑进了某一行。第r行的顶边在y = 210 − 30 × r。站在它上面 12 的地方。 - 往上跳:头跑进了某一行。那一行的底边在
y = 180 − 30 × r。停在它下面 12 的地方。
define fall
change [y velocity v] by (-1)
change y by (y velocity)
set [on ground v] to (0)
check box :: custom
if <(hits) > (0)> then
if <(y velocity) < (0)> then
tile at (x position) ((y position) - (11)) :: custom
set y to (((210) - ((30) * (row))) + (12))
set [on ground v] to (1)
else
tile at (x position) ((y position) + (11)) :: custom
set y to (((180) - ((30) * (row))) - (12))
end
set [y velocity v] to (0)
end
怎么用: 点绿旗开始。别的都不用按:骑士会掉到地板上,然后在突出的砖台下面跳起来。
6 · 墙
走路用的是奶酪迷宫的方法:移动、检查、退回。往旁边走一步。如果碰撞区现在在砖块里,就直接退回来。
define walk (speed)
change x by (speed)
check box :: custom
if <(hits) > (0)> then
change x by ((0) - (speed))
end
walk (4) :: custom,往左走用 walk (-4) :: custom。怎么用: 点绿旗开始。别的都不用按:看骑士走到台阶前,跳过去,再爬上塔。
基础任务:你的地图上的骑士 25 分钟
自己动手做出“学一学”里的骑士,用上面的地图或者你自己的地图都可以。
第 1 步:地图
- 新建一个作品,加上 Pen 扩展。把你的 Tile 角色搬过来:在图块工坊里,右键点它,选 export(导出);在这里,用 Upload Sprite(上传角色)。或者用我们的图块文件,按图块工坊 I 重新做一个。
- 保留
load level :: custom、go to cell (c) (r) :: custom和draw level :: custom。把今天的十二行打进load level,或者用你自己的关卡。 - 把
r和c设成仅适用于当前角色,并在draw level里用它们。如果你的 draw level 最后用了画刷的虚像特效,就像第 1 部分那样,把show和hide放回去。 - 在 Tile 的绿旗脚本最后,加上
broadcast [play v]。
第 2 步:骑士
- 点 Choose a Sprite(选择一个角色),搜索
Knight,把它加进来。 - 做六个适用于所有角色的变量:
col、row、tile、hits、y velocity、on ground。
第 3 步:自制积木
- 先做
tile at (x) (y) :: custom,再做corner (dx) (dy) :: custom,然后做check box :: custom。 - 做
walk (speed) :: custom和fall :: custom。 - 五个都勾上 Run without screen refresh(不刷新屏幕运行)。
第 4 步:游戏循环
- 用
when I receive [play v]开头。骑士会等到地图画好。 - 设置好:左右翻转,大小 18,放在 S 的位置 (−195, −135)。
- 在 forever 里:方向键、跳跃,然后掉落。
- 点绿旗。走到台阶前。跳上塔。撞一撞突出的砖台。
when I receive [play v]
set rotation style [left-right v]
set size to (18) %
point in direction (90)
go to x: (-195) y: (-135)
set [y velocity v] to (0)
forever
if <key (right arrow v) pressed?> then
point in direction (90)
walk (4) :: custom
end
if <key (left arrow v) pressed?> then
point in direction (-90)
walk (-4) :: custom
end
if <<key (space v) pressed?> and <(on ground) = (1)>> then
set [y velocity v] to (12)
end
fall :: custom
end
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
骑士直接穿过地板掉下去了。
tile at (x) (y) :: custom 读的是一个空列表。检查两个角色里的列表是不是都叫 level,还有地图是不是在广播 play 之前就建好了。
骑士站着不动的时候上下抖。
落地用的是 + 11。它必须是 + 12,比四个角多 1。
骑士穿墙走过去了。
在 walk (speed) :: custom 里,检查必须放在 change x by (speed) 后面,退回要用 ((0) - (speed))。
骑士在半空中还能再跳。
跳跃需要 <(on ground) = (1)>,而且 fall :: custom 在检查之前必须先把 on ground 设成 0。
我站在什么上面?
让骑士一直说出它脚下的那个字母。
- 在砖块上,它说
On: #。 - 在空中,它说
On: .
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,按 space 键跳。
#;每次跳到最高点时,都显示 .。Teacher reveal
tile at (x position) ((y position) - (20)) :: custom
say (join [On: ] (tile))
At the end of the forever, after the fall. Standing, the feet are 12 above the floor’s top, so 20 below the middle is 8 into the brick. Pupils who try 12 get the empty row above it half the time.
冲刺,不留空隙
从墙边退回来时,骑士最多会差 3 个像素才碰到墙。用两倍速度的话,会差 7 个像素。
- 按住 Z,骑士的速度从 4 变成 8。
- 它走到墙前停下时,要刚好贴着墙。不管多快,都没有空隙。
怎么用: 点绿旗,然后在舞台上点一下。按住 ← 或 → 让骑士走,同时按住 Z 冲刺,按 space 键跳。
Teacher reveal
define walk (speed)
change x by (speed)
check box :: custom
if <(hits) > (0)> then
if <(speed) > (0)> then
tile at ((x position) + (8)) (y position) :: custom
set x to (((-240) + ((30) * ((col) - (1)))) - (9))
else
tile at ((x position) - (8)) (y position) :: custom
set x to (((-240) + ((30) * (col))) + (9))
end
end
Cell to edge: the left edge of column c is −240 + 30 × (c − 1). The right corner is 8 from the middle, so the middle goes 9 short of the edge. The main loop sets a speed variable to 8 or 4 with <key (z v) pressed?>.
格子光标
图块工坊 II 的光标是自己算的。用 tile at (x) (y) :: custom 把它重新做一遍,这样骑士和光标都用同一个自制积木,把点变成格子。
- 用你在图块工坊 II 做的 Cursor 角色,或者一个新的 30 × 30 方框,中间是空的。
- 它刚好落在鼠标下面的那个格子上,永远不会卡在两个格子中间。
col、row和tile显示框显示的就是那个格子。
怎么用: 点绿旗,然后把鼠标指针慢慢地在地图上移动。
Teacher reveal
when I receive [play v]
forever
tile at (mouse x) (mouse y) :: custom
go to x: ((-225) + ((30) * ((col) - (1)))) y: ((165) - ((30) * ((row) - (1))))
end
A round trip: point to cell with tile at (x) (y) :: custom, then cell back to centre with go to cell’s formula. The cursor needs its own copy of the My Block — My Blocks belong to one sprite. Drag it onto the cursor in the sprite list to copy it.
巨人骑士
把骑士的大小设成 50。现在它大约 68 个像素高,比两行还高。
- 做两条走廊,每条都有一块砖挂在巨人腰的高度(看下面的地图)。
- 用四个角的话,巨人会直接穿过那块砖。找出为什么。
- 修好它,让巨人被那块砖挡住。
怎么用: 点绿旗开始。别的都不用按:两个巨人往右走,一个用四个角,一个用六个点。
Teacher reveal
define check box
set [hits v] to (0)
corner (-14) (-33) :: custom
corner (14) (-33) :: custom
corner (-14) (33) :: custom
corner (14) (33) :: custom
corner (-14) (0) :: custom
corner (14) (0) :: custom
Corners at ±14 and ±33, plus a middle point on each side. Points 33 apart are never more than 30 from the next. The landing becomes + 34, and the head − 34.
总结 5 分钟
- 列表就是关卡。图是从列表画出来的,骑士读的是列表,不是图。
tile at (x) (y) :: custom把一个点变成一列、一行和一个字母。- 四个角告诉你骑士是不是在砖块里。
- 行号准确地告诉你要落在哪里:那一行顶边的上面 12。
- 网格计算
- 从位置算出格子,或者从格子算出位置。
- 格子
- 地图上的一个方块:一列、一行、一个字母。
- 向下取整(floor)
- 往下取到整数:4.67 变成 4。
- 碰撞区
- 游戏检查的是这个框,不是图片。我们的碰撞区是四个角。
- 对齐(snap)
- 根据网格算出来,把角色刚好放在一条边上。
交作业
保存你的城堡攀登。下节课,城堡会有三层楼、一个通往上一层的出口,还有尖刺。