CodeMonkey過關學習筆記系列:特技關卡 5-1 ~ 5-10 關
阿新 • • 發佈:2019-02-06
特技關卡 5-1
turtles[0].step 6
turtles[2].step -8
turtles[1].step 6+6
step 19
特技關卡 5-2
step 6
turtles[2].step 20
step 6
turtles[1].step 8
step 6
turtles[0].step -12
特技關卡 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
特技關卡 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]
特技關卡 5-5
step 15
beavers[1 ].step 15
beavers[0].step -15
turn left
step 15
turn left
step 15
特技關卡 5-6
x=0
4.times ->
beavers[x].step 2.5*(x+1)
x += 1
step distanceTo banana
特技關卡 5-7
beavers[0].step 5
beavers[1].step 10
beavers[2].step 10
step distanceTo banana
特技關卡 5-8
x = 0
7.times ->
turnTo bananas[x]
step distanceTo bananas[x]
x = x+1
特技關卡 5-9
crocodiles[2].turn left
crocodiles[0].turn -45
step 9
turn left
step 9
turn right
step 9
特技關卡 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])