1.绘图区(cancas是绘图区域)
turtle.screensize(800,600,'white') 画布的大小以及背景色
turtle.setup(width=0.5,height=0.75) 起始位置高宽
2.画笔设置
turtle.forward(x) 向当前画笔朝前移动x像素长度
turtle.backward(x) 向当前画笔相反方向移动x像素长度
turtle.right(x) 顺时针旋转多少度
aturtle.left(x) 逆时针旋转多少度
aturtle.pendown() 移动时绘制图形
turtle.goto(x,y) 将画笔移动到坐标为x,y的坐标位置
turtle.penup() 移动时不绘制图形,提起笔
turtle.speed(a) 画笔绘制的速度范围
turtle.end_fill() 填充完成
turtle.hideturtle() 隐藏箭头显示
turtle.showturtle() 显示箭头
turtle.circle() 画图,半径为正,表示圆心在画笔的左边画圈
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始位置
turtle.undo() 撤销上一个turtle动作
turtle.pensize(width) 绘制图形的宽度
turtle.pencolor() 画笔的颜色
turtle.fillcolor() 绘制图形的填充颜色
turtle.color(x,y) 同时设置pencolor=x,fillcolor=y
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 准备开始填充图形
下图是一幅樱花图 :
# 每次运行 树的形状是随机的
import turtle as T
import random
import time
# 画樱花的躯干(60,t)
def 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) # 6
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
Tree(branch - 10 * b, t)
t.left(40 * a)
Tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()
# 掉落的花瓣
def 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 = T.Turtle()
# 画布大小
w = T.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')
# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()
T.done()
本文介绍了Python绘图的基础知识,包括绘图区Canvas的使用和画笔设置,帮助理解如何在Python中绘制图像,示例为一幅精美的樱花图。
503

被折叠的 条评论
为什么被折叠?



