- 学一学30 分钟 · 方格 → x 和 y,x 和 y → 方格,floor
- 基础任务25 分钟 · 一个会吸附到鼠标下面那个方格的光标
- 挑战 1–327 分钟 · 在墙上变红、不离开网格、用方向键
- 加分任务加分 · 数一数旁边的墙
复习:图块工坊 I 5 分钟
- 关卡是一个有 12 行的列表。每行有 16 个字母。
(letter (col) of (item (row) of [level v]))读出一个图块。- Tile 角色在每个方格上盖一个造型。每个造型都用它的字母来命名。
快问快答
我们关卡的第 11 行是 #....#E.D......#。第 9 列是什么?
看答案
D:门。从左边数:# 是第 1 列。今天,你不用数,就能在舞台上找到第 9 列。
今天的主题 3 分钟
- 从方格算出它的 x 和 y:一个算式,放在一块自制积木里
- 用
([floor v] of (7.6)),从任何 x 和 y 算回它所在的方格 - 一个会吸附到鼠标下面那个方格的光标
- 读出鼠标下面的图块
学习目标
上完这节课,你能够:
- 从任何方格的列和行,算出它的中心。
- 算出任何一个点在哪个方格里。
- 做一个光标,显示鼠标下面的方格和图块。
学一学:网格计算,两个方向 30 分钟
1 · 从方格到它的 x 和 y
上节课,绘制器一路走过去,每次加 30。这只能按顺序来。游戏需要直接跳到任何一个方格,所以我们需要一个算式。
- 第 1 列的中心在 x −225。每一列往右多 30。所以第
c列在 x = −225 + 30 × (c − 1)。 - 第 1 行的中心在 y 165。每一行往下低 30。所以第
r行在 y = 165 − 30 × (r − 1)。
| 列 | x | 行 | y |
|---|---|---|---|
| 1 | -225 | 1 | 165 |
| 2 | -195 | 2 | 135 |
| 9 | 15 | 7 | -15 |
| 16 | 225 | 12 | -165 |
把算式放进一块有两个输入 c 和 r 的自制积木:
define go to cell (c) (r)
go to x: ((-225) + ((30) * ((c) - (1)))) y: ((165) - ((30) * ((r) - (1))))
when flag clicked
go to [front v] layer
go to cell (9) (7) :: custom
say (join (join [Column 9, row 7: x ] (x position)) (join [, y ] (y position))) for (1.8) seconds
怎么用: 点绿旗开始。别的都不用按。看 Ladybug 跳到五个方格上,说出每个方格的 x 和 y。
go to cell (col) (row) :: custom 也能让 draw level 变短。你再也不需要 tile x 和 tile y 了:
define draw level
erase all
show
set [row v] to (1)
repeat (12)
set [col v] to (1)
repeat (16)
go to cell (col) (row) :: custom
switch costume to (letter (col) of (item (row) of [level v]))
stamp
change [col v] by (1)
end
change [row v] by (1)
end
hide
2 · 从一个点算回它的方格
现在反过来。鼠标在某个 x 上。那是第几列?
- 加 240。现在舞台的左边缘是 0,不是 −240。
- 除以 30。这告诉你离左边缘有几个方格,比如说 4.67 个方格。
- 用
([floor v] of (4.67))向下取整。走进去 4.67 个方格,表示你身后有 4 个完整的方格。 - 加 1。你在第 5 个方格里:第 5 列。
| x | (x + 240) ÷ 30 | floor | 列 |
|---|---|---|---|
| -240 | 0 | 0 | 1 |
| -100 | 4.67 | 4 | 5 |
| 0 | 8 | 8 | 9 |
| 110 | 11.67 | 11 | 12 |
| 239 | 15.97 | 15 | 16 |
行是从上往下数的,所以 y 要用 180 − y:行 = floor((180 − y) ÷ 30) + 1。
when flag clicked
go to [front v] layer
go to x: (-200) y: (140)
glide (2) secs to x: (110) y: (80)
glide (2) secs to x: (160) y: (-120)
glide (2) secs to x: (-100) y: (-60)
glide (2) secs to x: (0) y: (0)
when flag clicked
forever
set [cell col v] to (([floor v] of (((x position) + (240)) / (30))) + (1))
set [cell row v] to (([floor v] of (((180) - (y position)) / (30))) + (1))
end
怎么用: 点绿旗开始。别的都不用按。Ladybug 滑动的时候,看两个显示框。
3 · 会吸附的光标
把两个算式放在一起。先算出鼠标下面是哪个方格,再走到这个方格的中心。光标就会从一个方格跳到另一个方格,而不是滑过去。
知道了方格,你就能读出它的字母,用的还是上节课那块 (letter (mouse col) of (item (mouse row) of [level v]))。这就是基础任务。
基础任务:会吸附的光标 25 分钟
打开你上节课的图块工坊作品。
第 1 步:go to cell
- 在 Tile 角色里,做一块叫
go to cell的积木,有两个数字输入c和r。 - 搭出“学一学”第 1 部分的那组积木。
- 把
draw level改成用它。点绿旗:关卡看起来应该一模一样。
第 2 步:Cursor 角色
- 画一个新角色,叫
Cursor:一个 30 × 30 的亮黄色方框,中间是透明的。让它的中心对准十字。 - 把
go to cell的 define 帽子积木拖到角色列表里的 Cursor 上。这样就把这块自制积木复制给它了。 - 建变量
mouse col、mouse row和tile,三个都勾上,让它们显示在舞台上。
第 3 步:吸附和读取
- 在 Cursor 里,加上
when flag clicked、go to [front v] layer和一个forever。 - 在它里面,用 floor 算式设定
mouse col和mouse row。 - 然后
go to cell (mouse col) (mouse row) :: custom。 - 然后把
tile设为那个方格里的字母。 - 点绿旗,把鼠标移到关卡上。
when flag clicked
go to [front v] layer
forever
set [mouse col v] to (([floor v] of (((mouse x) + (240)) / (30))) + (1))
set [mouse row v] to (([floor v] of (((180) - (mouse y)) / (30))) + (1))
go to cell (mouse col) (mouse row) :: custom
set [tile v] to (letter (mouse col) of (item (mouse row) of [level v]))
end
怎么用: 点绿旗,然后把鼠标移到关卡上。看光标和三个显示框。
光标偏了半个方格。
方框在绘图编辑器里没有对准十字的中心。拖动它,直到十字在方框正中间。
光标是滑过去的,不是跳过去的。
循环里还留着一块 go to [mouse-pointer v]。只有 go to cell 能移动 Cursor。
上下反了。
行的算式必须是 ((180) - (mouse y)),不是 ((mouse y) + (180))。行是从上往下数的。
到了舞台的最边上,tile 变成空的。
这是真的问题,它就是挑战 2。
在墙上变红
在玩家把东西放到墙上以前,先提醒他。
- 在砖块上,光标是红色的。
- 在别的地方,它又变回黄色。
怎么用: 点绿旗,然后把鼠标移到关卡上。光标在砖块上会变红。
Teacher reveal
A second costume, blocked, then at the end of the forever:
if <(tile) = [#]> then
switch costume to [blocked v]
else
switch costume to [cursor v]
end
不离开网格
把鼠标一直推到舞台的最右边。mouse col 显示 17,可是根本没有第 17 列。最下边会得到第 13 行。把它修好。
- 不管鼠标到哪里,光标都不离开 16 × 12 的网格。
- 在右下角,显示框显示 16 和 12。
怎么用: 点绿旗,然后把鼠标移到舞台的右下角。显示框停在 16 和 12。
Teacher reveal
After the two set blocks:
if <(mouse col) > (16)> then
set [mouse col v] to (16)
end
if <(mouse row) > (12)> then
set [mouse row v] to (12)
end
Only two checks are needed. The mouse x is never below −240, so the column is never below 1; the mouse y is never above 180, so the row is never below 1. Ask pupils why — it is the best question in the lesson.
用方向键控制的光标
做第二个版本,完全不用鼠标。
- 光标从第 9 列第 7 行开始。
- 每按一次方向键,它正好移动一个方格。
- 它永远不离开网格。
- 每次移动,它都说出自己落在哪个字母上。
怎么用: 点绿旗,然后点一下舞台,按 ← → ↑ 或 ↓,让 Cursor 移动一个方格。它会说出自己落在哪个字母上。
#。Teacher reveal
define show cursor
go to cell (cursor col) (cursor row) :: custom
say (letter (cursor col) of (item (cursor row) of [level v]))
when flag clicked
go to [front v] layer
set [cursor col v] to (9)
set [cursor row v] to (7)
show cursor :: custom
when [right arrow v] key pressed
if <(cursor col) < (16)> then
change [cursor col v] by (1)
end
show cursor :: custom
when [up arrow v] key pressed
if <(cursor row) > (1)> then
change [cursor row v] by (-1)
end
show cursor :: custom
Plus left and down, the same way round. Up is −1: rows count down from the top.
数一数旁边的墙
游戏常常要问“我旁边有什么?”:走一步以前、跳起来以前。让光标来回答这个问题。
- 变量
walls数出和光标所在方格挨着的四个方格里有几块砖:上、下、左、右。 - 鼠标移动时,它会跟着更新。
怎么用: 点绿旗,然后把鼠标移到关卡上。看 walls 显示框。
Teacher reveal
define count walls
set [walls v] to (0)
if <(letter ((mouse col) + (1)) of (item (mouse row) of [level v])) = [#]> then
change [walls v] by (1)
end
if <(letter ((mouse col) - (1)) of (item (mouse row) of [level v])) = [#]> then
change [walls v] by (1)
end
if <(letter (mouse col) of (item ((mouse row) - (1)) of [level v])) = [#]> then
change [walls v] by (1)
end
if <(letter (mouse col) of (item ((mouse row) + (1)) of [level v])) = [#]> then
change [walls v] by (1)
end
Called at the end of the cursor’s forever. Off the edge of the map, the letter is empty, so it does not count as a wall.
总结 5 分钟
- 方格 → 中心:x = −225 + 30 × (c − 1),y = 165 − 30 × (r − 1)。它放在
go to cell里。 - 点 → 方格:列 = floor((x + 240) ÷ 30) + 1,行 = floor((180 − y) ÷ 30) + 1。
- floor 是向下取整。用 round 的话,有一半的时候是错的。
- 舞台的右边缘和下边缘要拦住。
- 网格
- 由一样大的方格排成的行和列。
- 向下取整(floor)
- 把一个数往下取成整数:4.67 向下取整是 4。
- 吸附
- 跳到最近的方格上,而不是平平地滑过去。
- 相邻方格
- 和这个方格挨着的方格。
交作业
保存你的作品。下节课,光标会有一支画刷:点一个方格,你就能改变关卡。