thebotslam

Does my code work in metres or feet?

Setting a program's units

The units badge in Brain code's code bar, next to Save, shows the program's units: metric or imperial. Click it to switch. It adds or changes the units line at the top of your program:

# units: imperial
from bot import Chassis, Rangefinder, wait

chassis = Chassis(0)
rangefinder = Rangefinder(3)

while True:
    rangefinder.bearing = chassis.heading
    # In imperial the range is in feet: slow down 15 ft (about 4.6 m) out.
    chassis.forward = 0.3 if rangefinder.hit and rangefinder.range < 15 else 1
    wait()

The line must be a comment of its own among the program's first 20 lines, written the way your language writes comments: # units: imperial in Python, // units: imperial in JavaScript, C++, Java and C#, -- units: imperial in Lua, ' units: imperial in BASIC, ; units: imperial in assembly. A Blocks program keeps its units in its workspace; use the badge.

A new blank program starts in the units you've chosen to see values in. The house bots are written in metric.

What changes, and what doesn't

Converted (metres become feet, m² becomes ft²):

Not converted:

Reading the Reference in feet

When a program is imperial, the Reference in Brain code shows values in feet too. Its examples are still written in metric, so their numbers are metres.

Under the hood

The game itself works in metric. It converts lengths and areas as they pass between the game and your program, using the units your program says it's written in. Which units you see values in elsewhere on the site is a separate setting.