Codemonkey之编码冒险之故事模式1-165关卡学习笔记与参考答案

Codemonkey之编码冒险之故事模式1-165关卡学习笔记与参考答案

image18.jpeg

//第 0 关挑战
step 15

image18.jpeg

1 关挑战 和0关一样的
step 15

image19.jpeg

2 关挑战
step 20

image20.jpeg

3 关挑战

turn right

step 12

image21.jpeg

4 关挑战

turn left
step 20

image22.jpeg

5 关挑战
turn left
step 15

turn right
step 15


image23.jpeg

6 关挑战
turn left
step 5

turn right
step 17


turn right
step 5

image24.jpeg

7 关挑战
你也可以用 转向 turn 这个指令再加上转动的角度.
比如 "逆时针方向旋转 45 度" 写为 turn 45 或者 "顺时针方向旋转 90度" 写为turn -90.

turn 45
step 14

image25.jpeg

8 关挑战
turn left
step 15

turn left
step 15

turn left
step 15

image26.jpeg

9 关挑战
step 10


step -20

image27.jpeg

10 关挑战
turn 45.5
step 15

turn left
step 10

step -20

image28.jpeg

11 关挑战
面对 turnTo 这个指令能够让小猴面对其它对象.
turnTo banana

step 20

image29.jpeg

12 关挑战
你也可以用 面对turnTo函数让小猴面对香蕉 banana以外的对象
另外,你可以将鼠标移动到对象来看它的英文, 再点击一下就可以把它加到你的代码里.
turnTo bridge
step 10
turnTo banana
step 10

image30.jpeg

13 关挑战
turtle.step 20

image31.jpeg

14 关挑战
step 5

turtle.step 10

step 5

image32.jpeg

15 关挑战
turtle.step 10
monkey.step 15

image33.jpeg

16 关挑战

turnTo turtle
step 9
turtle.step 15
step -11

image34.jpeg

17 关挑战
turtle.turn right

step 10

turtle.step 20

image35.jpeg

18 关挑战
turtle.turnTo banana

turtle.step 10

monkey.step 10

turtle.step 10

image36.jpeg

19 关挑战
turtle.step 15
turtle.turnTo bananas[3]
turtle.step 28

image37.jpeg

20 关挑战
turtle.turnTo monkey
turtle.step 18


turtle.turnTo banana
turtle.step 18

image38.jpeg

21 关挑战
3.times ->
    turn left
    step 15

image39.jpeg

22 关挑战
10.times ->    
    step 6
    turn 36
或者
10.times ->
    turn -36
    step -6

image40.jpeg

23 关挑战

5.times ->
    step -10
    turn -60
或者:
5.times ->
    turn 60
    step 10
或者:
5.times ->
    step -10
    turn 300

image41.jpeg

24 关挑战

3.times ->
    step 13
    turn right


image42.jpeg

25 关挑战
//你可以修正这个循环 loop 的错误吗 ?
4.times ->
    turn left
    step 5
    turn right
    step 5
//解法:
s = 20
For I in [1..11]
   step s
   turn left
   if I % 3 == 0
      s -= 5
//另外的解法:
s=20
I=1
11.times ->
    step s
    turn left
    if I % 3 == 0
        s -= 5
    I++
//另外的解法:
2.times ->
    turn 45
    step 30
    step -30
    turn -45
    step -5
//另外的解法:
2.times ->
    turn 45
    step 28
    turn 135
    step 5

image43.jpeg

26 关挑战
step 5
3.times ->
    turtle.step 10
    turtle.turn right


image44.jpeg

27 关挑战
4.times ->
    step 10
    step -10
    turn right



image45.jpeg

28 关挑战
10.times ->
    step 3
    turn 18
step 10


image46.jpeg

29 关挑战
turtle.step 5
step 3
3.times ->
    turtle.step 10
    turtle.turn left
turn left
step 5


image47.jpeg

30 关挑战
快过关了喽 !让我看看你怎么使用循环吧.
step 10
3.times ->
    step 10
    step -10
    turtle.step 10
对循环的一个总结,先初始化,猴子先到乌龟上面

第2解法:
3.times->
   step 20
   step -10
   turtle.step 10
   step -103种解法:
step 20
3.times ->
    step -10
    turtle.step 10
    step 10


