# SimEx06.py # Two touch sensors, complex track import time from ch.aplu.nxtsim import NxtContext, NxtRobot, Gear, TouchSensor, SensorPort, Tools, LightSensor NxtContext.setLocation(10, 10) NxtContext.setStartDirection(5) NxtContext.setStartPosition(100, 240) NxtContext.useObstacle(NxtContext.channel) NxtContext.useBackground("sprites/channel.jpg") 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)
from ch.aplu.nxtsim import NxtRobot, Motor, MotorPort, Tools, TouchSensor, SensorPort, NxtContext NxtContext.init() NxtContext.showNavigationBar(); NxtContext.useObstacle("sprites/bar0.gif", 250, 200); NxtContext.useObstacle("sprites/bar1.gif", 400, 250); NxtContext.useObstacle("sprites/bar2.gif", 250, 400); NxtContext.useObstacle("sprites/bar3.gif", 100, 250); robot = NxtRobot() motA = Motor(MotorPort.A) motB = Motor(MotorPort.B) sensor = TouchSensor(SensorPort.S3) robot.addPart(motA) 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)