- Learnt28 min · offset, aim, a speed cap, a fair miss
- Basic25 min · an aiming paddle
- Challenge 1–327 min · hearts, an aimed serve, spin
- Extrabonus · an autopilot
Review — Brick Breaker II 5 min
- The wall was a list. Each letter was a brick, and the letter was its hit points.
- Brick clones only remembered
my colandmy row, and drew what the list said. - The ball moved across and asked
brick at (x) (y) :: custom, then moved up and asked again — so it knew which face it hit.
Quick-fire
In your Brick Breaker II game, the ball starts with vx 4 and vy 5. After a wall, a brick and the paddle, what could vx be?
Reveal the answer
Only 4 or −4. Every bounce flips a sign; nothing ever changes the size. So the ball has one slant for the whole game, and the player cannot aim. Today fixes that.
Today’s Topic 3 min
- The
offset: where the ball lands on the paddle - vx from the offset — aiming
- A speed that grows, and two caps that keep it fair
- A miss that puts the speed back
Learning outcome
By the end of this lesson you will be able to:
- Work out vx from where the ball hits the paddle.
- Make a game harder over time, with a cap that keeps it possible.
- Explain why a game is fair or unfair with a number.
Learnt — aiming with the paddle 28 min
1 · One slant for ever
Flipping only changes the sign. So the ball’s slant never changes. It lands in different places, but it always leaves at the same angle.
How to use this: Click the green flag to start it. Nothing else to press — a robot paddle catches the ball four times with the pen down.
2 · The offset sets vx
Every real paddle game cheats a little. It does not bounce like a mirror. Instead, the spot the ball lands on decides where it goes.
- The
offsetis((x position) - (paddle x)): how far from the middle the ball landed. - The paddle is 88 wide, so the offset runs from −44 (far left) to 44 (far right).
((offset) / (44))turns that into a number from −1 to 1.- Times
speed, it is the new vx. Andvyis justspeed, going up.
if <<touching [Paddle v]?> and <(vy) < (0)>> then
set [offset v] to ((x position) - (paddle x))
set [vx v] to (((offset) / (44)) * (speed))
set [vy v] to (speed)
end
How to use this: Click the green flag to start it. Nothing else to press — three balls land on one paddle.
3 · Faster, but fair
A game should get harder. So every paddle hit adds 0.5 to speed. But the paddle can only move 8 steps a frame. A ball faster than that cannot always be caught — that is not hard, it is unfair.
Two caps keep it fair:
| Cap | Blocks | Why |
|---|---|---|
| Speed cap | if <(speed) < (8)> then | Never faster than the paddle. |
| Angle cap | offset kept between −44 and 44 | vx is never more than speed, so the ball is never flatter than a diagonal. |
if <<touching [Paddle v]?> and <(vy) < (0)>> then
set [offset v] to ((x position) - (paddle x))
if <(offset) > (44)> then
set [offset v] to (44)
end
if <(offset) < (-44)> then
set [offset v] to (-44)
end
set [vx v] to (((offset) / (44)) * (speed))
set [vy v] to (speed)
change [hits v] by (1)
if <(speed) < (8)> then
change [speed v] by (0.5)
end
end
Below, a robot paddle that moves at most 8 a frame plays under a steel roof. The only difference between the two is the speed cap.
How to use this: Click the green flag to start it. Nothing else to press — watch the speed monitor climb.
How to use this: Click the green flag to start it. Nothing else to press — watch the speed monitor stop at 8.
4 · A fair miss
After a miss, the speed goes back to 5. Otherwise the player serves straight into the fastest ball of the game. serve now sends the ball straight up at the current speed.
define serve
repeat (15)
go to x: (paddle x) y: (-128)
end
set [vx v] to (0)
set [vy v] to (speed)
if <(y position) < (-170)> then
change [lives v] by (-1)
if <(lives) = (0)> then
say [Game over!]
stop [this script v]
end
set [speed v] to (5)
serve :: custom
end
How to use this: Click the green flag to start it. Nothing else to press — this demo speeds up fast, so the robot paddle misses.
Basic — an aiming paddle 25 min
Open your Brick Breaker II project. The wall, the bricks and the four My Blocks stay.
Step 1 — three new variables
- Make
speed,offsetandhitsfor all sprites. Tickspeedso it shows. - Change
serveto the straight-up version in Learnt section 4.
Step 2 — the new main loop
when flag clicked
set [lives v] to (3)
set [score v] to (0)
set [speed v] to (5)
serve :: custom
forever
move ball :: custom
walls :: custom
if <<touching [Paddle v]?> and <(vy) < (0)>> then
set [offset v] to ((x position) - (paddle x))
if <(offset) > (44)> then
set [offset v] to (44)
end
if <(offset) < (-44)> then
set [offset v] to (-44)
end
set [vx v] to (((offset) / (44)) * (speed))
set [vy v] to (speed)
change [hits v] by (1)
if <(speed) < (8)> then
change [speed v] by (0.5)
end
end
if <(y position) < (-170)> then
change [lives v] by (-1)
if <(lives) = (0)> then
say [Game over!]
stop [this script v]
end
set [speed v] to (5)
serve :: custom
end
end
How to use this: Click the green flag, then move the mouse left and right over the stage to slide the Paddle.
The ball always goes straight up.
The offset is always 0. Check it is ((x position) - (paddle x)) on the Ball, and that the Paddle still sets paddle x every frame.
The ball shoots off flat and fast.
The two offset caps are missing, or one says 44 where it should say -44.
The speed never stops growing.
The change [speed v] by (0.5) is outside the if <(speed) < (8)> then.
Three hearts
A number in a box is hard to notice mid-game. Show lives as hearts.
- Three hearts sit in the top-left corner.
- Each miss fades one heart away, from the right.
- Hide the
livesmonitor.
How to use this: Click the green flag to start it. Nothing else to press — a robot paddle that dozes off now and then.
Teacher reveal
A Heart sprite with one variable, my life, for this sprite only:
when flag clicked
hide
set [my life v] to (1)
go to x: (-215) y: (160)
repeat (3)
create clone of [myself v]
change x by (30)
change [my life v] by (1)
end
when I start as a clone
show
wait until <(lives) < (my life)>
repeat (8)
change [ghost v] effect by (12)
change y by (2)
end
delete this clone
Each clone keeps the number it was made with: 1, 2 or 3. The ball’s script does not change at all.
Aim the serve
Every serve goes straight up. Let the player aim it with the same sum.
- While the ball waits, ← and → slide it along the paddle.
- It cannot slide off: it stays within 40 of the middle.
- Space serves it, using the paddle’s aiming sum.
How to use this: Click the green flag, then move the mouse to slide the Paddle. Click the stage once, then hold ← or → to slide the ball along it, and press the space bar to serve.
Teacher reveal
define serve
set [offset v] to (0)
repeat until <key (space v) pressed?>
if <<key (right arrow v) pressed?> and <(offset) < (40)>> then
change [offset v] by (2)
end
if <<key (left arrow v) pressed?> and <(offset) > (-40)>> then
change [offset v] by (-2)
end
go to x: ((paddle x) + (offset)) y: (-128)
end
set [vx v] to (((offset) / (44)) * (speed))
set [vy v] to (speed)
The last two blocks are the paddle’s own sum — one rule for every launch.
Spin
In real paddle games, a moving paddle drags the ball with it. Add that.
- The paddle moves with ← and → (8 a frame).
pvis how far the paddle moved this frame.- A hit adds 0.4 × pv to vx.
How to use this: Click the green flag to watch two drops: the paddle still, then sliding right. Then click the stage once and hold ← or → to try it.
Teacher reveal
when flag clicked
go to x: (-30) y: (-150)
set [last x v] to (-30)
forever
if <key (right arrow v) pressed?> then
change x by (8)
end
if <key (left arrow v) pressed?> then
change x by (-8)
end
set [pv v] to ((x position) - (last x))
set [last x v] to (x position)
set [paddle x v] to (x position)
end
And in the paddle hit, straight after vx is set:
change [vx v] by ((pv) * (0.4))
pv is the paddle’s own velocity — the same idea as the ball’s, measured instead of set.
An autopilot
Make a paddle that plays by itself — not by chasing the ball, but by working out where it will land.
- While the ball comes down, the paddle works out where it will reach y −128.
- It counts the side walls on the way.
- It moves there at most 6 a frame, like a player would.
How to use this: Click the green flag to start it. Nothing else to press — the paddle plays on its own.
Teacher reveal
On the Paddle (the Ball also posts ball y every frame):
define predict
set [px v] to (ball x)
set [py v] to (ball y)
set [pvx v] to (vx)
set [pvy v] to (vy)
repeat until <(py) < (-128)>
change [px v] by (pvx)
change [py v] by (pvy)
if <(px) > (232)> then
set [pvx v] to ((0) - (pvx))
end
if <(px) < (-232)> then
set [pvx v] to ((0) - (pvx))
end
end
set [target v] to ((px) + (15))
when flag clicked
go to x: (0) y: (-150)
set [target v] to (0)
forever
if <(vy) < (0)> then
predict :: custom
end
set [gap v] to ((target) - (x position))
if <(gap) > (6)> then
set [gap v] to (6)
end
if <(gap) < (-6)> then
set [gap v] to (-6)
end
change x by (gap)
set [paddle x v] to (x position)
end
Tick “Run without screen refresh” on predict, or the paddle waits a whole flight for its answer. The +15 lands the ball off-centre, so it does not go straight up for ever.
Summary 5 min
- Flipping keeps one slant for ever. The paddle aims by setting vx from the offset.
(((offset) / (44)) * (speed))turns a landing spot into a direction.- The speed grows each hit, and a cap stops it outrunning the paddle.
- A miss puts the speed back to 5, so a serve is never the fastest ball.
- Offset
- How far from the middle something is: here, ball x minus paddle x.
- Cap
- The most a number is allowed to be.
- Fair
- Hard, but always possible for a player who plays well.
Hand in
Save your aiming game. Next lesson is the last of the arc: power-ups, many balls, sparks, levels — and a game you can win.