目录
一、turtle简介
二、绘图坐标体系
三、笔画控制函数
四、形状绘制函数
五、颜色填充
六、浪漫樱花代码实例
七、五角星代码实例
八、彩虹玫瑰代码实例
九、谢尔宾斯基三角形代码实例
一、turtle简介
turtle:是3.6版本中新推出的绘图工具库,是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
# 安装turtlepip install turtle
二、绘图坐标体系
import turtle# turtle.setup(width,height,startx,starty)# width,height:为主窗体的宽和高# startx,starty:为窗口距离左侧与屏幕左侧像素距离和窗口顶部与屏幕顶部的像素距离。turtle.setup(300, 300, 300, 100)
三、笔画控制函数
turtle.penup() # 抬起画笔turtle.pendown() # 放下画笔turtle.pensize() # 设置画笔尺寸turtle.pencolor() # 设置画笔颜色
四、形状绘制函数
turtle.fd() # 朝小乌龟当前方向前进turtle.bk() # 朝小乌龟当前方向的反方向前进turtle.left() # 画笔方向向左转,基于当前画笔方向turtle.right() # 画笔方向向右转,基于当前画笔方向turtle.seth() # 在平行于X轴的基础上,改变画笔绘制方向turtle.circle(radius, extent) # 根据半径radius绘制extent角度的弧形turtle.hideturtle() # 绘图结束后隐藏小乌龟turtle.clear() # 清空画板turtle.reset() # 清空画板,并让小乌龟回到初始位置
五、颜色填充
# 成对使用,填充颜色。turtle.begin_fill()turtle.end_fill()
六、浪漫樱花代码实例
# @Author: xiaofang# @Email: 983770299@# @Date: Create in /07/01 19:45# @Description: turtle绘制樱花树import turtleimport randomimport time# 画樱花的躯干(60,t)def cherry_tree(branch, t):time.sleep(0.0005)if branch > 3:if 8 <= branch <= 12:if random.randint(0, 2) == 0:t.color('snow') # 白else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 3)elif branch < 8:if random.randint(0, 1) == 0:t.color('snow')else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 2)else:t.color('sienna') # 赭(zhě)色t.pensize(branch / 10) # 6t.forward(branch)a = 1.5 * random.random()t.right(20 * a)b = 1.5 * random.random()cherry_tree(branch - 10 * b, t)t.left(40 * a)cherry_tree(branch - 10 * b, t)t.right(20 * a)t.up()t.backward(branch)t.down()# 掉落的花瓣def cherry_fall_petal(m, t):for i in range(m):a = 200 - 400 * random.random()b = 10 - 20 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color('lightcoral') # 淡珊瑚色t.circle(1)t.up()t.backward(a)t.right(90)t.backward(b)# 绘图区域t = turtle.Turtle()# 画布大小w = turtle.Screen()t.hideturtle() # 隐藏画笔t.getscreen().tracer(5, 0)w.screensize(bg='wheat') # wheat小麦t.left(90)t.up()t.backward(150)t.down()t.color('sienna')# 画樱花的躯干cherry_tree(60, t)# 掉落的花瓣cherry_fall_petal(200, t)w.exitonclick()
七、五角星代码实例
import turtleturtle.color("red") # 设置画笔颜色turtle.fillcolor("red") # 设置填充颜色turtle.begin_fill()for i in range(5):turtle.forward(200)turtle.right(144)turtle.end_fill()turtle.hideturtle()turtle.done()
八、彩虹玫瑰代码实例
import turtleturtle.pensize(2)turtle.bgcolor("white")turtle.speed(0)colors = ["red","orange","yellow","green","blue","cyan","purple"] # 彩虹7颜色for i in range(200) :turtle.pencolor(colors[i % 7])turtle.forward(i)turtle.left(60 + 1)turtle.hideturtle()
九、谢尔宾斯基三角形代码实例
import turtledef draw_triangle(points, color, t):t.fillcolor(color)t.up()t.goto(points[0][0], points[0][1])t.down()t.begin_fill()t.goto(points[1][0], points[1][1])t.goto(points[2][0], points[2][1])t.goto(points[0][0], points[0][1])t.end_fill()def get_mid(point1, point2):return (point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2def sierpinski(points, degree, t):color_map = ['blue', 'red', 'green', 'yellow', 'violet', 'orange', 'white', ]draw_triangle(points, color_map[degree], t)if degree > 0:sierpinski([points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])], degree - 1, t)sierpinski([points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])], degree - 1, t)sierpinski([points[2], get_mid(points[0], points[2]), get_mid(points[1], points[2])], degree - 1, t)if __name__ == "__main__":t = turtle.Turtle()t.speed(5)win = turtle.Screen()points = [[-100, -50], [0, 100], [100, -50]]sierpinski(points, 3, t)t.hideturtle()win.exitonclick()