- Learnt30 min · square → x and y, x and y → square, floor
- Basic25 min · a cursor that snaps to the square under the mouse
- Challenge 1–327 min · red on walls, stay on the grid, arrow keys
- Extrabonus · count the walls next door
Review — Tile Maker I 5 min
- A level is a list of 12 rows. Each row is 16 letters.
(letter (col) of (item (row) of [level v]))reads one tile.- The Tile sprite stamps one costume per square. Each costume is named after its letter.
Quick-fire
Row 11 of our level is #....#E.D......#. What is in column 9?
Reveal the answer
D — the door. Count from the left: # is column 1. Today you will find column 9 on the Stage without counting at all.
Today’s Topic 3 min
- From a square to its x and y — one sum, in a My Block
- From any x and y back to its square, with
([floor v] of (7.6)) - A cursor that snaps to the square under the mouse
- Reading the tile under the mouse
Learning outcome
By the end of this lesson you will be able to:
- Work out the centre of any square from its column and row.
- Work out which square any point is in.
- Build a cursor that shows the square, and the tile, under the mouse.
Learnt — grid maths, both ways 30 min
1 · From a square to its x and y
Last lesson the drawer walked along, adding 30 each time. That only works in order. A game needs to jump straight to any square — so we need a sum.
- Column 1’s centre is at x −225. Each column is 30 further right. So column
cis at x = −225 + 30 × (c − 1). - Row 1’s centre is at y 165. Each row is 30 lower. So row
ris at y = 165 − 30 × (r − 1).
| Column | x | Row | y |
|---|---|---|---|
| 1 | -225 | 1 | 165 |
| 2 | -195 | 2 | 135 |
| 9 | 15 | 7 | -15 |
| 16 | 225 | 12 | -165 |
Put the sum in a My Block with two inputs, c and 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
How to use this: Click the green flag to start it. Nothing else to press — watch the Ladybug hop to five squares and say each x and y.
go to cell (col) (row) :: custom also makes draw level shorter. You do not need tile x and tile y any more:
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 · From a point back to its square
Now the other way. The mouse is at some x. Which column is that?
- Add 240. Now the left edge of the Stage is 0 instead of −240.
- Divide by 30. That says how many squares from the left edge you are — 4.67 squares, say.
- Round down with
([floor v] of (4.67)). 4.67 squares in means 4 whole squares are behind you. - Add 1. You are in the 5th square: column 5.
| x | (x + 240) ÷ 30 | floor | column |
|---|---|---|---|
| -240 | 0 | 0 | 1 |
| -100 | 4.67 | 4 | 5 |
| 0 | 8 | 8 | 9 |
| 110 | 11.67 | 11 | 12 |
| 239 | 15.97 | 15 | 16 |
Rows count down from the top, so y uses 180 − y instead: row = 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
How to use this: Click the green flag to start it. Nothing else to press — watch the two readouts as the Ladybug glides.
3 · A cursor that snaps
Put both sums together. Work out the square under the mouse, then go to the centre of that square. The cursor jumps from square to square instead of sliding.
And once you know the square, you can read its letter — the same (letter (mouse col) of (item (mouse row) of [level v])) as last lesson. That is the Basic.
Basic — the snapping cursor 25 min
Open your Tile Maker project from last lesson.
Step 1 — go to cell
- In the Tile sprite, make a Block called
go to cellwith two number inputs,candr. - Build the stack from Learnt part 1.
- Change
draw levelto use it. Click the green flag: the level should look exactly the same.
Step 2 — the Cursor sprite
- Paint a new sprite called
Cursor: a 30 × 30 frame, bright yellow, with a see-through middle. Centre it on the cross. - Drag the
go to celldefine hat onto the Cursor in the sprite list. That copies the My Block to it. - Make the variables
mouse col,mouse rowandtile, and tick all three so they show on the Stage.
Step 3 — snap and read
- In the Cursor, add
when flag clicked,go to [front v] layerand aforever. - Inside it, set
mouse colandmouse rowwith the floor sums. - Then
go to cell (mouse col) (mouse row) :: custom. - Then set
tileto the letter in that square. - Click the green flag and move the mouse over the level.
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
How to use this: Click the green flag, then move the mouse over the level. Watch the cursor and the three readouts.
The cursor sits half a square off.
The frame is not centred on the cross in the paint editor. Drag it until the cross is in the middle of the frame.
The cursor slides instead of jumping.
There is a go to [mouse-pointer v] left in the loop. Only go to cell should move the Cursor.
Up and down are the wrong way round.
The row sum must be ((180) - (mouse y)), not ((mouse y) + (180)). Rows count down from the top.
tile goes blank at the very edge of the Stage.
That is real, and it is Challenge 2.
Red on walls
Warn the player before they place something on a wall.
- Over a brick, the cursor is red.
- Anywhere else, it is yellow again.
How to use this: Click the green flag, then move the mouse over the level. The cursor turns red over a brick.
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
Stay on the grid
Push the mouse right against the right-hand edge. mouse col says 17 — but there is no column 17. The bottom edge gives row 13. Fix it.
- The cursor never leaves the 16 × 12 grid, wherever the mouse goes.
- In the bottom-right corner, the readouts say 16 and 12.
How to use this: Click the green flag, then move the mouse right into the bottom-right corner of the stage. The readouts stop at 16 and 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.
A cursor on the arrow keys
Make a second version with no mouse at all.
- The cursor starts on column 9, row 7.
- Each arrow key moves it exactly one square.
- It never leaves the grid.
- Every time it moves, it says the letter it landed on.
How to use this: Click the green flag, then click the stage once and press ← → ↑ or ↓ to move the Cursor one square. It says the letter it lands on.
# in each one.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.
Count the walls next door
A game often asks “what is next to me?” — before a step, before a jump. Make the cursor answer it.
- A variable
wallscounts the bricks in the four squares touching the cursor’s square: above, below, left and right. - It updates as the mouse moves.
How to use this: Click the green flag, then move the mouse over the level. Watch the walls readout.
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.
Summary 5 min
- Square → centre: x = −225 + 30 × (c − 1), y = 165 − 30 × (r − 1). It lives in
go to cell. - Point → square: column = floor((x + 240) ÷ 30) + 1, row = floor((180 − y) ÷ 30) + 1.
- Floor rounds down. Round would be wrong half the time.
- The right and bottom edges of the Stage need holding in.
- Grid
- Rows and columns of equal squares.
- Floor
- Rounds a number down to a whole number: floor of 4.67 is 4.
- Snap
- Jump to the nearest square instead of sliding smoothly.
- Neighbour
- A square that touches this one.
Hand in
Save your project. Next lesson the cursor gets a paintbrush: click a square and you change the level.