Project: enter a hard game

Light Cycles, Rescue Line or Capture the Flag: how to practise one properly, and the line and gripper rehearsal.

10.6CompetingRobot club25 min

Do this lesson in the simulator

Everything in this course now points at one of three games. Pick one, practise against the bots until you beat them, then take it to a room with other people.

The three

Light Cycles, free for all, after Module 6. Trails everywhere, your own included. Won by turning early and keeping to the outside, and by never standing still. Lesson 10.4 is the rehearsal.

Rescue Line, on your own, after Module 7. Follow a broken line round a block, then pick up the casualties with the gripper and carry them to safety. Two skills you know separately, used at once, which is the whole difficulty.

Capture the Flag, teams, after Module 9. Take the other team's flag home while they try the same. Roles, radio, and knowing when to defend. Lesson 10.5 is the rehearsal.

How to get good at any of them

  1. Read the game page. It lists exactly what info() gives you, and describes each built-in bot.
  2. Beat one bot. Not all of them. Pick the one that beats you most and work out what it does differently.
  3. Print, race, read. One line per loop with your state and the numbers you decided on. The log is where the answer is.
  4. Change one thing at a time. Two changes and a better result tells you nothing.
  5. Then play people. Rooms are on the competitions site: one screen hosts, everyone else joins with the code.
Getting good at a game: read the game page, then race, read the log and change one thing, round and round until you beat the bot, then play peopleraceagainst the botsread the logwhat did it do?change one thingonly oneagain, until you beat the botchange two things at once and you cannot tell which one helpedread the game pagethen play peoplebeaten it?
Steps 3 and 4 of the list go round and round. Each trip is one race, one look at the log and one change. You only leave the loop, for a room with other people, once the bot is beaten.

The rehearsal

This is Rescue Line's hardest moment on its own: the line, a ball to carry, and a zone to reach.

from bugbot import *
connect()
set_cv('line')

while True:
    seen = line()
    if seen:
        # line() gives [cx, angle]: steer by how far the line is from the middle of the picture
        cx, angle = seen
        drive(45, 0, (cx - 160) * 0.35)
    else:
        drive(25, 0, 40)
    wait(0.1)

This first go only follows the line. It passes all four checkpoints, which is 60 points, and never picks anybody up, so it ties with Liner Lou and loses to Map Reader Mia, who gets one casualty to safety for 100. The task below adds the carrying.

The Rescue Line course from above: a broken line round a block, four checkpoints, the evacuation zone with two casualties in it, the safe corner, and the path the rehearsal program took1234the lineyour pathcheckpointblockcasualtyevacuationzonesafe cornerstart
The rehearsal's first lap in one race. It bumps the block, finds the line again past it, passes all 4 checkpoints and drives through the evacuation zone, but it never stops for the two casualties (red), so it scores 60. At the end of the line it turns and follows the line back.

Task: carry it home

Follow the black line to the red ball, pick it up with the gripper, and carry it into the green zone at the end without dropping it. The line bends to the right, so driving straight up the mat misses the ball by 15 cm: the line is the only way to it.

from bugbot import *
connect()
set_cv('line')

gripper("open")

# follow the line first: steer by where it is and where it is going,
# and slide sideways onto it if you drift
while position()[1] < 40:
    seen = line()
    if seen:
        cx, angle = seen
        drive(35, (cx - 160) * 0.15, (cx - 160) * 0.15 + angle * 1.5)
    else:
        forward(20)
    wait(0.1)

stop()

Where next

That is Robot club, from your first line of Python to a hard game. Three things to do now:

  • Play in a room. The competitions site takes a class, a club or four friends on one screen.
  • Go deeper. The A level course covers the computer science under all of this: algorithms, data structures and how the machine works.
  • Put it on a real robot. Everything you have written runs on a BugBot without a single change.