gunmotor = Motor(MotorPort.A) motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) us = UltrasonicSensor(SensorPort.S4) robot.addPart(gunmotor) robot.addPart(motor1) robot.addPart(motor2) robot.addPart(us) def start_turn(m1, m2): m1.stop() m2.stop() m1.backward() m2.forward() def stop(m1, m2, t): m1.stop() m2.stop() Tools.delay(t) start_turn(motor1, motor2) while true: if (us.getDistance() < 80): stop(motor1, motor2, 500) gunmotor.forward() Tools.delay(1000) gunmotor.stop() start_turn(motor1, motor2) Tools.delay(400) # don't shoot again for awhile Tools.delay(20)
robot = NxtRobot() motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) ts1 = TouchSensor(SensorPort.S1) ts2 = TouchSensor(SensorPort.S2) robot.addPart(motor1) robot.addPart(motor2) robot.addPart(ts1) robot.addPart(ts2) m1counter = 0 m2counter = 1 while true: if (ts1.isPressed()): if (m1counter == 0): motor1.forward() m1counter = 5 if (ts2.isPressed()): if (m2counter == 0): motor2.forward() m2counter = 5 if (m1counter == 0): motor1.stop() else: m1counter = m1counter - 1 if (m2counter == 0): motor2.stop() else: m2counter = m2counter - 1 Tools.delay(500)