//另外一种方法
step 20
2.times ->
    step -7.5
    turn right
    step 10
    turn left
    step 7.5

//还有一种
step 20
turn 180
2.times ->
    step -10
    turn left
    step 10
    turn right
    step 10
//还有一种
step 20
2.times ->
    2.times ->
        step 10
        turn right
    step 10
    turn 180
    turtle.step 20

image48.jpeg

31 关挑战
变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念。
x = 20
step x

image49.jpeg

32 关挑战
x = 15
4.times ->
    turn right
    step x


image50.jpeg

33 关挑战
所有的数目字 number 你都可以用 变量 variable来代替它.

n = 6
x = 10
d = 60

n.times ->
    step x
    turn d


image51.jpeg

34 关挑战
t = 3
x = 20
d = 120

t.times ->
    step x
    turn d
//这种转圈循环的,就可以使用这种通用解法,确定3个数字,即:次数(香蕉个数),步数(2个香蕉直接距离),角度(360除以香蕉个数)

image52.jpeg

35 关挑战
x=15
turtle.step x
step x
turn left
step x


image53.jpeg

36 关挑战

x = 15
4.times ->
    step x
    step -x
    turtle.step 8

//这种的是不是也可以用个for循环?? 不过有要求,香蕉的排序下标最右边从0开始
x = 15
for b in bananas
    step x
    step -x
    turtle.step 8

image54.jpeg

37 关挑战
say "xxxxxxxxx"

step 15

image55.jpeg

38 关挑战
x = 20
say x

turnTo banana
step x

image56.jpeg

39 关挑战

变量也可以用来取代你不知道的数目.
x = distanceTo turtle
say x

step x

//介绍一下函数distanceTo 的作用

image57.jpeg

40 关挑战

//你也可以使用 turtle.distanceTo 的指令写代码 !
x = turtle.distanceTo banana

step 2

image58.jpeg

41 关挑战

d = turtle.distanceTo monkey
turtle.step d
turtle.step –d
turtle.turn right
turtle.step d

image59.jpeg

42 关挑战
turnTo banana
step distanceTo banana

image60.jpeg

43 关挑战

turnTo bridge
step distanceTo bridge
turnTo banana
step distanceTo banana

image61.jpeg

44 关挑战

别让老鼠把宝贵的香蕉给偷走了 !
say "Let's go!“

turnTo banana

step distanceTo banana

image62.jpeg

45 关挑战
say "Hello world!“
turnTo bridge
step distanceTo bridge

turnTo banana

step distanceTo banana


image63.jpeg

46 关挑战
当我们有一根以上的香蕉时,我们可以用 [] 这个指令来帮它们分别命名.
数组  "数组"索引
turnTo bananas[0]
step distanceTo bananas[0]

turnTo bananas[1]
step distanceTo bananas[1]


image64.jpeg

47 关挑战

在写代码的时候,你可以从 零 zero 开始编号,因此第一根香蕉可以命名为" 零号香蕉" bananas[0] ;而第二根香蕉可以命名为"一号香蕉" bananas[1].

say "Boo!“
step 10
turnTo bananas[0]
step distanceTo bananas[0]

turnTo bananas[1]
step distanceTo bananas[1]

image65.jpeg

48 关挑战
bananas 是一个 数组 array 也就是对象 (在这里指的是香蕉) 的"集合“

turnTo bananas[0]
step distanceTo bananas[0]
turnTo bananas[1]
step distanceTo bananas[1]
turnTo bananas[2]
step distanceTo bananas[2]


image66.jpeg

49 关挑战
灌木丛 bushes 也是一个 数组 array
turnTo bushes[0]
step distanceTo bushes[0]
turnTo bushes[1]
step distanceTo bushes[1]
turnTo banana
step distanceTo banana


image67.jpeg

50 关挑战
河狸们 beavers 会帮助我们的小猴过河.
beavers[0].step 10
beavers[1].step 10
step distanceTo banana

image68.jpeg

51 关挑战

请用你的代码指令让河狸们动起来.
beavers[1].step 13
beavers[2].step 13
step distanceTo banana

image69.jpeg

52 关挑战

beavers[0].step 4
beavers[1].step 8
beavers[2].step 12
step distanceTo banana

image70.jpeg

53 关挑战

