# Simple but fun idea. Press both touch sensors together to unleash balls from ch.aplu.nxt import NxtRobot, Motor, MotorPort, TouchSensor, SensorPort, Tools robot = NxtRobot() gunmotor = Motor(MotorPort.A) ts1 = TouchSensor(SensorPort.S1) ts2 = TouchSensor(SensorPort.S2) robot.addPart(gunmotor) robot.addPart(ts1) robot.addPart(ts2) while true: if ts1.isPressed() and ts2.isPressed(): gunmotor.forward() else: gunmotor.stop()
from ch.aplu.nxt import NxtRobot, Motor, MotorPort, TouchSensor, SensorPort, Tools 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
robot.addPart(ts2) robot.addPart(us) def back_away(m1, m2, t): m1.stop() m2.stop() m1.backward m2.backward() Tools.delay(t) m1.stop() m2.stop() def shoot(m, t): m.forward() Tools.delay(t) m.stop() for i in range(5): # wait until something approaches while(us.getDistance() > 80): Tools.delay(20) # back away when it does back_away(m1, m2, 1000) if ts1.isPressed() or ts2.isPressed(): # shoot if backed against wall! shoot(gunmotor, 1000) # after we're forced back 5 times # we just open fire! shoot(gunmotor, 10000) robot.exit()
m1.stop() m2.stop() def start_forward(m1, m2): m1.forward() m2.forward() def stopfor(m1, m2, t): m1.stop() m2.stop() Tools.delay(t) start_forward(motor1, motor2) last_us_distance = -1 while True: t1 = ts1.isPressed() t2 = ts2.isPressed() if (t1 and t2): move_backward(motor1, motor2, 500) turn(motor1, motor2, 400) start_forward(motor1, motor2) else: if t1: move_backward(motor1, motor2, 1000) turn(motor2, motor1, 800) start_forward(motor1, motor2) elif t2: move_backward(motor1, motor2, 1000) turn(motor1, motor2, 800) start_forward(motor1, motor2)