我的代码很长,我需要帮助压缩它以使其更方便。我拥有的代码是假设执行我命令它在侥幸机器人上执行的一组指令。我正在使用python。机器人应该使用它的传感器执行以下代码。我需要帮助浓缩它。
编辑:
我的代码:
from Myro import *
from Graphics import *
init('/dev/tty.IPRE6-366079-DevB')
def markYellow(pic):
for pix in getPixels(pic):
r = getRed(pix)
g = getGreen(pix)
b = getBlue(pix)
if r > 200 and b < 90 and g > 150:
setRed(pix,255)
setGreen(pix,255)
setBlue(pix,255)
else:
setRed(pix,0)
setGreen(pix,0)
setBlue(pix,0)
def pctMarked(pic):
totalPixels = 0
whitePixels = 0
for pix in getPixels(pic):
if getRed(pix) == 255:
whitePixels = whitePixels + 1
totalPixels = totalPixels + 1
result = whitePixels / float(totalPixels)
return result
def findAvgX(pic):
pixelCount = 0
totalXCount = 0
for pix in getPixels(pic):
if getRed(pix) == 255:
x = getX(pix)
totalXCount = totalXCount + x
pixelCount = pixelCount + 1
avgX = totalXCount / float( pixelCount)
return avgX
def turn():
findAvgX(pic)
if wallLocation <= 85:
turnLeft(1,0.25)
elif ballLocation >= 170:
turnRight(1,0.25)
def celebrate():
move(0.25,1)
beep(1,800)
beep(1,1600)
beep(1,800)
stop()
def main():
p = takePicture()
markYellow(p)
pctMarked(p)
while pctMarked(pic) < 0.2:
rotate(1,1)
p = takePicture()
markYellow(p)
pctMarked(p)
turn()
while getObstacle('center')> 1000: # I'm not sure about the number. We can test it tomorrow
forward(1,1)
celebrate()