The answersDownload the PDF
Worksheet

10.1 A program in a game

Competing · Robot club · about 14 min

BugBotLab
NameClassDate

What this lesson is about

info(), your own log, and the loop every game program is built from.

Questions 6 marks in all

  1. [1 mark]A student copies the Sprint and Stop program into the task. It stops with NameError: name 'info' is not defined. Why?

    1. Ainfo() only exists in a game, and in a task no game is telling you anything
    2. Binfo needs importing separately
    3. CThe stop box is called something else in tasks
    4. DTasks do not allow while loops
  2. [1 mark]Which of these are true of a program in a game?

    Tick every answer that is true.

    1. AOther robots are on the mat
    2. BThe game tells you things through info()
    3. CEveryone in the game can read your print() lines
    4. DThere is no task to pass, just a result
    5. EThe program keeps running until the game ends
  3. [1 mark]Put the four parts of a game loop in order.

    Number the lines 1 to 4 to put them in the right order.

    1. Look: read the sensors and what the game says
    2. Wait a moment
    3. Decide
    4. Move
  4. [1 mark]What does this program print?

    finish_y = 55
    for y in [10, 40, 52]:
        gap = finish_y - y
        if gap > 25:
            print(gap, "forward(90)")
        elif gap > 4:
            print(gap, "forward(30)")
        else:
            print(gap, "stop")
  5. [1 mark]In the game loop, why does each pass end with wait(0.1)?

    1. ATo let a moment pass, so the robot keeps hearing from you
    2. BTo make the robot slower than the bots
    3. CBecause info() only updates once a second
    4. DThe game ends if you do not wait
  6. [1 mark]In the task the target is 55 cm up the mat and the red strip starts 5 cm beyond it. position()[1] reads 58. How many cm are you from the red strip?

The task: stop on the line

Same idea, on your own: drive 55 cm up the mat and stop within 5 cm of that point, without crossing into the red strip five centimetres beyond it. Remember position() counts from where the robot started.

from bugbot import *
connect()

# position() counts from the start, so the line is 55 cm up from here
forward(70)
wait(1)
stop()
print("stopped at", position())

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/10-1-a-program-in-a-game/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Print the gap every time round the loop, race, then read your log to see where the braking started.
  2. Make the light amber while creeping and green once stopped.
  3. What is the fastest first speed that still stops in time?