当前位置:网站首页>Analysis of grammar elements in turtle Library
Analysis of grammar elements in turtle Library
2022-07-06 05:37:00 【Rookie 1!!】
Python Of turtle Library is an intuitive and interesting graphics rendering function library .turtle( Sea turtle ) The concept of graphic drawing was born in 1969 year , And successfully applied to LOGO programing language . because turtle The concept of graphic drawing is very valuable and popular ,Python Accepted the concept , Formed a Python Of turtle library , And become one of the standard libraries .
One 、 Drawing coordinate system
turtle The graphics drawn by the library have a basic framework : A little turtle crawls in the coordinate system , Its crawling track forms a drawing pattern . For little turtles , Yes “ Forward “、” back off ”、“ rotate ” Wait for crawling behavior , The exploration of the coordinate system is also through “ The way forward ”、“ Backward direction ”、“ Left direction ” and “ Right side direction ” Wait for the little turtle to complete its own angle and orientation . At the beginning of drawing , The little turtle is in the middle of the canvas , The coordinates here are (0,0), The direction of travel is horizontal to the right . for example , Use the following code to draw the coordinate system shown in the figure .
turtle.setup(650,350,200,200)
This code uses turtle.setup() function , This function is used as follows :
turtle.setup(width,height,startx,starty)effect : Set the size and position of the main form .
The parameters are as follows :
width: Window width , If the value is an integer , Represents the pixel value ; If the value is decimal , Represents the ratio of window width to screen .
height: Window height , If the value is an integer , Represents the pixel value ; If the value is decimal , Represents the ratio of window height to screen .
startx: The pixel distance between the left side of the window and the left side of the screen , If the value is None, The window is located in the horizontal center of the screen .
starty: The pixel distance between the top of the window and the top of the screen , If the value is None, The window is located in the vertical center of the screen .
Two 、 Brush control function
1、turtle.penup() and turtle.pendown() function
turtle The paintbrush in ( Little Turtle ) Can be controlled by a set of functions ,turtle.penup() and turtle.pendown() It's a group. , They said to lift the brush and drop it respectively , The function is defined as follows :
turtle.penup()
Alias turtle.pu(),turtle.up()
effect : Lift up the brush. , After that, moving the brush will not draw graphics .
Parameters : nothing .
turtle.pendown()
Alias turtle.pd(),turtle.down()
effect : Drop the brush , Then moving the brush will draw the figure .
Parameters : nothing .
2、turtle.pensize() function
turtle.pensize() Function to set the brush size , The function is defined as follows :
turtle.pensize(width)
Alias turtle.width()
effect : Set brush width , Returns the current brush width when there is no parameter input .
The parameters are as follows :
width: Set the brush line width , If the value is None Or empty , The function returns the current brush width .
3、turtle.pencolor() function
turtle.pencolor() Function to set the color of the brush . The function is defined as follows :
turtle.pencolor(colorstring) or turtle.pencolor((r,g,b))
effect : Set brush color , Returns the current brush color when there is no parameter input .
The parameters are as follows :
colorstring: A string representing a color , for example ,“purple”、“red”、“blue” etc. .
(r,g,b): Color for RGB The number , for example (51,204,140).
quite a lot RGB Colors have fixed English names , These English names can be used as colorstring Input to turtle.pencolor() Function , You can also use (r,g,b) Enter the color value directly in the form . Here are some typical RGB Color :
| English name | RGB | Hexadecimal | Chinese name |
| white | 255 255 255 | #FFFFFF | white |
| black | 0 0 0 | #000000 | black |
| grey | 190 190 190 | #BEBEBE | gray |
| darkgreen | 0 100 0 | #006400 | Dark green |
| gold | 255 215 0 | #FFD700 | golden |
| violet | 238 130 238 | #EE82EE | Violet |
| purple | 160 32 240 | #A020F0 | violet |
3、 ... and 、 Shape drawing function
1、turtle.fd() function
turtle Through a set of functions to control the travel action of the brush , Then draw the shape .turtle.fd() Function is most commonly used to control the brush to advance a distance in the current direction of travel , The function is defined as follows :
turtle.fd(distance)
Alias
turtle.forwardd(distance)
effect : Advance one in the current direction of the little turtle distance distance .
The parameters are as follows :
distance: Pixel value of travel distance , When the value is negative , It means moving in the opposite direction .
example 2.1 in 5、14、16 Line code :
#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)
respectively : Travel a distance in the current direction or the opposite direction of the brush , Then draw a straight line . The first 5 Line code matches the 4 Line lift brush function , therefore , It will not draw a straight line , Instead, move the brush to a certain position .
2、turtle.seth() function
turtle.seth() Function to change the drawing direction of the brush , The function is defined as follows :
turtle.seth(to_angle)
Alias
turtle.setheading(to_angle)
effect : Set the current travel direction of the little turtle to to_angle, This angle is the absolute direction angle value .
The parameters are as follows :
to_angle: The integer value of the angle .
As shown in the figure turtle The angular coordinate system of the Library , for turtle.seth() And so on .
It should be noted that ,turtle The angular coordinate system of the library takes the east direction as the absolute 0 degree , This is also the initial crawling direction of the little turtle , Due west is absolute 180 degree , This directional coordinate system is the absolute directional system of the direction , It has nothing to do with the little turtle crawling in the current direction . therefore , This absolute coordinate system can be used to change the direction of the little turtle at any time .
The sample code 2.1 pass the civil examinations 9 Set the direction of the little turtle to -40 degree , namely 320 degree , Then the little turtle moved in this direction .
3、for Loop statements and turtle.circle() function
for i in range(4):
turtle.circle(40,80)
turtle.circle(-40,80)
turtle.circle(40, 80/2)
turtle.circle(16,180)
Due to indentation , The sample code 2.1 pass the civil examinations 10、11、12 Line is a reserved word for The whole of guidance , This is another circular structure , be called “ Traversal cycle ”.for The circulation format of language direction is as follows :
for i in range(< cycles >):
< Sentence block 1>
The sample code 2.1 pass the civil examinations 10 Yes for The loop represents the second 11、12 Lines of code execute continuously 4 Time .
turtle.circle() Function is used to draw an arc , The function is defined as follows :
turtle.circle (radius, extent=None)
effect : According to radius radius draw extent Arc of angle .
The parameters are as follows .
radius: Arc radius , When the value is positive , The radius is to the left of the little turtle , When the value is negative , The radius is to the right of the little turtle .
extent: The angle at which the arc is drawn , When the parameter is not set or the parameter is set to None when , Draw the entire circle .
The sample code 2.1 Each western number in the middle includes the angle value 、 The radius value and other parameters are determined according to the style adjustment of the drawing content .
边栏推荐
- Select knowledge points of structure
- What impact will frequent job hopping have on your career?
- Modbus协议通信异常
- Application Security Series 37: log injection
- 【LeetCode】18、四数之和
- What is independent IP and how about independent IP host?
- 自建DNS服务器,客户端打开网页慢,解决办法
- Installation de la Bibliothèque de processus PDK - csmc
- Unity Vector3. Use and calculation principle of reflect
- Zoom and pan image in Photoshop 2022
猜你喜欢
随机推荐
备忘一下jvxetable的各种数据集获取方法
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
jdbc使用call调用存储过程报错
Vulhub vulnerability recurrence 68_ ThinkPHP
Web Security (V) what is a session? Why do I need a session?
Game push image / table /cv/nlp, multi-threaded start
[machine learning notes] univariate linear regression principle, formula and code implementation
Pickle and savez_ Compressed compressed volume comparison
In 2022, we must enter the big factory as soon as possible
B站刘二大人-线性回归 Pytorch
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Installation de la Bibliothèque de processus PDK - csmc
Solution of QT TCP packet sticking
Questions d'examen écrit classiques du pointeur
图数据库ONgDB Release v-1.0.3
注释、接续、转义等符号
大型网站如何选择比较好的云主机服务商?
04. Project blog log
应用安全系列之三十七:日志注入
05. Security of blog project








![[JVM] [Chapter 17] [garbage collector]](/img/f4/e6ff0e3edccf23399ec12b7913749a.jpg)