beavers[0].step 5
beavers[1].step 5

step distanceTo bananas[0]
turnTo bananas[1]
step distanceTo bananas[1]

image71.jpeg

54 关挑战

beavers[0].step 5
beavers[1].step 5
beavers[2].step 5
beavers[3].step 5

step distanceTo bananas[0]
turnTo bananas[1]
step distanceTo bananas[1]

image72.jpeg

55 关挑战

beavers[0].step 4
beavers[1].step 4

step distanceTo bananas[0]
beavers[0].step 11
beavers[1].step 11

turnTo bananas[1]
step distanceTo bananas[1]

turnTo bananas[2]
step distanceTo bananas[2]

image73.jpeg

56 关挑战

开始介绍for循环语句,for可以遍历一个数组
for b in bananas
    turnTo b
    step distanceTo b

image74.jpeg

57 关挑战

for b in bananas
    turnTo b
    step distanceTo b

image75.jpeg

58 关挑战

for b in bananas
    turtle.turnTo b
    turtle.step distanceTo b

image76.jpeg

59 关挑战

say "Hi!"
for b in bananas
    turnTo b
    step distanceTo b

image77.jpeg

60 关挑战

step distanceTo bridge
for b in bananas
    turnTo b
    step distanceTo b

image78.jpeg

61 关挑战

x = 20
for t in turtles
    t.step x
step distanceTo banana

image79.jpeg

62 关挑战

for b in bananas
    turnTo raft
    step distanceTo raft
    turnTo b
    step distanceTo b

image80.jpeg

63 关挑战

for b in islands
    turnTo b

    step distanceTo b

image81.jpeg

64 关挑战

for t in turtles
    step distanceTo t
    t.step 20

image82.jpeg

65 关挑战

for b in bananas
    x = distanceTo  b
    step x
    step -x
    turtle.step 8

image83.jpeg

66 关挑战


for c in crocodiles
    c.turnTo banana
step distanceTo banana

image84.jpeg

67 关挑战

for c in crocodiles
    c.turnTo banana
turnTo banana
step distanceTo banana

image85.jpeg

68 关挑战

for c in crocodiles
    c.turnTo islands[0]
for i in    islands
    turnTo i
    step distanceTo i

image86.jpeg

69 关挑战

for c in crocodiles
    c.turnTo raft
for b in bananas
    turnTo b
    d = distanceTo b
    step d
    step -d

image87.jpeg

70 关挑战

for b in bananas
    for c in crocodiles
        c.turnTo monkey
    turnTo b
    step distanceTo b

image88.jpeg

71 关挑战
"函数"农场

step distanceTo match
grab()
step pile.distanceTo rat
drop()

image89.jpeg

72 关挑战

turnTo match
step distanceTo match

grab()

turnTo pile
step distanceTo pile
drop()

image90.jpeg

73 关挑战

# 顺便说一下,这是一行注释哦  这里开始引入了注释的概念
# 它是由 '#' 开始的

for m in matches
    turnTo m
    step distanceTo m
    grab()
    turnTo pile
    step distanceTo pile
    drop()
    # 每次只能捡一根火柴

image91.jpeg

74 关挑战

# 有没有发现代码有重复的地方,下面会引入函数,就可以减少代码的重复了

turnTo bridge
step distanceTo bridge
turnTo match
step distanceTo match

grab()

turnTo bridge
step distanceTo bridge
turnTo pile
step distanceTo pile

drop()

image92.jpeg

75 关挑战

# 这里开始引入函数的概念了,这一关卡是不是和上一个很类似
goto = (t) ->
    turnTo t
    step distanceTo t


goto bridge
goto match
grab()
goto bridge
goto pile
drop()

image93.jpeg

76 关挑战

goto = (a) ->
    turnTo a
    step distanceTo a

for match in matches
    goto match
    grab()
    goto pile
    drop()

//================================================

goto = (a) ->
    turnTo a
    step distanceTo a
goto matches[0]
grab()
goto pile
drop()
goto matches[1]
grab()
goto pile
drop()

image94.jpeg

77 关挑战

goto = (a) ->
    turnTo a
    step distanceTo a

for m in matches
    goto m
    grab()
    goto pile
    drop()

image95.jpeg

78 关挑战

