您的位置:首页 > 编程语言 > Python开发

Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 7. 条件循环

2017-12-05 18:51 1061 查看
条件循环能够让程序在条件成立时(即为真时)重复执行循环体中的语句。如果条件一直成立(即永远不会为假),则循环会一直进行下去,不会停止。如果初始时,条件不成立,则循环 1 次也不会执行。Python 中条件循环使用 while 关键字来实现。

看下面这段示例代码,

1 import turtle
2
3
4 turtle.shape("turtle")
5 turtle.speed(0)
6
7
8 def forward(max_distance):
9     distance = 0
10     step = 1
11     while distance < max_distance:
12         turtle.forward(step)
13         distance += step
14
15
16 forward(100)
17
18
19 turtle.exitonclick()


函数 forward 实现的功能和原版的 turtle.forward 是一样的,但这里我们是借助条件循环来实现的。



第 5 行 turtle.speed(0) 设置海龟爬行的速度。速度的取值为 [0, 10]。当取值在 [1, 10] 之间时,数值越大,绘图的速度就越快。当取值为 0 时,海龟将以最快的速度绘图。这里,我们让海龟以最快的速度爬行。因为,在我们自定义的 forward 函数中(第 8-13 行),我们要让海龟移动很多步,但每次只移动 1 个很小的距离,为了尽快看到最终绘图的效果,我们得让海龟爬得快一些。

第 8 行 def forward(max_distance): 声明 forward 函数,它有 1 个参数 max_distance。这个函数要实现的功能是,让海龟一直爬行,直到海龟总的爬行距离达到(或超过) max_distance 时才停止爬行。

第 9 行 distance = 0 变量 distance 用于记录当前海龟已经累计爬行了多远的距离。初始时,海龟还没有开始爬行,因此 distance 的初始值为 0。

第 10 行 step = 1 设置海龟爬行的步长,即每 1 步要爬多远的距离。

第 11-13 行是用 while 语句实现的条件循环,其中第 11 行, while distance < max_distance: 指明了循环条件为 distance < max_distance 即,当海龟累计爬行距离 (distance) 小于规定的最大爬行距离 (max_distance) 时,执行循环体中的语句(第 12, 13 行)。

第 12, 13 行是循环体,其中第 12 行 turtle.forward(step) 让海龟以指定步长 step 前进一小段距离。第 13 行 distance += step 这是一种简写的形式,它等价于 distance = distance + step ,即将海龟刚才(第 12 行)爬行的那一小段距离累加到 distance 变量中。类似 for 循环,while 循环的循环体(第 12, 13 行)也要比 while 语句所在的行(第 11 行),向右缩进一些,通常我们用连续的 4 个空格来表示一级的缩进。

不过,现在的 forward 跟原版的 turtle.forward 效果是一样的,没啥意思,咪博士把代码改进一下,让它变得好玩一些。

1 import turtle
2
3
4 turtle.shape("turtle")
5 turtle.speed(0)
6
7
8 def forward(max_distance, max_radius):
9     distance = 0
10     step = 1
11     while distance < max_distance:
12         if turtle.distance(0,0) >= max_radius:
13             angle = turtle.towards(0,0)
14             turtle.setheading(angle)
15         turtle.forward(step)
16         distance += step
17
18
19 forward(550, 100)
20
21
22 turtle.exitonclick()


再次运行程序,你会看到海龟在一条直线上折返爬行。



第 8 行 def forward(max_distance, max_radius): 我们为 forward 函数引入了一个新的参数 max_radius。后面,我们要实现的功能是当海龟离开原点的距离达到(或超过) max_radius 时,就调头往回爬,即往复爬行。

实现往复爬行对应的代码在第 12-14 行。

第 12 行 if turtle.distance(0,0) >= max_radius: ,其中 turtle.distance(0,0) 返回的是海龟与原点 (0, 0) 的距离。因此,当海龟与原点的距离达到或超过 max_radius 时,if 条件成立,程序将执行相应的语句(第 13, 14 行)。

第 13 行 angle = turtle.towards(0,0) ,其中 turtle.towards(0,0) 返回的是海龟朝向原点 (0, 0) 的角度。计算角度时,水平向右为 0 度,逆时针方向为角度增大的方向。

第 14 行 turtle.setheading(angle) 直接设定海龟的朝向,由于前面(第 13 行),将 angle 的值设置为海龟朝向原点的角度,所以这里经过 setheading 设置之后,海龟将朝向原点的方向。因此,后面再调用 turtle.forward (第 15 行),海龟就会往原点的方向移动,即往回爬行。

现在,海龟能够在一条直线上往复爬行,虽然比之前改进了一些,但还是显得比较单调。接下来,咪博士再进一步改进代码,让它变得更生动一些。

1 import turtle
2 import random
3
4
5 turtle.shape("turtle")
6 turtle.speed(0)
7
8
9 def forward(max_distance, max_radius):
10     distance = 0
11     step = 1
12     while distance < max_distance:
13         if turtle.distance(0,0) >= max_radius:
14             angle = turtle.towards(0,0) + random.randint(-45, 45)
15             turtle.setheading(angle)
16         turtle.forward(step)
17         distance += step
18
19
20 forward(10000, 100)
21
22
23 turtle.exitonclick()


运行上面的代码,你会看到海龟随机地往返爬行。刚开始,你看不出什么规律,但等它爬行一段时间之后,你会发现海龟实际上是被限制在一个圆圈当中运动。



第 2 行 import random 导入 random 模块,用于生成随机数。

第 14 行 angle = turtle.towards(0,0) + random.randint(-45, 45) 其中的 random.randint(-45, 45) 用于产生 [-45, 45] 之间的随机整数,将这个随机整数加到朝向原点的角度上,这样海龟就不再精确地朝向原点,而是有一个随机波动的角度。因此,海龟就不再精确地按原路返回,从而摆脱直线的束缚。

但是,原来的 2 个约束依然存在:

海龟离开原点的距离不能超过 max_radius(第 13 行),所以它被限制在一个圆圈中运动

海龟总的运动距离不能超过 max_distance (第 12 行),所以它最终会停止运动

原文链接:http://www.ipaomi.com/2017/11/28/python-零基础-快速入门-趣味教程-咪博士-海龟绘图-turtle-7-条/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