示例#1
0
# Import the stuff we need:
from ch.aplu.nxtsim import NxtRobot, Motor, MotorPort, Tools

# Make a robot object!
robot = NxtRobot()

# Make motor objects!
motA = Motor(MotorPort.A)
motB = Motor(MotorPort.B)

# Add motoro to robot!
robot.addPart(motA)
robot.addPart(motB)

# move the robot forward!
motA.forward()
motB.forward()
Tools.delay(2000)

# pause!
motA.stop()
motB.stop()
Tools.delay(2000)

# turn the robot!
motA.backward()
motB.forward()
Tools.delay(2000)

# pause!
motA.stop()
示例#2
0
# Import the stuff we need:
from ch.aplu.nxtsim import NxtRobot, Motor, MotorPort, Tools, LightSensor, SensorPort, NxtContext

NxtContext.useBackground("sprites/road.gif")

# Make a robot object!
robot = NxtRobot()

motA = Motor(MotorPort.A)
motB = Motor(MotorPort.B)
sensor = LightSensor(SensorPort.S3)

robot.addPart(motA)
robot.addPart(motB)
robot.addPart(sensor)

def step_forward():
    motA.setSpeed(50)
    motB.setSpeed(50)
    motA.forward()
    motB.forward()
    Tools.delay(1000)
    motA.stop()
    motB.stop()
    Tools.delay(300)

def run_backward():
    motA.setSpeed(100)
    motB.setSpeed(100)
    motA.backward()
    motB.backward()
示例#3
0
# 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)
示例#4
0
# Import the stuff we need:
from ch.aplu.nxtsim import NxtRobot, Motor, MotorPort, Tools, LightSensor, SensorPort, NxtContext

NxtContext.useBackground("sprites/road.gif")

# Make a robot object!
robot = NxtRobot()

motA = Motor(MotorPort.A)
motB = Motor(MotorPort.B)
sensor = LightSensor(SensorPort.S3)

robot.addPart(motA)
robot.addPart(motB)
robot.addPart(sensor)

def step_forward():
    motA.setSpeed(50)
    motB.setSpeed(50)
    motA.forward()
    motB.forward()
    Tools.delay(1000)
    motA.stop()
    motB.stop()
    Tools.delay(300)

def run_backward():
    motA.setSpeed(100)
    motB.setSpeed(100)
    motA.backward()
    motB.backward()