今天介绍Python中的一个random模块,使用这个模块,我们可以在海龟屏幕上绘制随机的形状,或者在海龟屏幕上的随机位置绘制图形。
绘制随机大小和颜色的螺旋线随机分布在海龟屏幕上
import turtle as t
import random # 导入随机模块
t.speed(0)
t.bgcolor('black')
# 颜色列表
colors = ['red','yellow','green','blue','gray','purple','orange']
for m in range(50): # 程序执行50次
t.pencolor(random.choice(colors)) # 随机选择一种颜色
size = random.randint(10,40) # 10-40之间随机整数
# 随机获得屏幕宽度和高度的值
x = random.randrange(-t.window_width()//2,
t.window_width()//2)
y = random.randrange(-t.window_height()//2,
t.window_height()//2)
t.pu() # 抬笔
t.setpos(x,y) # 海龟从原点(0,0)移动到的位置
t.pd() # 落笔
for x in range(size):
t.fd(x*2)
t.lt(90)
在海龟屏幕上的随机位置,绘制了随机颜色和大小的螺旋线。
一、导入random模块
import random # 导入随机模块
二、从颜色列表中获取任意的颜色
# 颜色列表
colors = ['red','yellow','green','blue','gray','purple','orange']
t.pencolor(random.choice(colors)) # 随机选择一种颜色
random.choice(list)函数接受一个列表或集合作为参数,例如list,返回从该列表中随机选取的一个元素。
>>> import random # 导入random模块
>>> colors = ['red','yellow','green','blue','gray','purple','orange']
>>> random.choice(colors)
'green'
>>> random.choice(colors)
'blue'
>>> random.choice(colors)
'yellow'
将我们定义的颜色列表colors作为参数传递给random.choice()函数,每次都会从列表中返回一个颜色。
三、绘制随机大小的图形
size = random.randint(10,40) # 10-40之间随机整数
random.randint(最小值,最大值)函数用于生成范围在指定的最小值和最大值之间的一个随机的整数,产生的随机数包括指定的最小值和最大值
shell:
>>> random.randint(1,10)
10
>>> random.randint(1,10)
7
>>> random.randint(1,10)
8
我们使用random.randint(10,40)获取一个螺旋线边数随机值,然后赋值给变量size。
使用for循环遍历size的值,然后绘制出螺旋线
for x in range(size):
t.fd(x*2)
t.lt(90)
四、如何在屏幕上随机绘制出现
获取位置坐标
之前我们介绍过海龟屏幕坐标,是一个二维坐标,x轴和y轴。一开始海龟位于屏幕中心原点(0,0)。
海龟在屏幕上的每一个位置都用坐标点来表示,即(x,y)。x代表水平方向,从原点开始,向左或向右;y代表竖直方向,从原点开始,向上或向下。
turtle.setpos(x,y) 使海龟从原来的位置移动到另外一个新的位置(x,y)
为了避免在移动到新的位置过程中绘制直线,需要在每次开始移动之前将海龟钢笔抬起来,移动到新的位置之后,在将海龟钢笔放下来。
t.pu() # 抬笔
t.setpos(x,y) # 海龟从原点(0,0)移动到的位置
t.pd() # 落笔
setpos()函数将一对随机数组合成(x,y)坐标,使我们可以在不同的位置绘制螺旋线。那如何确定随机数字的范围呢?
画布有多大
我们绘制的图形是要放置在海龟屏幕上的随机位置,但是我们还要保证我们所选择的位置是在海龟窗口可见的位置,不能够超过海龟窗口的宽度和高度。所以我们需要知道画布有多大。
turtle.window_width()获取海龟窗口的宽度
turtle.window_height()获取海龟窗口的高度
shell:
import turtle as t
>>> t.window_width() # 海龟窗口的宽度
768
>>> t.window_height() # 海龟窗口的高度
648
所以我们x坐标的取值范围-t.window_width()//2到t.window_width()//2
y坐标的取值范围-t.window_height()//2到t.window_height()//2
// 操作符表示整除,确保在除以2的时候会得到一个整数结果
randrange()函数生成指定范围之内的一个随机数
# 随机获得屏幕宽度和高度的值
x = random.randrange(-t.window_width()//2,
t.window_width()//2)
y = random.randrange(-t.window_height()//2,
t.window_height()//2)
这样使用randrange()函数生成一对(x,y)坐标值,我们在可见的窗口中绘制螺旋线。
镜面效果图形
在海龟屏幕上有4个点,A(3,3)、B(-3,3)、C(-3,-3)和D(3,-3)。假如垂直的y轴是镜子,那么A和B,C和D都彼此是对方的镜面反射图像;假如垂直的x轴是镜子,那么A和D,B和C都彼此是对方的镜面反射图像。
根据这个原理,只要我们确定一个方向的坐标,就可以确定其他位置的对应反射位置坐标。
在屏幕的右上方选择一个随机位置(x,y),并在那里绘制一条螺旋线;
在屏幕的左上方的(-x,y)处,绘制相同的螺旋线;
在屏幕的左下方的(-x,-y)处,绘制相同的螺旋线;
在屏幕的右下方的(x,-y)处,绘制相同的螺旋线。
代码:
import turtle as t
import random # 导入随机模块
t.speed(0)
t.bgcolor('black')
# 颜色列表
colors = ['red','yellow','green','blue','gray','purple','orange']
for m in range(50): # 程序执行50次
t.pencolor(random.choice(colors)) # 随机选择一种颜色
size = random.randint(10,40) # 10-40之间随机整数
# 记录海龟所朝向的方向
angle = t.heading()
# 随机获得屏幕宽度和高度的值
x = random.randrange(0,t.window_width()//2) # 选择x轴右半部分
y = random.randrange(0,t.window_height()//2) # 选择y轴上半部分
#1 屏幕右上方坐标(x,y)
t.penup()
t.setpos(x,y)
t.pendown()
for m in range(size):
t.forward(m*2)
t.left(91)
#2 屏幕左上方坐标(-x,y)
t.penup()
t.setpos(-x,y)
t.pendown()
t.setheading(180-angle) # 设置角度
for m in range(size):
t.forward(m*2)
t.right(91)
#3 屏幕左下方坐标(-x,-y)
t.penup()
t.setpos(-x,-y)
t.pendown()
t.setheading(angle-180) # 设置角度
for m in range(size):
t.forward(m*2)
t.left(91)
#4 屏幕右下方坐标(x,-y)
t.penup()
t.setpos(x,-y)
t.pendown()
t.setheading(360-angle) # 设置角度
for m in range(size):
t.forward(m*2)
t.right(91)
turtle.heading()返回海龟当前的朝向
turtle.setheading(to_angle)设置海龟的朝向为 to_angle
更多内容可以关注微信公众号【陪孩子一起学编程】