robot = NxtRobot() gear = Gear() robot.addPart(gear) ts1 = TouchSensor(SensorPort.S1) # right sensor ts2 = TouchSensor(SensorPort.S2) # left sensor ls1 = LightSensor(SensorPort.S3) robot.addPart(ts1) robot.addPart(ts2) robot.addPart(ls1) gear.forward() lightsensor_timeout = False while not robot.getGameGrid().isDisposed(): if lightsensor_timeout != False and time.clock() > lightsensor_timeout: lightsensor_timeout = False t1 = ts1.isPressed() t2 = ts2.isPressed() if (t1 and t2): gear.backward(500) gear.left(400) gear.forward() else: if t1: gear.backward(500) gear.left(400) gear.forward() else: if t2: gear.backward(500) gear.right(100) gear.forward()
robot.addPart(motB) robot.addPart(sensor) def start_forward(): motA.stop() motB.stop() motA.forward() motB.forward() def move_backward(delay): motA.stop() motB.stop() motA.backward() motB.backward() Tools.delay(delay) def turn_right(delay): motA.stop() motB.stop() motA.backward() motB.forward() Tools.delay(delay) start_forward() while (True): if sensor.isPressed(): move_backward(1200) turn_right(750) start_forward()