- Learnt28 min · waypoints, braking, lights, position, a finish
- Basic25 min · race the rival
- Challenge 1–327 min · a skill level, a jump start, a rubber band
- Extrabonus · race your own ghost
Review — Kart Racer III 5 min
- The gates were two lists,
gate xandgate y. - One Gate sprite stood on the next gate, waited for the kart, then hopped on.
- A lap time was
timer − lap start, andbestsurvived the green flag.
Quick-fire
The Gate always stood exactly where the kart had to go next. If a kart had no driver, how could that same trick help it?
Reveal the answer
It is a target to aim at. A kart that knows where its next point is can point towards it. Today a hidden sprite called the Pin does for the rival exactly what the Gate did for you.
Today’s Topic 3 min
- A rival that uses your own drive block — only the driver is a script
- Waypoints in two lists, and a hidden
Pinon the next one - Slowing down for corners, from a third list:
way speed - Start lights, a race position and a finish card
Learning outcome
By the end of this lesson you will be able to:
- Write a script that drives a sprite by choosing its inputs.
- Use a list to tell a computer player how to behave in each place.
- Work out who is winning from two numbers.
Learnt — a rival kart 28 min
1 · Same kart, different driver
Duplicate your kart and call it Rival. It keeps the whole drive (gas) (steer) :: custom block — same push, same friction, same steering. A computer player that cheats on speed is no fun to beat.
Only the key blocks go. In their place, a script decides gas and steer every tick.
2 · Waypoints and the Pin
The rival needs to know where to go. That is eight points, one at each corner of the track, in two lists: way x and way y. The last point is the finish line.
A hidden sprite called Pin stands on the rival’s next point. It is the Gate trick again:
when flag clicked
hide
set [rival wp v] to (1)
set [rival laps v] to (0)
forever
go to x: (item (rival wp) of [way x v]) y: (item (rival wp) of [way y v])
wait until <(distance to [Rival v]) < (60)>
change [rival wp v] by (1)
if <(rival wp) > (length of [way x v])> then
set [rival wp v] to (1)
change [rival laps v] by (1)
end
end
point towards and distance to still find it.3 · Steering towards the Pin
The rival needs a steer, not a new direction. So it peeks: point at the Pin, remember that direction, then turn back. The gap between the two, wrapped like the slip in Kart Racer II, says which way to steer.
set [old v] to (direction)
point towards [Pin v]
set [want v] to (direction)
point in direction (old)
set [turn v] to (((((want) - (old)) + (540)) mod (360)) - (180))
set [steer v] to (0)
if <(turn) > (3)> then
set [steer v] to (1)
end
if <(turn) < (-3)> then
set [steer v] to (-1)
end
4 · Slowing for corners
Flat out into every corner, the rival runs wide onto the grass. So each corner gets a speed limit, in a third list, way speed: 4.5, 5, 4, 4.5, 4.5, 5, 4.5 — and 10 for the finish straight.
set [limit v] to (item (rival wp) of [way speed v])
set [gas v] to (1)
if <<(distance to [Pin v]) < (100)> and <(rival speed) > (limit)>> then
set [gas v] to (-1)
end
drive (gas) (steer) :: custom
How to use this: Click the green flag to start it. Nothing else to press — the rival drives two laps on its own. The Pin is shown here.
How to use this: Click the green flag to start it. Nothing else to press — the same rival with its brakes taken away.
5 · Start lights
A Lights sprite with five costumes (off, 1, 2, 3, go) and one variable, racing. Both karts start with wait until <(racing) = (1)>.
when flag clicked
show
set [racing v] to (0)
set [light v] to (0)
switch costume to [lights-off v]
wait (0.6) seconds
repeat (3)
change [light v] by (1)
switch costume to (join [lights-] (light))
wait (0.8) seconds
end
switch costume to [lights-go v]
set [racing v] to (1)
say [GO!] for (1) seconds
hide
join builds the costume’s name: lights-1, lights-2, lights-3.How to use this: Click the green flag to start it. Nothing else to press — watch the lights, then both karts go.
6 · Who is winning?
Count gates passed. You pass 4 a lap. The rival passes 8 waypoints a lap — and every second waypoint is a gate, so halve it.
set [my gates v] to (((laps) * (4)) + ((next gate) - (1)))
set [rival gates v] to ([floor v] of ((((rival laps) * (8)) + ((rival wp) - (1))) / (2)))
if <(my gates) > (rival gates)> then
set [position v] to [1st]
end
if <(my gates) < (rival gates)> then
set [position v] to [2nd]
end
7 · The finish
Whoever finishes lap 3 first sets winner — but only if it is still empty. Then a Card sprite shows you-win or you-lose.
if <(laps) = (3)> then
if <(winner) = []> then
set [winner v] to [you]
end
broadcast (finish v)
end
rival laps and [rival].How to use this: Click the green flag to start it. Nothing else to press — a fast script drives the red kart against the rival.
How to use this: Click the green flag to start it. Nothing else to press — a careful script drives the red kart, braking early.
Basic — race the rival 25 min
Open your race from Kart Racer III.
Step 1 — the rival
- Duplicate the Kart. Rename it
Rivaland switch its costume to car-blue.svg. - Give it its own
rival speed(For all sprites, so you can watch it), and use it everywhere in its drive block instead ofspeed. - Delete its key script. Put it on the lower grid box: x −37, y 113.
Step 2 — the lists and the Pin
- Make three lists:
way x= 170, 185, 120, 10, −100, −185, −170, 0;way y= 125, −10, −125, −60, −125, −40, 125, 125;way speed= 4.5, 5, 4, 4.5, 4.5, 5, 4.5, 10. - Add a small
Pinsprite (any shape) and build its script from Learnt section 2.
Step 3 — the driver
On the Rival: wait until <(racing) = (1)>, then a repeat until <not <(winner) = []>> holding sections 3 and 4.
Step 4 — lights, position, finish
- Add the Lights sprite from section 5.
- In your Kart:
wait until <(racing) = (1)>before its loop, and the position blocks from section 6 inside it. - Add the finish blocks from section 7 to the Gate and the Pin, and a Card sprite.
How to use this: Click the green flag, then click the stage once. Wait for green, then hold ↑ to go, ← and → to steer. Three laps.
The rival drives in circles round one corner.
The Pin never hops on. Check its wait until <(distance to [Rival v]) < (60)> asks (distance to [Rival v]) — not the Kart.
The rival spins left and right very fast.
The point in direction (old) is missing, so the rival really turns to the Pin every tick and then steers on top of it.
The card always says I won.
The Pin’s finish blocks are missing, or they set winner to you.
Pick how good the rival is
New players need an easy rival. Fast players need a hard one.
- A
skillvariable: 0.8 easy, 1 normal, 1.1 hard. - While the lights are red, keys 1, 2 and 3 choose it.
- The rival’s corner limits are multiplied by
skill.
How to use this: Click the green flag to start it. Nothing else to press — an easy rival (skill 0.8) drives two laps.
Teacher reveal
set [limit v] to ((item (rival wp) of [way speed v]) * (skill))
And three scripts:
when [1 v] key pressed
if <(racing) = (0)> then
set [skill v] to (0.8)
end
A jump start
Pressing ↑ before green should cost you.
- Hold ↑ while any light is red: that is a jump start.
- On green, the kart says “Jump start! 1 second penalty.” and waits one second.
- The rival leaves on time.
How to use this: Click the green flag to start it. Nothing else to press — the script presses the gas on the second red light.
Teacher reveal
set [penalty v] to (0)
repeat until <(racing) = (1)>
if <key (up arrow v) pressed?> then
set [penalty v] to (1)
end
end
if <(penalty) = (1)> then
say [Jump start! 1 second penalty.] for (1) seconds
end
In place of the kart’s wait until <(racing) = (1)>. The say … for 1 seconds is the penalty: the script cannot go on until it is finished.
A rubber band
A race you win by half a lap is boring. So is one you lose by half a lap.
- While you are 1st, the rival’s skill is 1.15.
- While it is ahead, its skill drops to 0.9.
- The race should stay close all the way.
How to use this: Click the green flag to start it. Nothing else to press — the fast script races a rival with a rubber band.
Teacher reveal
In the rival’s loop, before it works out the limit:
if <(position) = [1st]> then
set [skill v] to (1.15)
else
set [skill v] to (0.9)
end
Talk about whether this is fair. Most racing games do it — and most players never notice.
Race your own ghost
The best rival is you, from one lap ago.
- During lap 1, record where the kart is every tick, in three lists.
- When lap 1 ends, a see-through Ghost kart replays it.
- Lap 2 is you against your own lap 1.
How to use this: Click the green flag to start it. Nothing else to press — a fast first lap is recorded, then a careful second lap races it.
Teacher reveal
if <(laps) = (0)> then
add (x position) to [ghost x v]
add (y position) to [ghost y v]
add (direction) to [ghost dir v]
end
In the kart’s loop. The Gate broadcasts ghost go when laps becomes 1. The Ghost:
when I receive [ghost go v]
show
set [i v] to (1)
repeat (length of [ghost x v])
go to x: (item (i) of [ghost x v]) y: (item (i) of [ghost y v])
point in direction (item (i) of [ghost dir v])
change [i v] by (1)
end
hide
Summary 5 min
- The rival has your kart. A script chooses its gas and steer.
- A hidden Pin on the next waypoint gives it something to point towards.
- A list of corner speeds tells it when to brake — and braking made it faster.
- Lights, a position from gates passed, and the first to three laps wins.
- Waypoint
- A point a computer player drives towards.
- AI
- Here: a script that plays the game by choosing the same inputs a person would.
- Rubber band
- Making the computer player easier when it is ahead and harder when it is behind.
Hand in
Save your finished Kart Racer — it is a whole game now. Next lesson starts something completely different: a quiz where every question is one line of text.