goto = (c) ->
    turnTo c
    step distanceTo c

goto bridge
goto match
grab()
goto bridge

goto(pile)
drop()


image96.jpeg

79 关挑战

goto = (p) ->
    #请在这里编写函数的实现
    turnTo p
    step distanceTo p

for m in matches
    goto bridge
    goto m
    grab()
    goto bridge
    goto pile
    drop()

image97.jpeg

80 关挑战

collect = (e) ->
    turnTo e
    step distanceTo e
    grab()
    turnTo pile
    step distanceTo pile
    drop()
for m in matches
    collect m

image98.jpeg

81 关挑战

这里不只有一个函数, 而是有 两个 函数 !
goto = (t) ->
    turnTo t
    step distanceTo t

collect = (e) ->
    goto e
    grab()
    goto pile
    drop()

for m in matches
    collect m


image99.jpeg

82 关挑战

#请在这里定义 goto 函数
goto = (x)->
    turnTo x
    step distanceTo x

goto match
grab()
goto pile
drop()

image100.jpeg

83 关挑战

接下来我们试试将小岛 islands 和木筏 rafts 作为函数的参数来使用

goto = (t) ->
    turnTo t
    step distanceTo t
gotoAll = (stuff) ->
    for s in stuff
        goto s

gotoAll islands
grab()
gotoAll rafts
drop()

image101.jpeg

84 关挑战

goto = (t) ->
    turnTo t
    step distanceTo t
getAndReturn = (r) ->
    goto r
    grab()
    goto turtle
for m in matches
    getAndReturn m
    goto pile
    drop()
    goto turtle
    turtle.step 8

image102.jpeg

85 关挑战

我们也可以用数字 numbers 作为函数的参数
goto = (t) ->
    turnTo t
    step distanceTo t
collect = (m) ->
    goto m
    grab()
    goto pile
    drop()
#这个函数应该让所有
#turtles 前进 d (d是数字)
allTurtlesStep = (d) ->
    #请完成这里!
    for c in turtles
        c.step d

allTurtlesStep 10
collect matches[0]
allTurtlesStep -10
collect matches[1]

image103.jpeg

86 关挑战

//"直到"沙地
//接下来要介绍的是 直到 until 这个循环指令 !
//这个和71关是一样的。这里用到了until和near。
until near match
    step 1

grab()
until near pile
    step 1
drop()

image104.jpeg

87 关挑战

//接近 near 这个指令会返回给你 - 肯定的 yes 或 否定的 no 的结果
//这个和71,86关是一样的。这里用到了until和near。

until near match
    step 1
grab()
until near pile
    step 1
drop()

image105.jpeg

88 关挑战

//如果你不好好使用的话 , 直到 until 这个循环指令将会永无止尽的继续下去.
//介绍了死循环

until near match
    step 1
grab()
turnTo pile
until near pile
    step 1
drop()

image106.jpeg

89 关挑战

接下来你需要好好利用 直到 until 这个循环指令哦.

until near match
    step 1
grab()
turnTo pile
step distanceTo pile
drop()

image107.jpeg

90 关挑战
turn left
until near match
    step 1
grab()
turnTo pile
step distanceTo pile
drop()


image108.jpeg

91 关挑战

until near match
    step 1
grab()
turnTo pile
step distanceTo pile
drop()

image109.jpeg

92 关挑战
until turtle.near bridge
    turtle.step 1
grab()
step distanceTo pile
drop()



image110.jpeg

93 关挑战

until near match
    turnTo match
    step 1
grab()
turnTo pile
step distanceTo pile
drop()

image111.jpeg

94 关挑战

希望你没有忘了函数,也就是 functions !
chase = (m) ->
    until near m
        turnTo m
        step 1
chase match
grab()
chase pile
drop()

image112.jpeg

95 关挑战
chase = (m) ->
    until near m 
        turnTo m
        step 1
#这段代码是正常的:
for m in matches
    chase m
    grab()
    chase pile
    drop()


image113.jpeg

96 关挑战

goto = (t) ->
    turnTo t
    step distanceTo t
collect = (t) ->
    goto t
    grab()
    goto pile
    drop()

#如果猫还醒着,千万别走过去!
until cat.sleeping()
    wait()
collect match

step 15

image114.jpeg

97 关挑战

