我的代码很长,我需要帮助压缩它使它更方便。我的代码是假设我命令它在福禄克机器人上执行的一组指令。我用的是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()
此实现的工作原理是使用
ARGB
像素,如果有RGB
像素替换:0xFFFFFFFF
带{我要做的是:
所以用十六进制表示:
^{pr2}$A
必须是FF
(255)才能没有透明度。在我应该提到,我用这段代码做了一个假设,即像素是32位整数的形式。在
几点意见:
您的
pcMarked
只看到红色而找到白色,这意味着它将获得所有完全红色。您的
markyellow
函数使像素变为白色而不是黄色。在相关问题 更多 >
编程相关推荐