写在前面:因为想要玩耍就开始学这个库了
参考博客
参考简书
官方开发文档
小学生python文档(
目录
设置颜色几种输入格式(pencolor/color/fillcolor)
常用Turtle和Screen方法
turtle方法
海龟动作
移动和绘制
- forward() fd() 前进
- backward() bk() back() 后退
- right() rt() 右转
- left() lt() 左转
- goto() setpos() setposition() 前往/定位
- setx() 设置x坐标
- sety() 设置y坐标
- setheading() seth() 设置朝向
- home() 返回原点
- circle() 画圆
- dot() 画点
- stamp() 印章
- clearstamp() 清楚印章
- undo() 撤销
- speed() 速度
获取海龟的状态
- position() pos()位置
- towards() 目标方向
- xcor() x坐标
- ycor() y坐标
- heading() 朝向
- distance() 距离
设置与度量单位
- degrees() 角度
- radians() 弧度
画笔控制
绘图状态
- pendown() pd() down() 画笔落下
- penup() pu() up() 画笔抬起
- pensize() width() 画笔粗细
- pen() 画笔
- isdown() 画笔是否落下
颜色控制
- color() 颜色
- pencolor() 画笔颜色
- fillcolor() 填充颜色
填充
- filling() 是否填充
- begin_fill() 开始填充
- end_fill() 结束填充
更多绘图控制
- reset() 重置
- clear() 清空
- write() 书写
海龟状态
可见性
- showturtle() st() 显示海龟
- hideturtle() ht() 隐藏海龟
- isvisible() 是否可见
外观
- shape() 形状
- resizemode() 大小调整模式
- shapesize() turtlesize() 形状大小
- shearfactor() 剪切因子
- settiltangle() 设置倾角
- tiltangle() 倾角
- tilt() 倾斜
- shapetransform() 变形
- get_shapepoly() 获取形状多边形
使用事件
- onclick() 当鼠标点击
- onrelease() 当鼠标释放
- ondrag() 当鼠标拖动
特殊海龟方法
- begin_poly() 开始记录多边形
- end_poly() 结束记录多边形
- get_poly() 获取多边形
- clone() 克隆
- getturtle() getpen() 获取海龟画笔
- getscreen() 获取屏幕
- setundobuffer() 设置撤销缓冲区
- undobufferentries() 撤销缓冲区条目数
TurtleScreen/Screen 方法
窗口控制
- bgcolor() 背景颜色
- bgpic() 背景图片
- clear() clearscreen() 清屏
- reset() resetscreen() 重置
- screensize() 屏幕大小
- setworldcoordinates() 设置世界坐标系
动画控制
- delay() 延迟
- tracer() 追踪
- update() 更新
使用屏幕事件
- listen() 监听
- onkey() | onkeyrelease() 当键盘按下并释放
- onkeypress() 当键盘按下
- onclick() | onscreenclick() 当点击屏幕
- ontimer() 当达到定时
- mainloop() | done() 主循环
设置与特殊方法
- mode() 模式
- colormode() 颜色模式
- getcanvas() 获取画布
- getshapes() 获取形状
- register_shape() | addshape() 添加形状
- turtles() 所有海龟
- window_height() 窗口高度
- window_width() 窗口宽度
输入方法
- textinput() 文本输入
- numinput() 数字输入
Screen 专有方法
- bye() 退出
- exitonclick() 当点击时退出
- setup() 设置
- title() 标题
代码展示
大丽花
import turtle
turtle=turtle.Turtle()
screen=turtle.getscreen()#获取屏幕
turtle.color('lightcoral', 'lavenderblush')#颜色
turtle.begin_fill()#开始填充
for i in range(50):
turtle.forward(200)#前进
turtle.left(170)#左转
turtle.end_fill()#结束填充
screen.mainloop()#主循环 允许循环执行
玫瑰
改了自己喜欢的颜色
import turtle
import time
turtle.speed(5)
# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
# 花蕊
turtle.fillcolor("mediumpurple")
turtle.begin_fill()
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150,80)
turtle.left(50)
turtle.circle(150,90)
turtle.end_fill()
# 花瓣1
turtle.left(150)
turtle.circle(-90,70)
turtle.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turtle.circle(-90,40)
# 花瓣2
turtle.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83)
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80,90)
turtle.left(90)
turtle.circle(80,90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,60)
樱花树
这个是我运行的时候觉得最震撼的一个 太好看了
from turtle import *
from random import *
from math import *
def tree(n,l):
pd()#下笔
#阴影效果
t = cos(radians(heading()+45))/8+0.25
pencolor(t,t,t)
pensize(n/3)
forward(l)#画树枝
if n>0:
b = random()*15+10 #右分支偏转角度
c = random()*15+10 #左分支偏转角度
d = l*(random()*0.25+0.7) #下一个分支的长度
#右转一定角度,画右分支
right(b)
tree(n-1,d)
#左转一定角度,画左分支
left(b+c)
tree(n-1,d)
#转回来
right(c)
else:
#画叶子
right(90)
n=cos(radians(heading()-45))/4+0.5
pencolor(n,n*0.8,n*0.8)
circle(3)
left(90)
#添加0.3倍的飘落叶子
if(random()>0.7):
pu()
#飘落
t = heading()
an = -40 +random()*40
setheading(an)
dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
forward(dis)
setheading(t)
#画叶子
pd()
right(90)
n = cos(radians(heading()-45))/4+0.5
pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
circle(2)
left(90)
pu()
#返回
t=heading()
setheading(an)
backward(dis)
setheading(t)
pu()
backward(l)#退回
bgcolor(0.5,0.5,0.5)#背景色
ht()#隐藏turtle
speed(10)#速度 1-10渐进,0 最快
tracer(0,0)#非飘落效果是(0,0) 动画效果是(1,0)就是加载的太慢
pu()#抬笔
backward(100)
left(90)#左转90度
pu()#抬笔
backward(300)#后退300
tree(12,100)#递归7层
done()
梅花树
不要问我为什么是梅花树 可能是因为避免重复吧
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()
其他
turtle库基本颜色
因为经常想换颜色整活所以干脆把它加到了博客里方便自己查阅
设置颜色几种输入格式(pencolor/color/fillcolor)
pencolor()
返回以颜色描述字符串或元组 (见示例) 表示的当前画笔颜色。可用作其他 color/pencolor/fillcolor 调用的输入。
pencolor(colorstring)
设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串,例如 "red"
、"yellow"
或 "#33cc8c"
。
pencolor((r, g, b))
设置画笔颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode,colormode 的值为 1.0 或 255 (参见 colormode())。
pencolor(r, g, b)
设置画笔颜色为以 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode。
我用到的一共有如下几种
你甚至可以拿它画二次元plmm(感慨)