- Learnt28 min · walk, then pull the world back; screen x and world x
- Basic24 min · the scroller
- Challenge 1–327 min · respawns, a crab, a checkpoint
- Extrabonus · a camera that looks ahead
Review — move the world 5 min
scroll xsays how far along the world the Stage is looking.- Everything sets
set x to ((my x) - (scroll x)). - Far-away sprites hide, or they wait at the edge of the Stage.
Quick-fire
Last lesson the arrow keys changed scroll x instead of Pico’s x. Jump Quest’s Pico has walls. What goes wrong if you try that with it?
Reveal the answer
The walls stop working. Pico’s walk block moves Pico. It asks if Pico hit green, and undoes the move. If Pico never moves, there is nothing to ask about. Today Pico goes back to walking for real. Then the world catches up afterwards.
Today’s Topic 3 min
- Walk for real, then pull the world back
- Screen x and world x:
((scroll x) + (x position)) - Respawn points, enemies and checkpoints that live in the world
Learning outcome
By the end of this lesson you will be able to:
- Put a Jump Quest character into a scrolling level. You will not rewrite its falling and jumping code.
- Tell a screen position from a world position. Use the right one.
- Make a respawn, an enemy and a checkpoint work in a world bigger than the Stage.
Learnt — a camera that follows 28 min
1 · Walk for real, then pull the world back
Keep every block of Pico’s Jump Quest loop: walk, gravity, land, the jump. They move Pico across the Stage as they always did. The walls and floors work because the world stands still while they run. Then, at the very end of the tick, add one My Block:
define scroll the world
change [scroll x v] by (x position)
set x to (0)
if <(scroll x) < (0)> then
set x to (scroll x)
set [scroll x v] to (0)
end
if <(scroll x) > (1440)> then
set x to ((scroll x) - (1440))
set [scroll x v] to (1440)
end
broadcast [scroll v]
ifs are last lesson’s ends. At an end, the world stops and Pico keeps what is left.How to use this: Click the green flag. Watch Pico cross all four screens of level 1 on its own.
2 · Two kinds of x
Pico’s x position is now nearly always 0. So it tells you nothing about where Pico is in the level. That is its screen x. Where Pico really is is its world x. It is the scroll plus the screen x:
set [world x v] to ((scroll x) + (x position))
How to use this: Click the green flag. Watch the scroll x and world x monitors while Pico walks and hops.
Basic — the scroller 24 min
Combine your road from last lesson with your Pico from Jump Quest.
Step 1 — a level worth scrolling
- Repaint the four Ground costumes as a level. Add gaps with water in them, steps 50 up and a crate. Name them
L1 screen 1toL1 screen 4. - Keep the ground and steps one green. Keep the water one blue. Do it just as in Jump Quest.
- Change the costume
jointo(join [L1 screen ] (screen)).
Step 2 — bring Pico across
- Drag your Jump Quest Pico onto this project’s sprite list, or export and import it.
- Its
land,walkand hitbox come with it. Do not change them. - Delete last lesson’s road-walking Pico.
Step 3 — pull the world back
- Make the
scroll the worldblock from Learnt part 1. - Put it as the last block inside Pico’s forever loop.
- Start Pico at x −180 with
scroll xat 0.
Step 4 — water and the flag
- When Pico touches the water: go to x −180, set
y velocityto 0, setscroll xto 0, and broadcast scroll. - Put the finish flag at world x 1600. Place it the same way as the screens.
when flag clicked
go to x: (-180) y: (-104)
set [scroll x v] to (0)
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
change [y velocity v] by (-1)
change y by (y velocity)
land :: custom
if <<key (space v) pressed?> and <(on ground) = (1)>> then
set [y velocity v] to (12)
end
scroll the world :: custom
if <touching color [#2F80ED]?> then
go to x: (-180) y: (-104)
set [y velocity v] to (0)
set [scroll x v] to (0)
broadcast [scroll v]
end
if <touching (Flag v)?> then
say [Level complete!]
end
end
How to use this: Click the green flag. Use the arrows to walk and space to jump. Cross all four screens to the flag.
Pico walks through steps and crates.
scroll the world is before walk or land. Move it to the very end of the loop.
Pico drifts off towards the edge of the Stage.
The set x to (0) is missing. So the world scrolls, but Pico is never put back in the middle.
The world goes the wrong way.
Use change [scroll x] by (x position). It is plus, not minus. When Pico is right of the middle, the world must slide left. That means a bigger scroll x.
After falling in the water, Pico comes back somewhere strange.
That is Challenge 1.
The respawn that forgot the camera
A respawn puts Pico back after a fall. Fall into the first pit twice. The first respawn only moves Pico. The second also resets scroll x. Report Pico’s world x each time.
- Both respawns go to x −180.
- Only one sets
scroll xto 0. - Work out Pico’s world x after each.
How to use this: Click the green flag. Pico walks into the first pit twice. It reports where it came back.
Teacher note
“Go to x −180” is a screen position: it only means “the start” while scroll x is 0. This is the lesson’s central idea turning up as a bug, and it is worth letting the class find it in their own scroller before showing them this.
A crab in the world
Add a crab that walks up and down one stretch of ground. It lives in the world, not on the Stage.
- The crab keeps its own
my x. It changes that to walk. - It places itself with
set x to ((my x) - (scroll x)). It hides when it is off the Stage. - Try a second crab that just changes its x. Watch the difference.
How to use this: Click the green flag. Pico walks right. Watch both crabs.
Teacher reveal — the patrol, in world coordinates
forever
repeat (20)
change [my x v] by (-2)
set x to ((my x) - (scroll x))
end
repeat (20)
change [my x v] by (2)
set x to ((my x) - (scroll x))
end
end
Cheese Maze’s patrol, with one change: the crab moves its my x, and works out its Stage x from that every step. Anything that lives in the world has to.
A checkpoint in the world
Put a checkpoint flag at world x 540. Once Pico has touched it, falling in the water brings Pico back there. The camera comes back there too.
- A
respawn xvariable. It is −180 at the start. The checkpoint sets it to 540. - The water sets
scroll xtorespawn x. It puts Pico at x 0 and pulls the world.
How to use this: Click the green flag. Pico reaches the checkpoint, then walks back into the pit behind it.
Teacher note
Notice the respawn is now one number, a world x, and the camera is worked out from it. That is the right way round: store where things are in the world, and let the Stage follow.
A camera that looks ahead
In most side-scrolling games the hero is not in the middle. They are a little to the left. Then you can see more of what is coming. Keep Pico at x −100 instead of 0.
- Change
scroll the worldso it aims Pico at −100. - The ends of the level still work.
- Play a level both ways and decide which is fairer.
How to use this: Click the green flag. Watch where Pico sits on the Stage as it walks.
Teacher note
The best follow-up question: should the camera look ahead when Pico walks left, too? Answering it properly needs the direction Pico faces — and the pupils who get there have invented the camera most commercial platformers use.
Summary 5 min
- Pico walks for real. Then the world is pulled back by the same amount.
scroll the worldgoes last in the tick, after every touch check.- Screen x is where Pico is on the Stage. World x is
((scroll x) + (x position)). - Respawns, enemies and checkpoints are all world positions.
- Camera
- The part of the world the Stage is showing. Here,
scroll xsets it. - Screen x
- A position on the Stage, from −240 to 240.
- World x
- A position in the whole level.
- Respawn
- Coming back after a fall. In a scrolling game, it moves the camera too.
Hand in
Save your scroller. Next lesson it gets a second level. It has its own screens and its own sky.