用 wait 指令来静待猫咪睡觉吧...
goto = (t) ->
    turnTo t
    step distanceTo t
collect = (t) ->
    goto t
    grab()
    goto pile
    drop()
for m in     matches
    until cat.sleeping()
        wait()
    collect m


image115.jpeg

98 关挑战
goto = (t) ->
    turnTo t
    step distanceTo t
#让这个函数能够收集t  collect t
#并且不要被吃掉或掉到水里哦
safeCollect = (t) ->
    until cat.sleeping()
        wait()
    #就在那儿!
    goto bridge
    goto t
    grab()
    goto bridge

    goto pile
    drop()

for m in matches
    safeCollect m


image116.jpeg

99 关挑战

chase = (t) ->
    #实现这里!
    turnTo t
    step distanceTo t

safeCollect = (m) ->
    until cat.sleeping()
        wait()
    #完成这里!
    until near m
        chase m
    grab()
    chase pile
    drop()

safeCollect match

image117.jpeg

100 关挑战

chase = (t) ->
    turnTo t
    step distanceTo t

safeCollect = (m) ->
    until cat.sleeping()
        wait()
    until near m
        chase m
    grab()
    chase pile
    drop()
for m in matches
    safeCollect m

image118.jpeg

101 关挑战

goto bridge

goto banana

image119.jpeg

102 关挑战

goat.hit()goto banana

image120.jpeg

103 关挑战
goat.hit(banana)
goto banana


image121.jpeg

104 关挑战

#goat.hit()
goat.turn right
goat.hit()

step 20

image122.jpeg

105 关挑战

goat.goto bananas[0]
goat.hit()

goat.goto bananas[2]

goat.hit()

for b in bananas
    goto b


image123.jpeg

106 关挑战

请试试看 冷冻的香蕉banana.frozen() 这个指令.


say banana.frozen()
goat.hit()
say banana.frozen()
goto banana

image124.jpeg

107 关挑战

请使用 如果 if 指令来判断条件是否成立 !
if banana.frozen()
    goat.hit()

goto banana

image125.jpeg

108 关挑战

有的时候,你写在 如果 if 指令里面的判断条件并不会真的发生.

if banana.frozen()
    goat.goto banana
    goat.hit()
goto banana

image126.jpeg

109 关挑战

你可以在一个 for循环中使用 如果 if 条件判断
for b in bananas
    if b.frozen()
        goat.goto b
        goat.hit b
    goto b

image127.jpeg

110 关挑战

#在 loop 循环中使用 if

for b in bananas
    if b.frozen()
        goat.goto(b)
        goat.hit(b)
    monkey.goto b

image128.jpeg

111 关挑战

for b in bananas
    if b.frozen()
        goat.hit(b)
    monkey.goto b

    turtle.step 5

image129.jpeg

112 关挑战

goat.goto banana

image130.jpeg

113 关挑战

goat.step 15

monkey.step 15

image131.jpeg

114 关挑战


if banana.green()
    goat.goto banana

image132.jpeg

115 关挑战

if banana.green()
    say "It's green!"
else
    monkey.goto(banana)

image133.jpeg

116 关挑战

for b in bananas
    if b.green()
        goat.goto b
    else
        monkey.goto b

image134.jpeg

117 关挑战

不管是哪种颜色的香蕉,所有的 结冰香蕉都应该使用 撞击 hit() 指令来去冰, 但是只有山羊会吃掉 绿色的香蕉.

for b in bananas
    goat.goto b
    goat.hit()

    if b.green()
        goat.goto b
    else
        monkey.goto b

image135.jpeg

118 关挑战

for b in bananas
    if b.green()
        goat.goto b
    else
        monkey.goto b

image136.jpeg

119 关挑战

breakBanana = (f) ->
    if f.frozen()
        goat.goto f
        goat.hit f
    if f.green()
        goat.goto f
    else
        monkey.goto f
for b in bananas
    breakBanana b

image137.jpeg

120 关挑战
until tiger.sleeping()
    wait()
goto banana


image138.jpeg

121 关挑战
until bear.sleeping()
    wait()
goto banana


image139.jpeg

122 关挑战

for b in bananas
    until bear.sleeping()
        wait() //#等待,什么?
    goto b

image140.jpeg

123 关挑战

