Competing · Robot club · about 14 min
others() and the camera tags, and giving way early so contact never costs you the game.
[1 mark]In the traffic task, a student calls others() to find the moving robots. What happens?
others() is only there in games, so in a task use the cameraothers() is the game's own view. In a task, robots() shows what is in front of you, each one found by its dome colour.[1 mark]What does this program print?
import math
bots = [{"name": "red-1", "x": 30, "y": 40}, {"name": "blue-2", "x": -6, "y": 8}, {"name": "blue-3", "x": 20, "y": 0}]
closest, best = None, 1e9
for bot in bots:
d = math.hypot(bot["x"], bot["y"])
if d < best:
closest, best = bot["name"], d
print(closest, best)blue-2 10.0
Measured from (0, 0) the distances are 50, 10 and 20, so blue-2 is closest at 10.0 cm.
[1 mark]You touch another robot that drove into you. Whose fault is it, in most games?
[1 mark]Which habits keep you out of trouble on a busy mat?
Tick every answer that is true.
left() or right()[1 mark]robots() comes back as [['red', 160, 118, 35.0]]. What is 35.0?
[colour, cx, cy, distance], nearest first. The bearing comes from cx: (cx - 160) * 120 / 320.[1 mark]What does this program print?
for gap in [40, 25, 12]:
if gap < 25:
print(gap, "step aside")
else:
print(gap, "carry on")40 carry on 25 carry on 12 step aside
gap < 25 is false when the gap is exactly 25, so only 12 is close enough to step aside.
Two robots are driving back and forth across the mat, 35 cm and 55 cm ahead of you. Get from the start to the green zone without touching either of them, or anything else.
from bugbot import *
connect()
while position()[1] < 80:
forward(50)
wait(0.1)
stop()The hint students can ask for: Two robots cross your path, one red and one blue. Watch their domes with robots() and wait or go round.
from bugbot import *
connect()
def robot_near():
return any(r[3] < 30 for r in robots())
while position()[1] < 76:
if robot_near():
stop()
else:
forward(45)
wait(0.1)
stop()
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.