# 斗牛太危险，来斗HEXA吧

OSC_Lucia 发布于 2017/09/22 14:19

l   转动HEXA的头部，每转动一定角度，HEXA就拍一张照片

l   判断照片里面是否有红色

l   如果有红色，HEXA停止转动头部，并向前跑去

l   如果没有红色，则转动头部继续寻找

l    跑动过程中继续拍照，如果还有红色则继续前进

l   跑动过程中如果没有红色里则停下寻找红色

https://github.com/vincross/hexa-example-skills

1. 动头

func (skill *ScanRed) searchRed() {
for skill.status {
if skill.round {
direction := hexabody.Direction()
direction += 30
skill.checkRedLightDistrict()
time.Sleep(time.Millisecond * 100)
}
time.Sleep(time.Millisecond * 200)
}
}

2. 检查图片是否为红

func isRed() bool {
thresHold := 200
subRed := 0
srcImg := media.SnapshotRGBA()
srcBounds := srcImg.Bounds()
m := image.NewRGBA(srcBounds)
ptX := (srcBounds.Size().X * 1) / 10
ptY := (srcBounds.Size().Y * 1) / 10
draw.Draw(m, srcImg.Bounds(), srcImg, image.Pt(ptX, ptY), draw.Src)
subBounds := image.Rect(srcBounds.Min.X/2, srcBounds.Min.Y/2, srcBounds.Max.X/2, srcBounds.Max.Y/2)
newImg := m.SubImage(subBounds)
wight := newImg.Bounds().Size().X
hight := newImg.Bounds().Size().Y
for w := 0; w < wight; w++ {
for h := 0; h < hight; h++ {
r, g, b, _ := newImg.At(w, h).RGBA()
r = r >> 8
g = g >> 8
b = b >> 8
c := (int(r) - int(g)) + (int(r) - int(b))
if c > thresHold {
subRed++
}
}
}
log.Info.Printf("%d %d", subRed, wight*hight)
if subRed > (wight*hight)/200 {
return true
}
return false
}

3. 往前走

func (skill *ScanRed) goToRed() {
for skill.status {
if skill.run {
log.Info.Printf("RUN...")
hexabody.Walk(hexabody.Direction(), 100)
}
else {
time.Sleep(time.Millisecond * 200)
}
}
}

0

0