turtle是python画图的一个工具,原理和生活中用笔画图一样。
import turtle
turtle.setup(800, 600)
# 1)设置笔的颜色 - 画出来的线的颜色,颜色值是0~1的rgb值
turtle.pencolor(1, 0, 0)
# 2)设置线的宽度(粗细)
turtle.width(5)
# 3)设置笔的移动速度:1~10(1最小,10最大,0比10还大)
turtle.speed(1)
# 1)前进和后退(笔默认的方向是水平向右的)
# 前进:forward(距离)、fd(距离)
turtle.forward(100)
# 后退:back(距离)、bk(距离)
turtle.pencolor(0, 1, 0)
turtle.width(2)
# 后退
turtle.back(200)
# 2)移动到指定点
# goto(x坐标, y坐标)
turtle.goto(100, 200)
# 3)回到初始状态(初始位置(0,0)和初始角度(水平向右))
turtle.home()
turtle.mainloop()
import turtle
turtle.setup(800, 800)
turtle.pencolor(1, 0, 0)
turtle.width(3)
# 1)left(角度) - 从当前方向上向左旋转指定角度
# 2)right(角度) - 从当前方向上右左旋转指定角度
# 3)setheading(角度) - 设置绝对角度
turtle.left(90)
turtle.right(90)
# 绝对角度向上
turtle.setheading(90)
turtle.forward(200)
# 笔默认是放下状态(和画布接触的状态)
# turtle.up() - 笔抬起
turtle.up()
turtle.goto(100, 200)
# turtle.down()笔放下
turtle.down()
turtle.forward(100)
turtle.mainloop()
import turtle
turtle.setup(800, 800)
turtle.pencolor(1, 0, 0)
turtle.width(3)
第一步:先用线画出需要填充部分的框(先画轮廓)
第二步:在画轮廓的代码前设置填充颜色和开始填充
第三步:在画轮廓的代码的后面结束填充
turtle.fillcolor(1, 0, 0)
turtle.begin_fill()
# ----下面是画轮廓的代码-----
turtle.forward(100)
turtle.left(120)
turtle.forward(100)
turtle.home()
# ------上面是画轮廓的代码-----
turtle.end_fill()
练习:右边添加一个绿色的框
turtle.up()
turtle.goto(130, 0)
turtle.down()
turtle.pencolor(0, 1, 0)
turtle.fillcolor(0, 1, 0)
turtle.begin_fill()
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.end_fill()
turtle.mainloop()
import turtle
turtle.setup(800, 800)
turtle.width(3)
turtle.pencolor(1,0,1)
turtle.speed(5)
for _ in range(180):
turtle.forward(1)
turtle.left(1)
turtle.setheading(315)
for _ in range(80):
turtle.forward(1)
turtle.left(2)
turtle.setheading(90)
for _ in range(90):
turtle.forward(1)
turtle.right(2)
turtle.up()
turtle.goto(50,50)
turtle.down()
for _ in range(180):
turtle.forward(2)
turtle.left(1)
turtle.setheading(360)
for _ in range(360):
turtle.forward(1)
turtle.left(1)
turtle.setheading(180)
for _ in range(180):
turtle.forward(2)
turtle.left(1)
turtle.up()
turtle.goto(30, 60)
turtle.down()
for _ in range(180):
turtle.forward(1)
turtle.left(2)
# 1)turtle.circle(半径, 角度)
# 角度默认值是360
turtle.circle(100, 90)
turtle.setheading(-90)
turtle.circle(80, 120)
turtle.up()
turtle.goto(-100,0)
turtle.down()
# 2)画完整的圆
# turtle.dot(直径)
turtle.pencolor(0, 1, 0)
turtle.dot(100)
turtle.mainloop()
import turtle
turtle.setup(800, 800)
turtle.width(5)
for _ in range(36):
turtle.forward(150)
turtle.goto(0,0)
turtle.left(10)
from random import randint
for _ in range(36):
turtle.fillcolor(randint(0, 255)/255, randint(0, 255)/255, randint(0, 255)/255)
turtle.begin_fill()
turtle.forward(100)
turtle.left(45)
turtle.forward(100)
turtle.goto(0,0)
turtle.end_fill()
turtle.left(10)
练习:重复的三角形(起点不同)
from random import randint
x = 0
y = 0
for _ in range(6):
turtle.fillcolor(randint(0, 255) / 255, randint(0, 255) / 255, randint(0, 255) / 255)
turtle.begin_fill()
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.goto(x, y)
turtle.end_fill()
turtle.setheading(0)
turtle.up()
x -= 50
y -= 50
turtle.goto(x, y)
turtle.down()