Preparation
First, to use Python
the built-in Turtle
drawing library, you need to add the following code before the program:
import turtle
It can also be written like this:
from turtle import *
Let's talk about their differences: when
using import
it, you need to define a variable as a parameter control item, such as:
import turtle
t=turtle.Pen()
# 画图
t.forward(10) # 向右画10像素,详见下表
When using from
, you can directly command the program without redundant statements
from turtle import *
# 画图
forward(10) # 向右画10像素,详见下表
Basic functions and usage
function | Function |
---|---|
forward(x) Shorthand:fd(x) |
Advance the length of pixels, if negative, draw in the opposite direction 右 . x The default forward direction is右 |
backward(x) Shorthand:bk(x) |
Advance the length of pixels, if it is a negative value 左 , draw x in the opposite direction ( 右 ) |
goto(x,y) |
From current position to (x,y) draw line |
circle(r,extent = x) |
draws an arc r with radius and x angle |
left(x) Shorthand:lt(x) |
x turn left |
right(x) Shorthand:rt(x) |
x turn right |
pencolor("red") |
Set the brush color to 红色 (can be modified) |
fillcolor("red") |
set 填充颜色 to red |
bgcolor(black) |
set 背景色 to black |
down() |
pen down function,没有参数 |
up() |
pen up function,没有参数 |
pensize(x) |
Set the brush thickness tox |
speed(x) |
Set the drawing speed to x , the larger the value, the faster it 0 is, the fastest |
done() |
Pause the program, stop the brush drawing, but the drawing form will not be closed until the user closes Turtle the graphical window,没有参数 |
drawing graphics
1. Draw a square
import turtle
t=turtle.Pen()
t.pencolor("orange") # 画笔颜色设置为橙色
for i in range(1,5):
t.fd(50) # 向右画50像素
t.lt(90) # 转90度,画下一条边
2. Draw a triangle
Steps: First draw a side, turn right 120°
, then draw another side, turn right 120°
, and finally draw a side
import turtle
t=turtle.Pen()
t.pencolor("orange") # 画笔颜色设置为橙色
t.fd(150)
t.rt(120)
t.fd(150)
t.rt(120)
t.fd(150)
3. Draw an octagonal star
import turtle
t=turtle.Pen()
t.pencolor("orange")
for x in range(1,9): # 执行8次
t.fd(100)
t.lt(135)
4. Draw an octagon
Key point: rotate after each side is drawn45°
import turtle
t=turtle.Pen()
t.pencolor("orange")
for i in range(1,9): #循环8次
t.forward(50) #画线
t.right(45) #转向
We can also Turtle
draw some interesting graphics
5. Other graphics
#test 1.1
import turtle
t=turtle.Pen()
t.pencolor("orange")
for i in range(1,4):
t.fd(50)
t.rt(120)
t.fd(50)
t.rt(120)
t.fd(50)
import turtle #导入turtle库
t = turtle.Pen()
t.pencolor("orange")
for i in range(5):
t.forward(100) # 向右移动300
t.right(180-180/5) # 180-五角星的内角和/5
t.done()
So far, the graphics we have drawn have only one color, so how can we draw patterns of multiple colors?
We can first define a color list, and take out a color every time we draw 随机
, and we can draw colorful patterns
colors=["red","orange","yellow","green","blue","purple"]
The code to randomly take out the color:
t.pencolor([colors%5])
No.1, colorful five-pointed star
import turtle #导入turtle库
t = turtle.Pen()
colors=["red","orange","yellow","green","blue","purple"]
for i in range(5):
t.pencolor(colors[i%5])
t.forward(100) # 向右移动300
t.right(180-180/5) # 180-五角星的内角和/5
t.done()
No.2, colorful octagonal star
import turtle
t=turtle.Pen()
colors=["red","orange","yellow","green","blue","purple"]
for i in range(1,9): # 执行8次
t.pencolor(colors[i%6])
t.fd(100)
t.lt(135)
No.3, four round mosaic
import turtle
t=turtle.Pen()
colors=["red","green","yellow","blue"]
for i in range(1,100):
t.pencolor(colors[i%4])
t.circle(i) #画圆
t.lt(91)
No.4, rainbow spiral
import turtle
t=turtle.Pen()
colors=["red","orange","yellow","green","blue","purple"]
turtle.bgcolor("black")
for i in range(500000):
t.pencolor(colors[i%6])
t.fd(i)
t.lt(64)
t.width(i/10-1)