sensors.DistanceSensor

class sensors.DistanceSensor(port: Optional[int] = None)

Sensor for measuring linear distances.

distance() → float

Distance in meters to whatever is in front of the sensor. A slight smoothing filter is applied to this value. For an unfiltered version condsider stream().

The following example assumes that a distance sensor is assigned to port 0 and a TextScreen is assigned to port 1.

from sensors import DistanceSensor
from controllables import TextScreen

sensor = DistanceSensor(0)
screen = TextScreen(1)

while True:
    screen.text = f"Distance: {sensor.distance(): 4.1f}"
stream() → streams.Stream[float]

Creates an stream of distance values in meters.

The following example assumes that a distance sensor is assigned to port 0.

from sensors import DistanceSensor
from time import sleep

sensor = DistanceSensor(0)
stream = sensor.stream()

sensor.visible_laser = False

while True:
    for distance in stream:
        if distance < 1:
            sensor.visible_laser = True

    if sensor.visible_laser:
        sleep(.1)
        sensor.visible_laser = False
property visible_laser: bool

The visible laser property of the distance sensor avaliable in the properties tab.

name() → str

Returns the user editable name of the controllable as found in the properties tab of the game.

from ports import PortReference

print(PortReference(0).name())