CodeMonkey过关学习笔记系列:特技关卡 5-1 ~ 5-10 关

image47.jpeg

特技关卡 5-1
turtles[0].step 6
turtles[2].step -8
turtles[1].step 6+6
step 19

image48.jpeg

特技关卡 5-2
step 6
turtles[2].step 20
step 6
turtles[1].step 8
step 6
turtles[0].step -12

image49.jpeg

特技关卡 5-3
turtle.turnTo bananas[0]
turtle.step 9

turtle.turnTo bananas[1]
turtle.step 19


turtle.turnTo bananas[2]
turtle.step 19

//或者利用distanceTo来测量距离
turtle.turnTo bananas[0]
turtle.step distanceTo bananas[0]

turtle.turnTo bananas[1]
turtle.step distanceTo bananas[1]


turtle.turnTo bananas[2]
turtle.step distanceTo bananas[2]

//或者 使用之前我们学过的times循环
i = 0
3.times ->
    turtle.turnTo bananas[i]
    turtle.step distanceTo bananas[i]
    i++

//或者 使用我们还没学到的for循环
for b in bananas
    turtle.turnTo b
    turtle.step distanceTo b



image50.jpeg

特技关卡 5-4
turnTo bananas[4]
step distanceTo bananas[4]

turnTo bananas[7]
step distanceTo bananas[7]

turnTo bananas[10]
step distanceTo bananas[10]

turnTo bananas[7*2]
step distanceTo bananas[7*2]


image51.jpeg

特技关卡 5-5
step 15
beavers[1].step 15
beavers[0].step -15
turn left
step 15
turn left
step 15

image52.jpeg

特技关卡 5-6
x=0
4.times ->
    beavers[x].step 2.5*(x+1)
    x += 1
step distanceTo banana

image53.jpeg

特技关卡 5-7
beavers[0].step 5
beavers[1].step 10
beavers[2].step 10
step distanceTo banana

image54.jpeg

特技关卡 5-8
x = 0
7.times ->
    turnTo bananas[x]
    step distanceTo bananas[x]
    x = x+1


image55.jpeg

特技关卡 5-9

crocodiles[2].turn left
crocodiles[0].turn -45


step 9
turn left
step 9
turn right
step 9


image56.jpeg

特技关卡 5-10
goats[0].step 3
goats[0].hit()
goats[0].step -11
goats[0].hit()
goats[1].step 14
goats[1].hit()
goats[1].step -18
goats[1].hit()
4.times ->
    step 20
    turn right

//另外一种解法//--------------------------------------------------------------------------------------------
goto = (o, t) ->
    o.turnTo(t)
    o.step(o.distanceTo(t))
hitB = (goat, banana) ->
    goto(goat, banana)
    goat.hit()
hitB(goats[1], bananas[0])
hitB(goats[0], bananas[1])
hitB(goats[0], bananas[2])
hitB(goats[1], bananas[3])
3.times ->
    monkey.step 20
    monkey.turn right

//另外一种解法//--------------------------------------------------------------------------------------------

turnStepHit = (goat, banana) ->
    goat.turnTo banana
    goat.step goat.distanceTo banana
    goat.hit()
turnStepHit(goats[0], bananas[1])
turnStepHit(goats[0], bananas[2])
turnStepHit(goats[1], bananas[0])
turnStepHit(goats[1], bananas[3])
4.times ->
    step 20
    turn right

//另外一种解法   ,这种就是之前拼凑出重复的代码,然后封装为函数了。//--------------------------------------------------------------------------------------------

goats[1].turnTo bananas[0]
goats[1].step(goats[1].distanceTo bananas[0])
goats[1].hit()
monkey.step(monkey.distanceTo bananas[0])

monkey.turnTo bananas[1]  //之前这一句是不用加的
//--------------------------------------------------------------------------------------------

goats[0].turnTo bananas[1]
goats[0].step(goats[0].distanceTo bananas[1])
goats[0].hit()
monkey.step(monkey.distanceTo bananas[1])

monkey.turnTo bananas[2]
//--------------------------------------------------------------------------------------------

goats[0].turnTo bananas[2]
goats[0].step(goats[0].distanceTo bananas[2])
goats[0].hit()
monkey.step(monkey.distanceTo bananas[2])

monkey.turnTo bananas[3]
//--------------------------------------------------------------------------------------------

goats[1].turnTo bananas[3]
goats[1].step(goats[1].distanceTo bananas[3])
goats[1].hit()
monkey.step(monkey.distanceTo bananas[3])

//--------------------------------------------------------------------------------------------

//下面的解法为什么有个next参数? 是因为之前分步骤写的时候 把turnTo归到上面的代码了。所以才有了如下函数的封装
eatBananas = (monkey, goat, banana, next) ->
    goat.turnTo(banana)
    goat.step(goat.distanceTo(banana))
    goat.hit()
    monkey.step(monkey.distanceTo(banana))
    monkey.turnTo(next)

eatBananas(monkey, goats[1], bananas[0], bananas[1])
eatBananas(monkey, goats[0], bananas[1], bananas[2])
eatBananas(monkey, goats[0], bananas[2], bananas[3])
eatBananas(monkey, goats[1], bananas[3], bananas[0])

//monkey eat bananas with goat help//--------------------------------------------------------------------------------------------

eatBananas = (banana, next, goat) ->
    goat.turnTo(banana)
    goat.step(goat.distanceTo(banana))
    goat.hit()
    step(distanceTo(banana))
    turnTo(next)
eatBananas(bananas[0], bananas[1],goats[1])  # 第一个参数顺序很重要
eatBananas(bananas[1], bananas[2],goats[0])
eatBananas(bananas[2], bananas[3],goats[0])
eatBananas(bananas[3], bananas[0],goats[1])

//另外一种解法//--------------------------------------------------------------------------------------------

eatBananas = (banana, goat) ->
    goat.turnTo(banana)
    goat.step(goat.distanceTo(banana))
    goat.hit()
    turnTo(banana)
    step(distanceTo(banana))

eatBananas(bananas[0], goats[1])
eatBananas(bananas[1], goats[0])
eatBananas(bananas[2], goats[0])
eatBananas(bananas[3], goats[1])

//另外一种解法--------------------------------------------------------------------------------------------
goto = (o, t) ->
    o.turnTo(t)
    o.step(o.distanceTo(t))

eatBananas = (banana, goat) ->
    goto(goat, banana)
    goat.hit()
    goto(monkey, banana)

eatBananas(bananas[0], goats[1])
eatBananas(bananas[1], goats[0])
eatBananas(bananas[2], goats[0])
eatBananas(bananas[3], goats[1])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值