import turtle
turtle.shape('turtle')
# 设置色彩模式是RGB:
turtle.colormode(255)
turtle.lt(90)
lv = 14
l = 120
s = 45
turtle.width(lv)
# 初始化RGB颜色:
r = 0
g = 0
b = 0
turtle.pencolor(r, g, b)
turtle.penup()
turtle.bk(l)
turtle.pendown()
turtle.fd(l)
def draw_tree(l, level):
global r, g, b
# save the current pen width
w = turtle.width()
# narrow the pen width
turtle.width(w * 3.0 / 4.0)
# set color:
r = r 1
g = g 2
b = b 3
turtle.pencolor(r % 200, g % 200, b % 200)
l = 3.0 / 4.0 * l
turtle.lt(s)
turtle.fd(l)
if level < lv:
draw_tree(l, level 1)
turtle.bk(l)
turtle.rt(2 * s)
turtle.fd(l)
if level < lv:
draw_tree(l, level 1)
turtle.bk(l)
turtle.lt(s)
# restore the previous pen width
turtle.width(w)
turtle.speed("fastest")
draw_tree(l, 4)
turtle.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.shape('turtle')
# 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()
import random
import turtle
def love(x, y): # 在(x,y)处画爱心lalala
lv = turtle.Turtle()
lv.hideturtle()
lv.up()
lv.goto(x, y) # 定位到(x,y)
def curvemove(): # 画圆弧
for i in range(20):
lv.right(10)
lv.forward(2)
lv.color('red', 'pink')
lv.speed(0)
lv.pensize(1)
# 开始画爱心lalala
lv.down()
lv.begin_fill()
lv.left(140)
lv.forward(22)
curvemove()
lv.left(120)
curvemove()
lv.forward(22)
lv.write("{}".format("I Love You!"), font=("Arial", 10, "normal"), align="center")
lv.left(140) # 画完复位
lv.end_fill()
def tree(branchLen, t):
if branchLen > 5: # 剩余树枝太少要结束递归
if branchLen < 20: # 如果树枝剩余长度较短则变绿
t.color("green")
t.pensize(random.uniform((branchLen 5) / 4 - 2, (branchLen 6) / 4 5))
t.down()
t.forward(branchLen)
love(t.xcor(), t.ycor()) # 传输现在turtle的坐标
t.up()
t.backward(branchLen)
t.color("brown")
return
t.pensize(random.uniform((branchLen 5) / 4 - 2, (branchLen 6) / 4 5))
t.down()
t.forward(branchLen)
# 以下递归
ang = random.uniform(15, 45)
t.right(ang)
tree(branchLen - random.uniform(12, 16), t) # 随机决定减小长度
t.left(2 * ang)
tree(branchLen - random.uniform(12, 16), t) # 随机决定减小长度
t.right(ang)
t.up()
t.backward(branchLen)
def Fonts():
t.penup()
t.goto(-300, -300)
t.pencolor('red')
t.write("我爱你!^_^", font=('方正行黑简体', 30, 'normal'))
myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
Fonts()
myWin.exitonclick()
import turtle
def flyTo(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
def drawEye():
turtle.tracer(False)
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
else:
a = 0.05
turtle.left(3)
turtle.fd(a)
turtle.tracer(True)
def beard():
""" 画胡子, 一共六根
"""
# 左边第一根胡子
flyTo(-37, 135)
turtle.seth(165)
turtle.fd(60)
# 左边第二根胡子
flyTo(-37, 125)
turtle.seth(180)
turtle.fd(60)
# 左边第三根胡子
flyTo(-37, 115)
turtle.seth(193)
turtle.fd(60)
# 右边第一根胡子
flyTo(37, 135)
turtle.seth(15)
turtle.fd(60)
# 右边第二根胡子
flyTo(37, 125)
turtle.seth(0)
turtle.fd(60)
# 右边第三根胡子
flyTo(37, 115)
turtle.seth(-13)
turtle.fd(60)
def drawRedScarf():
""" 画围巾
"""
turtle.fillcolor("red") # 填充颜色
turtle.begin_fill()
turtle.seth(0) # 朝向右
turtle.fd(200) # 前进10个单位
turtle.circle(-5, 90)
turtle.fd(10)
turtle.circle(-5, 90)
turtle.fd(207)
turtle.circle(-5, 90)
turtle.fd(10)
turtle.circle(-5, 90)
turtle.end_fill()
def drawMouse():
flyTo(5, 148)
turtle.seth(270)
turtle.fd(100)
turtle.seth(0)
turtle.circle(120, 50)
turtle.seth(230)
turtle.circle(-120, 100)
def drawRedNose():
flyTo(-10, 158)
turtle.fillcolor("red") # 填充颜色
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
def drawBlackdrawEye():
turtle.seth(0)
flyTo(-20, 195)
turtle.fillcolor("#000000") # 填充颜色
turtle.begin_fill()
turtle.circle(13)
turtle.end_fill()
turtle.pensize(6)
flyTo(20, 205)
turtle.seth(75)
turtle.circle(-10, 150)
turtle.pensize(3)
flyTo(-17, 200)
turtle.seth(0)
turtle.fillcolor("#ffffff")
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
flyTo(0, 0)
def drawFace():
turtle.forward(183) # 前行183个单位
turtle.fillcolor("white") # 填充颜色为白色
turtle.begin_fill() # 开始填充
turtle.left(45) # 左转45度
turtle.circle(120, 100) # 右边那半边脸
turtle.seth(90) # 朝向向上
drawEye() # 画右眼睛
turtle.seth(180) # 朝向左
turtle.penup() # 抬笔
turtle.fd(60) # 前行60
turtle.pendown() # 落笔
turtle.seth(90) # 朝向上
drawEye() # 画左眼睛
turtle.penup() # 抬笔
turtle.seth(180) # 朝向左
turtle.fd(64) # 前进64
turtle.pendown() # 落笔
turtle.seth(215) # 修改朝向
turtle.circle(120, 100) # 左边那半边脸
turtle.end_fill() #
def drawHead():
""" 画了一个被切掉下半部分的圆
"""
turtle.penup() # 抬笔
turtle.circle(150, 40) # 画圆, 半径150,圆周角40
turtle.pendown() # 落笔
turtle.fillcolor("#00a0de") # 填充色
turtle.begin_fill() # 开始填充
turtle.circle(150, 280) # 画圆,半径150, 圆周角280
turtle.end_fill()
def drawAll():
drawHead()
drawRedScarf()
drawFace()
drawRedNose()
drawMouse()
beard()
flyTo(0, 0)
turtle.seth(0)
turtle.penup()
turtle.circle(150, 50)
turtle.pendown()
turtle.seth(30)
turtle.fd(40)
turtle.seth(70)
turtle.circle(-30, 270)
turtle.fillcolor("#00a0de")
turtle.begin_fill()
turtle.seth(230)
turtle.fd(80)
turtle.seth(90)
turtle.circle(1000, 1)
turtle.seth(-89)
turtle.circle(-1000, 10)
turtle.seth(180)
turtle.fd(70)
turtle.seth(90)
turtle.circle(30, 180)
turtle.seth(180)
turtle.fd(70)
turtle.seth(100)
turtle.circle(-1000, 9)
turtle.seth(-86)
turtle.circle(1000, 2)
turtle.seth(230)
turtle.fd(40)
turtle.circle(-30, 230)
turtle.seth(45)
turtle.fd(81)
turtle.seth(0)
turtle.fd(203)
turtle.circle(5, 90)
turtle.fd(10)
turtle.circle(5, 90)
turtle.fd(7)
turtle.seth(40)
turtle.circle(150, 10)
turtle.seth(30)
turtle.fd(40)
turtle.end_fill()
# 左手
turtle.seth(70)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.circle(-30)
turtle.end_fill()
# 脚
flyTo(103.74, -182.59)
turtle.seth(0)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.fd(15)
turtle.circle(-15, 180)
turtle.fd(90)
turtle.circle(-15, 180)
turtle.fd(10)
turtle.end_fill()
flyTo(-96.26, -182.59)
turtle.seth(180)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.fd(15)
turtle.circle(15, 180)
turtle.fd(90)
turtle.circle(15, 180)
turtle.fd(10)
turtle.end_fill()
# 右手
flyTo(-133.97, -91.81)
turtle.seth(50)
turtle.fillcolor("#FFFFFF")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
# 口袋
flyTo(-103.42, 15.09)
turtle.seth(0)
turtle.fd(38)
turtle.seth(230)
turtle.begin_fill()
turtle.circle(90, 260)
turtle.end_fill()
flyTo(5, -40)
turtle.seth(0)
turtle.fd(70)
turtle.seth(-90)
turtle.circle(-70, 180)
turtle.seth(0)
turtle.fd(70)
# 铃铛
flyTo(-103.42, 15.09)
turtle.fd(90)
turtle.seth(70)
turtle.fillcolor("#ffd200")
turtle.begin_fill()
turtle.circle(-20)
turtle.end_fill()
turtle.seth(170)
turtle.fillcolor("#ffd200")
turtle.begin_fill()
turtle.circle(-2, 180)
turtle.seth(10)
turtle.circle(-100, 22)
turtle.circle(-2, 180)
turtle.seth(180 - 10)
turtle.circle(100, 22)
turtle.end_fill()
flyTo(-13.42, 15.09)
turtle.seth(250)
turtle.circle(20, 110)
turtle.seth(90)
turtle.fd(15)
turtle.dot(10)
flyTo(0, -150)
drawBlackdrawEye()
def main():
turtle.screensize(800, 6000, "#F0F0F0")
turtle.pensize(3)
turtle.speed(9)
drawAll()
if __name__ == "__main__":
main()
turtle.mainloop()