Lesson 17: Lawn mower
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Cut the grass
- Robot lawn mowers cut the grass in stripes: up the lawn, move over a little, back down, and again.
- Here the pen is the blade: every line the robot draws is grass it has cut.
- The lawns have hedges round them and different sizes, so the robot must use its distance sensor to know where to turn and when to stop.
Words for this lesson
| Word | Meaning |
|---|---|
| lawn | an area of short grass |
| hedge | a line of bushes round a garden; here a wall |
| stripe | one long line of cut grass |
| break | leave a loop at once |
| while True | a loop that runs until a break |
English
The pattern
| Stripe | Drive | At the hedge, turn | Then |
|---|---|---|---|
| 1 | up | right | 200 mm, right again |
| 2 | down | left | 200 mm, left again |
| 3 | up | right | 200 mm, right again |
| … | … | … | until there is no room: stop |
- The turns go right, left, right, left. One variable,
side, keeps which way to turn next. - 200 mm between stripes: a lawn 800 mm wide gets 4 stripes, a lawn 1400 mm wide gets 7.
English
Swap two values
from vexcode_vr import *
def main():
side = RIGHT
for stripe in range(4):
if side == RIGHT:
brain.print("right")
side = LEFT
else:
brain.print("left")
side = RIGHT
brain.new_line()
vr_thread(main)
- The brain prints right, left, right, left: each time round,
sidechanges to the other value.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The robot is a lawn mower, and the pen is its blade. The starter mows one stripe up to the hedge. Then move over: turn right, drive 200 mm, turn right again, and mow back down to the other hedge. The lawns have different lengths, so use the distance sensor both ways.
点击“运行”查看此处输出。
难度 5/5 · 今日首次完成可获 400 金币及少量额外奖励
Now mow the whole lawn, stripe after stripe. The turns change side each time: right at the top, left at the bottom. Keep the side in the variable side. After you turn, if the side hedge is less than 200 mm away, the lawn is finished: break out of the loop and print Done!.
点击“运行”查看此处输出。