本文列举了常用的18个绘图命令,其中:
窗口设置命令2个;
画笔设置命令7个;
画笔移动命令8个;
其它常用命令1个;
【窗口设置】
1.setup()
例程1:设置窗口尺寸(宽,高),数值单位为像素。import turtle
# 设置窗口尺寸
turtle.setup(600, 600)
turtle.mainloop()
例程2:设置窗口尺寸(宽,高)和位置(X,Y),数值单位为像素。import turtle
# 设置窗口尺寸和位置
turtle.setup(600, 600, 420, 150)
turtle.mainloop()
2. bgcolor()
例程3:设置窗口背景颜色("英文颜色代码")import turtle
# 设置窗口背景颜色
turtle.bgcolor("Black")
turtle.mainloop()
例程4:设置窗口背景颜色("十六进制RGB颜色值")import turtle
# 设置窗口背景颜色
turtle.bgcolor("#000000")
turtle.mainloop()
【画笔设置】
1. pen = turtle.Turtle()
例程5:创建(实例化)并命名一支画笔import turtle
# 创建(实例化)并命名一支画笔
pen = turtle.Turtle()
turtle.mainloop()
2. pencolor()
例程6:设置画笔颜色import turtle
pen = turtle.Turtle()
# 设置画笔颜色
pen.pencolor("DeepSkyBlue")
turtle.mainloop()
3. fillcolor()
例程7:设置图形填充颜色import turtle
pen = turtle.Turtle()
pen.pencolor("DeepSkyBlue")
# 设置图形填充颜色
pen.fillcolor("red")
turtle.mainloop()
4. pensize()/width()
例程8:设置画笔粗细import turtle
pen =turtle.Turtle()
pen.pencolor("DeepSkyBlue")
pen.fillcolor("red")
# 设置画笔粗细
pen.pensize(10)
turtle.mainloop()
5. speed()
例程9:设置绘图速度import turtle
pen = turtle.Turtle()
pen.pencolor("DeepSkyBlue")
pen.fillcolor("red")
pen.pensize(10)
# 设置绘图速度
pen.speed(10)
turtle.mainloop()
6. shape()
例程10:设置画笔形状import turtle
turtle.setup(600, 600, 0, 276)
turtle.bgcolor("Black")
pen = turtle.Turtle()
pen.pencolor("DeepSkyBlue")
pen.fillcolor("red")
pen.pensize(10)
pen.speed(10)
# 设置画笔形状
pen.shape("turtle")
turtle.mainloop()
7. turtlesize()
例程11:设置画笔形状尺寸import turtle
turtle.setup(600, 600, 0, 276)
turtle.bgcolor("Black")
pen = turtle.Turtle()
pen.pencolor("DeepSkyBlue")
pen.fillcolor("red")
pen.pensize(10)
pen.speed(10)
pen.shape("turtle")
# 设置画笔形状尺寸
pen.turtlesize(2)
turtle.mainloop()
【画笔移动】
1 & 2. forward()/fd(), backward()/bk()
例程12:画笔向前移动,画笔向后移动import turtle
pen = turtle.Turtle()
# 画笔向前移动
pen.forward(100)
# 画笔向后移动
pen.backward(200)
turtle.mainloop()
3 & 4. left(), right()
例程13:画笔向左转,画笔向右转import turtle
pen = turtle.Turtle()
pen.forward(100)
# 画笔向右转
pen.right(90)
pen.forward(100)
turtle.mainloop()
5 & 6. up(), down()
例程14:画笔抬起,画笔放下import turtle
pen = turtle.Turtle()
pen.forward(100)
# 画笔抬起
pen.up()
pen.forward(100)
pen.right(90)
# 画笔放下
pen.down()
pen.forward(100)
turtle.mainloop()
7 & 8. bigin_fill(), end_fill()
例程15:开始填充,结束填充import turtle
turtle.setup(600, 600, 0, 276)
turtle.bgcolor("Black")
pen = turtle.Turtle()
pen.pencolor("DeepSkyBlue")
pen.fillcolor("yellow")
pen.pensize(10)
pen.speed(10)
pen.shape("turtle")
pen.turtlesize(2)
pen.begin_fill()# 开始填充
pen.forward(100)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.end_fill()# 结束填充
turtle.mainloop()
【其它命令】
1. mainloop()/done()
例程16:窗口主循环
import turtle
# 窗口主循环
turtle.mainloop()
参考:Python Turtle 标准库官方文档(Python 3.8.6)
https://docs.python.org/zh-cn/3/library/turtle.html#turtle.write