- Meet Turtle
Turtle is a renderer
Built based on the underlying graphics programming structure (API), mainly used for scene construction and 3D object rendering (3D games, virtual scenes, etc.)
Turtle is a form program
Turtle is a very popular function library for drawing images in the Python language. Imagine a little turtle starts at the origin (0,0) of a coordinate system with the horizontal axis as x and the vertical axis as y. It follows a set of function instructions. Control, moves in this plane coordinate system, thereby drawing graphics on the path it crawls
Graphical user interface (GUI) is a computer operation user interface displayed graphically. Turtle is a very standard GUI application.
- related functions
- Canvas settings
The canvas is the drawing area expanded by the turtle. Its size and initial position can be set. There are two commonly used canvas methods.
turtle.screensize(800,600,"red"): Set the canvas size (width, height, background color), default 800*600, white
tuetle.setup(500,500,0,0): Not only can you set the width and height, but you can also set the x and y coordinates when the program is running.
- Brush movement commands
turtle.forward(distance): Move distance pixel length to the current brush direction, default to right
turtle.backward(distance): Move distance pixel length in the opposite direction of the current brush
turtle.right(degree): move degree° clockwise
turtle.left(degree): move counterclockwise degree°
turtle.pendown(): Draw graphics when moving, the default is also drawing
turtle.goto(x,y): Move the brush to the position where the coordinates are x, y
turtle.penup(): Lift the pen and move it without drawing graphics. It is used to draw in another place.
tuetle.circle(): Draw a circle with a positive (negative) radius, which means the center of the circle is to the left (right) of the brush to draw a circle.
setx(): Move the current x-axis to the specified position
sety(): Move the current y-axis to the specified position
turtle.speed(): Controls the speed of brush drawing (1-10)
sentheading(angle): Set the current heading angle
home():设置当前画笔位置为原点,朝向东
dot():绘制一个指定直径和颜色的圆点
- 画笔控制命令
turtle.pensize():设置线条粗细
turtle.fillcolor(colorstring):绘制图形的填充颜色
turtle.color(color1,color2):同时设置pencolor=color1,fillcolor=color2,要在画之前,第一个是画笔颜色,第二个是填充颜色
turtle.begin_fill():准备开始填充
turtle.end_fill():填充完成
turtle.filling():返回当前是否在填充状态
turtle.hideturtle():隐藏画笔的turtle形状
turtle.showturtle():显示画笔的turtle形状
- 全局控制命令
turtle.clear():清空当前turtle窗口,但是turtle的位置和状态不变
turtle.reset():清空窗口,重置turtle状态为起始状态
turtlr.undo():撤销上一个turtle动作
turtle.isvisivle():返回当前turtle是否可见
stamp():复制当前图形
turtle.write(s [,font=("font-name",font_size,"font_type")]):写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项
- 其他命令
turtle.mainloop():启动事件循环 -调用Tkinter的mainloop函数。
turtle.done():表示所有工作已经做完了(一般在所有代码之后,放在最后一行)
turtle.delay(delay=None):设置或返回以毫秒为单位的绘图延迟。
turtle.begin_poly():开始记录多边形的顶点,当前的乌龟位置是多边形的第一个顶点
turtle.end_poly():停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连
turtle.get_poly():返回最后记录的多边形
- 案例
- 爱的魔力
代码
import turtle as t
import random
t.speed(0)
for i in range(50):
t.circle(80)
t.right(100)
t.done()
- 奥运五环
代码
import turtle as t
import random
# 圆环
# 奥运五环
t.pensize(9)
# 蓝环
t.color('blue')
t.circle(75)
t.penup()
t.forward(150)
t.pendown()
# 黑环
t.color('black')
t.circle(75)
t.penup()
t.forward(150)
t.pendown()
# 红环
t.color('red')
t.circle(75)
t.penup()
t.goto(220, -100)
t.pendown()
# 黄环
t.color('yellow')
t.circle(75)
t.penup()
t.goto(100, -100)
t.pendown()
# 绿环
t.color('green')
t.circle(75)
# 字体
t.color('black')
t.penup()
t.goto(50, 200)
t.pendown()
t.write('北京 2022', font=('YouYuan', 32))
t.done()
- 美队盾牌
代码
import turtle as t
import random
#美国盾牌
t.penup()
t.goto(0,-200)
t.pendown()
t.color('red')
t.begin_fill()
t.circle(200)
t.end_fill()
t.penup()
t.goto(0, -150)
t.pendown()
t.color('white')
t.begin_fill()
t.circle(150)
t.end_fill()
t.penup()
t.goto(0, -100)
t.pendown()
t.color('red')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(0, -60)
t.pendown()
t.color('blue')
t.begin_fill()
t.circle(60)
t.end_fill()
t.penup()
t.goto(-50, 18)
t.pendown()
t.color('white')
t.begin_fill()
for i in range(5):
t.forward(105)
t.right(144)
t.end_fill()
t.done()
- 彩球飘飘
代码
import turtle as t
import random
# 彩旗飘飘
t.colormode(255)
for i in range(20):
# 设置RGB值,随机生成
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
# 设置气球随机位置,随机生成x,y坐标
x = random.randint(-400, 400)
y = random.randint(-100, 300)
# 移动到指定的位置
t.penup()
t.goto(x, y)
t.pendown()
# 设置填充颜色
t.color(red, green, blue)
# 填充和画圆
t.begin_fill()
t.circle(30)
t.end_fill()
# 画线
t.right(90)
t.forward(30)
t.left(90)
t.done()
- 夜空繁星
代码
import turtle as t
import random
if __name__ == '__main__':
# 繁星漫天
# 夜空
t.speed(0)
t.screensize(800, 600, 'black')
t.colormode(255)
t.pensize(250)
for i in range(10):
t.goto(-500, 400 - i * 100)
t.color(i * 20, i * 20, i * 20)
t.forward(1000)
# 星星
for j in range(25):
x = random.randint(-400, 400)
y = random.randint(0, 400)
size = random.randint(5, 20)
t.color('yellow')
t.penup()
t.goto(x, y)
t.pendown()
t.pensize(3)
t.begin_fill()
for i in range(4):
t.forward(size)
t.left(30)
t.forward(size)
t.right(120)
t.end_fill()
t.done()