The worksheetDownload the PDF
Answers

10.1 A program in a game

Competing · Robot club · about 14 min

BugBotLab

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
    Answer: A. info() is how a game talks to you. In a task, work from position() instead: the target is 55 cm from the start.
  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
    Answer: A, B, D, E. Your log is your own: nobody else sees it, so print as much as you like.
  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
    Answer:
    Look: read the sensors and what the game says
    Decide
    Move
    Wait a moment

    Look, decide, move, wait: the behaviour loop from Module 5, with other robots in the way.

  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")
    Answer:
    45 forward(90)
    15 forward(30)
    3 stop

    Far away it drives at 90, closer it creeps at 30, and within 4 cm it stops.

  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
    Answer: A. Without it the loop spins flat out and the robot has no time to act on what you told it.
  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?

    Answer: 2. The strip starts at 55 + 5 = 60 cm, and 60 - 58 = 2 cm. Close, but safe.

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())

The hint students can ask for: Drive fast while the line is far away, creep when it is close, and remember the robot slides a little after stop().

A solution

from bugbot import *
connect()
while position()[1] < 38:
    forward(80)
    wait(0.1)
while position()[1] < 53:
    forward(25)
    wait(0.1)
stop()
print('stopped at', position())

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.