turtle库语法元素分析

Python的turtle库是一个直观有趣的图形绘制函数库。turtle(海龟)图形绘制的概念诞生于1969年,并成功运用于LOGO编程语言。由于turtle图形绘制概念十分值观且流行,Python接受了这个概念,形成了一个Python的turtle库,并成为标准库之一。

一、绘图坐标体系

turtle库绘制的图形有一个基本的框架:一个小海龟在坐标系中爬行,其爬行轨迹形成了绘制图形。对于小海龟来说,有“前进“、”后退”、“旋转”等爬行行为,对坐标系的探索也通过“前进方向”、“后退方向”、“左侧方向”和“右侧方向”等小海龟自身角度方位来完成。刚开始绘制时,小海龟位于画布正中央,此处坐标为(0,0),行进方向为水平右方。例如,用如下代码绘制如图所示的坐标体系。

turtle.setup(650,350,200,200)

该代码中使用了turtle.setup()函数,该函数使用方法如下:

turtle.setup(width,height,startx,starty)

作用:设置主窗体的大小和位置。

参数如下:

width:窗口宽度,如果值是整数,表示像素值;如果值是小数,表示窗口宽度与屏幕的比例。

height:窗口高度,如果值是整数,表示像素值;如果值是小数,表示窗口高度与屏幕的比例。

startx:窗口左侧与屏幕左侧的像素距离,如果值是None,窗口位于屏幕水平中央。

starty:窗口顶部与屏幕顶部的像素距离,如果值是None,窗口位于屏幕垂直中央。

二、画笔控制函数

1、turtle.penup()和turtle.pendown()函数

        turtle中的画笔(即小海龟)可以通过一组函数来控制,turtle.penup()和turtle.pendown()是一组,他们分别表示抬起画笔和落下画笔,函数定义如下:

turtle.penup()

别名turtle.pu(),turtle.up()

作用:抬起画笔,之后移动画笔不会绘制图形。

参数:无。

turtle.pendown()

别名turtle.pd(),turtle.down()

作用:落下画笔,之后移动画笔将会绘制图形。

参数:无。

2、turtle.pensize()函数

turtle.pensize()函数用来设置画笔尺寸,函数定义如下:

turtle.pensize(width)

别名turtle.width()

作用:设置画笔宽度,当无参数输入时返回当前画笔宽度。

参数如下:

width:设置的画笔线条宽度,如果值是None或者为空时,则函数返回当前画笔宽度。

3、turtle.pencolor()函数

turtle.pencolor()函数给画笔设置颜色。函数定义如下:

turtle.pencolor(colorstring)或turtle.pencolor((r,g,b))

作用:设置画笔颜色,当无参数输入时返回当前画笔颜色。

参数如下:

colorstring:表示颜色的字符串,例如,“purple”、“red”、“blue”等。

(r,g,b):颜色对于的RGB数值,例如(51,204,140)。

        很多RGB颜色都有固定的英文名字,这些英文名字可以作为colorstring输入到turtle.pencolor()函数中,也可以采用(r,g,b)形式直接输入颜色值。下面是几种典型的RGB颜色:

英文名称 RGB 十六进制 中文名称
white 255 255 255 #FFFFFF 白色
black 0 0 0 #000000 黑色
grey 190 190 190 #BEBEBE 灰色
darkgreen 0 100 0 #006400 深绿色
gold 255 215 0 #FFD700 金色
violet 238 130 238 #EE82EE 紫罗兰色
purple 160 32 240 #A020F0 紫色

三、形状绘制函数

1、turtle.fd()函数

        turtle通过一组函数控制画笔的行进动作,进而绘制形状。turtle.fd()函数最常用来控制画笔向当前行进方向前进一个距离,函数定义如下:

turtle.fd(distance)

别名

turtle.forwardd(distance)

作用:向小海龟当前行进方向前进一个distance距离。

参数如下:

distance:行进距离的像素值,当值为负数时,表示向相反方向行进。

实例2.1中5、14、16行代码:
 

#e2.1DrawPython.py
import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40 * 2/3)

分别表示:向画笔当前行进方向或反方向行进一个距离,进而绘制一条直线。第5行代码配合了第4行抬起画笔函数,因此,它将不绘制一条直线,而是将画笔移动到某个位置。

2、turtle.seth()函数

turtle.seth()函数用来改变画笔绘制方向,函数定义如下:

turtle.seth(to_angle)

别名

turtle.setheading(to_angle)

作用:设置小海龟当前行进方向为to_angle,该角度是绝对方向角度值。

参数如下:

to_angle:角度的整数值。

如图是turtle库的角度坐标体系,供turtle.seth()等函数使用。

        需要注意的是,turtle 库的角度坐标体系以正东向为绝对 0度,这也是小海龟初始爬行方向,正西向为绝对 180 度,这个方向坐标体系是方向的绝对方向体系,与小海龟爬向当前方向无关。因此,可以利用这个绝对坐标体系随时更改小海龟的前进方向。
        实例代码 2.1中第9行将小海龟的前进方向设定为-40度,即320度,之后小海龟向这个方向前进。


3、for 循环语句和 turtle.circle()函数

for i in range(4):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40, 80/2)
turtle.circle(16,180)


        Due to the indentation, the 10th, 11th, and 12th lines in the example code 2.1 are a whole guided by the reserved word for, which is another loop structure called "traversal loop". The loop format of the for statement is as follows:
for i in range(<number of loops>):
<statement block 1> The for loop in line 10 of the example code 2.1 means that the codes in lines 11 and 12 are executed 4 times in a row. The turtle.circle() function is used to draw an arc, and the function definition is as follows: turtle.circle (radius, extent=None) Function: Draw an arc with an extent angle according to the radius radius.
        


The parameters are as follows.
radius: arc radius, when the value is positive, the radius is on the left side of the little turtle, and when the value is negative, the radius is on the right side of the little turtle.
extent: The angle at which the arc is drawn. When no parameter is set or the parameter is set to None, the entire circle is drawn.
In example code 2.1, parameters including angle value, radius value and other parameters are determined according to the style adjustment of the drawing content.

Guess you like

Origin blog.csdn.net/fan18317517352/article/details/122887965