until bear.sleeping() and tiger.sleeping()
    wait()
goto banana

image141.jpeg

124 关挑战

until bear.sleeping() and tiger.sleeping()
    wait()
goto banana

image142.jpeg

125 关挑战

until bear.sleeping() and tiger.sleeping() //#并且...
    wait()

for b in bananas
    goto b

image143.jpeg

126 关挑战

for b in bananas
    until bear.sleeping() and tiger.sleeping() //#并且...
        wait()
    if b.green()
        goat.goto b
    monkey.goto b

image144.jpeg

127 关挑战

waitForSafety = () ->
    until bear.sleeping() and tiger.sleeping()
        wait()

for b in bananas
    waitForSafety()
    goto b

image145.jpeg

128 关挑战

for b in bananas
    until bear.sleeping() and tiger.sleeping()
            wait()
    if b.frozen()
        goat.goto b
        goat.hit()
    until bear.sleeping() and tiger.sleeping()
            wait()
    monkey.goto b


image146.jpeg

129 关挑战

until tiger.sleeping() or tiger.playing()
    wait()
goto banana

image147.jpeg

130 关挑战

或者 or 指令能够让小猴等到许多个判断式其中之一的条件成立了之后,就开始下个行动.
until tiger.sleeping() or tiger.playing()
    wait()
goto banana



image148.jpeg

131 关挑战

//你可以在所有的判断式里面都使用 或者 or 指令
until tiger.sleeping() or tiger.playing()
    wait()

step 10

//#这里等待 <code>wait</code> 熊玩耍或睡着
until bear.sleeping() or bear.playing()
    wait()
step 10

image149.jpeg

132 关挑战

until bear.playing() or bear.sleeping()
    wait()

goat.goto banana

image150.jpeg

133 关挑战
for b in bananas
    until tiger.playing() or tiger.sleeping()
        wait()
    if b.green()
        goat.goto b
    else
        monkey.goto b


image151.jpeg

134 关挑战
for b in bears
    until b.sleeping() or b.playing()
        wait()
    step 10

image152.jpeg

135 关挑战

waitFor = (t) ->
    until t.sleeping() or t.playing()
        wait()

for t in tigers
    for stepper in [monkey, goat]
        waitFor t
        stepper.step 10


image153.jpeg

136 关挑战

//这个开始是否逻辑了。

//not 这个否定指令会把原先是 肯定yes 的结果变成 否定no 的结果。此外,它也会把原先就 否定的no 结果变成 yes 的结果.

if not banana.green()
    //#你只能修改这里的代码:
    goto banana

image154.jpeg

137 关挑战

请试试看使用 烂掉的 rotten() 来识别烂掉的香蕉并且不要靠近它们!

rotten英 [ˈrɒtn]   美 [ˈrɑ:tn]  adj.腐烂的;恶臭的;堕落的;极坏的

for b in bananas
    turnTo b
    if b.rotten()
        say "Yuck!"
    else
        goto b

image155.jpeg

138 关挑战

goto bush
goto bananas[0]

image156.jpeg

139 关挑战

if not banana.rotten()
    goto banana

image157.jpeg

140 关挑战

for b in bananas
    #修复这里的条件:
    if  not b.rotten()
        goto b


image158.jpeg

141 关挑战

for b in bananas
    if not b.rotten()
        goto b
        goto bush

image159.jpeg

142 关挑战

say health()
goto banana

image160.jpeg

143 关挑战

goto bananas[0]
goto bananas[1]

image161.jpeg

144 关挑战

if health() == 40 //#是的,40等于40,得到yes
    goto banana
if health() == 100 //#不是,40不是100,得到no
    goto banana

image162.jpeg

145 关挑战

请待在 休息区 healthZone 以便恢复体力l.
goto healthZone
//#修改这里,它永远到不到1000的数值
until health() == 100
    wait()
goto banana

image163.jpeg

146 关挑战

for b in bananas
    goto healthZone
    //#在这里等待,直到小猴的健康值是100
    until health() == 100
        wait()
    goto b

image164.jpeg

147 关挑战

goto healthZone
until health() ==100
    wait()
goto banana

image165.jpeg

148 关挑战
请使用 小于 < 符号来比较是否一个数值小于另外一个数值.

if health() < 90 #是的,80小于90,得到yes
    goto banana
