這一關(guān)還是跟函數(shù)有關(guān)的。
我們在上一關(guān)里,試了函數(shù)的重復(fù)使用的方便性。在這里一關(guān)里我們將繼續(xù)體驗函數(shù)的強大。
我們將自己創(chuàng)建一個函數(shù),這次不僅僅是要右轉(zhuǎn)了,而是讓它能做一系列的事情。
我們能看到角色將重復(fù)幾個動作:前進一步,收集寶石,前進一步,切換開關(guān)。
我們把這些命令集成到一個函數(shù)里去,怎么樣,想到了吧。你以后只要發(fā)一個函數(shù)的命令,角色就能做四個不同的動作了。
是不是很心動。

我們把函數(shù)外的命令寫在下面。
yoyoFunc()
moveForward()
turnLeft()
yoyoFunc()
moveForward()
moveForward()
turnLeft()
yoyoFunc()
moveForward()
turnLeft()
yoyoFunc()
是不是覺得少了點什么?對就是把函數(shù)怎么寫的問題沒列出來。
我一樣列出來,大家要自己去點一下,才能感受到自己的勞作。
func yourfunc(){
? ? moveForward()
? ? collectGem()
? ? moveForward()
? ? toggleSwitch()
}
這里有一點點要動動腦的地方。就是上面代碼里的函數(shù)名和下面代碼里的函數(shù)名不一樣。你要自己動動手,改一下。
傳送門:
MacOS Playgrounds學(xué)習(xí) 學(xué)習(xí)編程一 前記--http://www.itdecent.cn/p/a0de277374a4
MacOS Playgrounds 學(xué)習(xí)編程一 第一關(guān) 發(fā)出命令--http://www.itdecent.cn/p/3c85397f1eae
MacOS Playgrounds 學(xué)習(xí)編程一 第二關(guān) 添加新命令--http://www.itdecent.cn/p/4efa5c18dc50
MacOS Playgrounds 學(xué)習(xí)編程一 第三關(guān) 切換開關(guān)--http://www.itdecent.cn/p/00556d6430bf
MacOS Playgrounds 學(xué)習(xí)編程一 第四關(guān) 傳送門練習(xí)--http://www.itdecent.cn/p/47bd57d0193f
MacOS Playgrounds 學(xué)習(xí)編程一 第五關(guān) 發(fā)現(xiàn)并修復(fù)程序錯誤--http://www.itdecent.cn/p/5bc9200edbbe
MacOS Playgrounds 學(xué)習(xí)編程一 第六關(guān) 消滅程序錯誤練習(xí)--http://www.itdecent.cn/p/f19bd2955ed8
MacOS Playgrounds 學(xué)習(xí)編程一 第七關(guān) 最短路線--http://www.itdecent.cn/p/1ef210253be5
MacOS Playgrounds 學(xué)習(xí)編程一 第八關(guān) 組合新行為--http://www.itdecent.cn/p/f548b1fd3134
MacOS Playgrounds 學(xué)習(xí)編程一 第九關(guān) 創(chuàng)建新函數(shù)--http://www.itdecent.cn/p/22ab39aaea97
MacOS Playgrounds 學(xué)習(xí)編程一 第十關(guān) 收集、切換、重復(fù)--http://www.itdecent.cn/p/e43d60f98bc7
MacOS Playgrounds 學(xué)習(xí)編程一 第十二關(guān) 嵌套模式--http://www.itdecent.cn/p/645f6716217c
MacOS Playgrounds 學(xué)習(xí)編程一 第十三關(guān) 嵌入式階梯--http://www.itdecent.cn/p/d87f9662601d
MacOS Playgrounds 學(xué)習(xí)編程一 第十四關(guān) 尋寶--http://www.itdecent.cn/p/18c73154e46b
MacOS Playgrounds 學(xué)習(xí)編程一 第十五關(guān) 使用循環(huán)--http://www.itdecent.cn/p/f7c0866930ec
MacOS Playgrounds 學(xué)習(xí)編程一 第十六關(guān) 循環(huán)每一側(cè)--http://www.itdecent.cn/p/94d75e9d5eab
MacOS Playgrounds 學(xué)習(xí)編程一 第十七關(guān) 行至邊緣再返回--http://www.itdecent.cn/p/cc7e8095bb9a
MacOS Playgrounds 學(xué)習(xí)編程一 第十八關(guān) 循環(huán)跳躍者--http://www.itdecent.cn/p/99618bef896e
MacOS Playgrounds 學(xué)習(xí)編程一 第十九關(guān) 拓展技能--http://www.itdecent.cn/p/a20810b11d6d
MacOS Playgrounds 學(xué)習(xí)編程一 第二十關(guān) 寶石農(nóng)場--http://www.itdecent.cn/p/8c3808d1a646
MacOS Playgrounds 學(xué)習(xí)編程一 第二十一關(guān) 席卷四處--http://www.itdecent.cn/p/cc7f6a1c34e7
MacOS Playgrounds 學(xué)習(xí)編程一 第二十二關(guān) 檢查開關(guān)--http://www.itdecent.cn/p/e34fb0431b6e
MacOS Playgrounds 學(xué)習(xí)編程一 第二十三關(guān) 使用 else if--http://www.itdecent.cn/p/6414ef7692e2
MacOS Playgrounds 學(xué)習(xí)編程一 第二十四關(guān) 循環(huán)條件代碼--http://www.itdecent.cn/p/a657e47b851d
MacOS Playgrounds 學(xué)習(xí)編程一 第二十五關(guān) 滿足條件時向上爬--http://www.itdecent.cn/p/4a69a1c3435f
MacOS Playgrounds 學(xué)習(xí)編程一 第二十六關(guān) 定義更巧妙的函數(shù)--http://www.itdecent.cn/p/d61e03e095ae
MacOS Playgrounds 學(xué)習(xí)編程一 第二十七關(guān) 圍困其中--http://www.itdecent.cn/p/88442c7ce61b
MacOS Playgrounds 學(xué)習(xí)編程一 第二十八關(guān) 決策樹--http://www.itdecent.cn/p/0843bd9ae286
MacOS Playgrounds 學(xué)習(xí)編程一 第二十九關(guān) 使用“非”運算符--http://www.itdecent.cn/p/be8fa74b6c46
MacOS Playgrounds 學(xué)習(xí)編程一 第三十關(guān) 非之螺旋--http://www.itdecent.cn/p/f42905cbaed1
MacOS Playgrounds 學(xué)習(xí)編程一 第三十一關(guān) 檢查這個與那個--http://www.itdecent.cn/p/67b1c6e16b6a
MacOS Playgrounds 學(xué)習(xí)編程一 第三十二關(guān) 檢查這個或那個--http://www.itdecent.cn/p/d94a67728a93
MacOS Playgrounds 學(xué)習(xí)編程一 第三十三關(guān) 邏輯迷宮--http://www.itdecent.cn/p/62bbc0ce2490
MacOS Playgrounds 學(xué)習(xí)編程一 第三十四關(guān) 當(dāng)……時運行代碼--http://www.itdecent.cn/p/9aa41733c6d0
MacOS Playgrounds 學(xué)習(xí)編程一 第三十五關(guān) 創(chuàng)建更巧妙的while循環(huán)--http://www.itdecent.cn/p/3fdfb33a443f
MacOS Playgrounds 學(xué)習(xí)編程一 第三十六關(guān) 正確選取工具--http://www.itdecent.cn/p/e5ed25be735e
MacOS Playgrounds 學(xué)習(xí)編程一 第三十七關(guān) 四乘以四--http://www.itdecent.cn/p/81ed2e6009d4
MacOS Playgrounds 學(xué)習(xí)編程一 第三十八關(guān) 轉(zhuǎn)身--http://www.itdecent.cn/p/a3c9bae7d6a2
MacOS Playgrounds 學(xué)習(xí)編程一 第三十九關(guān) 富饒之地--http://www.itdecent.cn/p/077743cdfcdd
MacOS Playgrounds 學(xué)習(xí)編程一 第四十關(guān) 套嵌循環(huán)--http://www.itdecent.cn/p/d8670c9e7962
MacOS Playgrounds 學(xué)習(xí)編程一 第四十一關(guān) 隨機矩形--http://www.itdecent.cn/p/61b916f61e37
MacOS Playgrounds 學(xué)習(xí)編程一 第四十二關(guān) 始終右轉(zhuǎn)--http://www.itdecent.cn/p/b7cf92fbfcaa
MacOS Playgrounds 學(xué)習(xí)編程一 第四十三關(guān) 右手定則--http://www.itdecent.cn/p/9ccd20151c96
MacOS Playgrounds 學(xué)習(xí)編程一 第四十四關(guān) 調(diào)整算法--http://www.itdecent.cn/p/10c2a0de990d
MacOS Playgrounds 學(xué)習(xí)編程一 第四十五關(guān) 征服迷宮--http://www.itdecent.cn/p/b4d731fe3639
MacOS Playgrounds 學(xué)習(xí)編程一 第四十六關(guān) 左轉(zhuǎn)還是右轉(zhuǎn)--http://www.itdecent.cn/p/cd7fa32316e5
MacOS Playgrounds 學(xué)習(xí)編程一 第四十七關(guān) 向左走,向右走--http://www.itdecent.cn/p/b9b1dc9c8f2e
MacOS Playgrounds學(xué)習(xí) 學(xué)習(xí)編程一 小結(jié)后記--http://www.itdecent.cn/p/8ad6485e447a