else
    goto banana

image166.jpeg

149 关挑战

for b in bananas
    goto b
    //#提示: 如果健康值低于70,就需要补充体力啦
    if health() <=70
        goto healthZone
        until health()==100
            wait()

image167.jpeg

150 关挑战

gotoNearestHealth = () ->
    d0 = distanceTo healthZones[0]
    d1 = distanceTo healthZones[1]
    if d0 < d1
        goto healthZones[0]
    else
        goto healthZones[1]

for b in bananas
    goto b
    if health() < 60
        gotoNearestHealth() 
        #在这里等待,直到小猴是健康的!
        until health() ==100
            wait()

image168.jpeg

151 关挑战

使用 返回 return 指令,我们可以把某个 函数 function 的运算结果传送回来.

example = () ->
    return  not banana.rotten()

if example() #example() 函数返回 no
    goto banana
else
    turn 360

image169.jpeg

152 关挑战

通常函数 function 的 返回 return 结果取决于我们如何定义这个函数.

yummy = (x) ->
    return not x.rotten()
for b in bananas
    if yummy b
        goto b

image170.jpeg

153 关挑战

healthy = () ->
    return health() == 100

goto healthZone

until healthy()
    wait()
goto banana

image171.jpeg

154 关挑战

healthy = () ->
    return health() == 100

injured = () ->
    return health() < 70

//#在这里使用 healthy() 函数和 injured() 函数
for b in bananas
    goto b
    if injured()
        goto healthZone
        until healthy()
            wait()

image172.jpeg

155 关挑战

healthy = () ->
    return health() > 99


//#这里应该是正常的,如果你已经修复了 healthy() 函数
goto healthZone
until healthy()
    wait()
goto bush
goto banana

image173.jpeg

156 关挑战

safeFrom = (a) ->
    return a.sleeping() or a.playing() //#或者 or ?

//#另一种调用有参数的函数的用法是使用小括号 '()' 
until safeFrom(tiger)
    wait()

goto banana

image174.jpeg

157 关挑战

safeFrom = (a) ->
    return a.sleeping() or a.playing() //#修复这里吧

until safeFrom(tiger) and safeFrom(bear)
    wait()

goto banana

image175.jpeg

158 关挑战

请写出一个函数 function , 以便让它 返回 return 肯定 yes 或者是 否定 no 的结果.

negative = () ->
    return no //#改成 nopositive = () ->
    return yes //#这段代码是正常的

if negative()
    goto bananas[0]
if positive()
    goto bananas[1]

image176.jpeg

159 关挑战

在一个函数 function 里面,所有写在 返回 return 指令后面的内容都不会被执行.

foo = () ->
    return yes

if foo()
    goto banana
else
    say "nope"

image177.jpeg

160 关挑战
until not crow.watching()
    say "Boo!"
goto banana

image178.jpeg

161 关挑战

//请使用 说话 say 这个指令,直到安全为止.
safe = () ->
    return not crow.watching()

//#使用 say 函数来吓走乌鸦 crow 吧

until safe()
    say "x"
goto banana

image179.jpeg

162 关挑战

safe = () ->
    if crows[0].watching()
        return no
    if crows[1].watching()
        return no
    #如果到达这里意味着两只乌鸦都没有在监控啦
    return yes

#在这里使用 safe() 函数
goto banana


或者
safe = () ->
    if crows[0].watching()  or crows[1].watching() //0号或者1号乌鸦在监视,就是不安全的情况
        return no
    return yes

image180.jpeg

163 关挑战


safe = () ->
    #只有乌鸦监控的时候才返回 yes
    return not( crows[1].watching() or crows[0].watching())

until safe()
    say "Go away!"

for b in bananas

    goto b

image181.jpeg

164 关挑战

safe = () ->
     return  (not crows[0].watching() and not crows[1].watching() and not crows[2].watching())
或者
     return not (crows[0].watching() or crows[1].watching() or crows[2].watching())

until safe()
    say "Go away!"

goto banana

image182.jpeg

165 关挑战

safe = () ->
    for c in crows
        if c.watching()
            return no //#这样是安全的吗?
    //#如果到达这里意味着没有乌鸦在监控啦
    return yes

until safe()
    say "Go away!"

goto banana

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值