1.樱花树
效果:图一 樱花树
代码:
import turtle
import random
from turtle import *
from time import sleep
# 画樱花的躯干(60,t)
def tree(branchLen,t):
sleep(0.0005)
if branchLen >3:
if 8<= branchLen <=12:
if random.randint(0,2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 3)
elif branchLen <8:
if random.randint(0,1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branchLen / 10) # 6
t.forward(branchLen)
a = 1.5 * random.random()
t.right(20*a)
b = 1.5 * random.random()
tree(branchLen-10*b, t)
t.left(40*a)
tree(branchLen-10*b, t)
t.right(20*a)
t.up()
t.backward(branchLen)
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)
def main():
# 绘图区域
t = turtle.Turtle()
# 画布大小
w = turtle.Screen()
t.hideturtle() # 隐藏画笔
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()
main()
2.彩虹
效果:图二 彩虹旋
代码:
import turtle
q = turtle.Pen()
turtle.bgcolor("black")
sides = 7
colors =["red","yellow",
"green","cyan","red","blue","purple"]
for x in range(360):
q.pencolor(colors[x%sides])
q.forward(x*3/sides+x)
q.left(360/sides+1)
q.width(x*sides/200)
3.玫瑰
效果:图三 玫瑰
代码:
import turtle
# 设置初始位置
turtle.penup(); turtle.left(90); turtle.fd(200);
turtle.pendown(); turtle.right(90); turtle.pensize(5); turtle.speed(10);
# 花蕊
turtle.fillcolor("red"); 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("blue"); turtle.begin_fill(); turtle.circle(-80,90);
turtle.right(90); turtle.circle(-80,90); turtle.end_fill(); turtle.right(135);
turtle.left(180); turtle.fd(25); turtle.left(90); turtle.fd(80);
# 叶子2
turtle.right(90); turtle.right(45); turtle.fillcolor("cyan");
turtle.begin_fill(); turtle.circle(80,90); turtle.left(90);
turtle.circle(80,90); turtle.end_fill(); turtle.left(135);
turtle.left(180); turtle.right(90); turtle.circle(200,60);
4.钟表
效果:图四 黎明的钟
代码:
from turtle import *
from datetime import *
import time
def SetupClock(radius):
#建立表的外框
reset()
pensize()
for i in range(60):
Skip(radius)
if i % 5 == 0:
forward(20)
Skip(-radius-20)
else:
dot(5)
Skip(-radius)
right(6)
def Skip(step):
penup()
forward(step)
pendown()
#定义表针函数mkHand()
def mkHand(name, length):
#注册Turtle形状,建立表针Turtle
reset()
Skip(-length*0.1)
begin_poly()
forward(length*1.1)
end_poly()
handForm = get_poly()
register_shape(name, handForm)
def Init():
global secHand, minHand, hurHand, printer
mode("logo") # 重置Turtle指向北
#建立三个表针Turtle并初始化
mkHand("secHand", 125)
mkHand("minHand", 130)
mkHand("hurHand", 90)
secHand = Turtle()
secHand.shape("secHand")
minHand = Turtle()
minHand.shape("secHand")
hurHand = Turtle()
hurHand.shape("secHand")
for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 3)
hand.speed(0)
#建立输出文字Turtle
printer = Turtle()
printer.hideturtle()
printer.penup()
def get_week_day():
week_day_dict = {
0 : '星期一',
1 : '星期二',
2 : '星期三',
3 : '星期四',
4 : '星期五',
5 : '星期六',
6 : '星期天',
}
today = int(time.strftime("%w"))
return week_day_dict[today]
#更新时钟函数Tick()
def Tick():
#绘制表针的动态显示
t = datetime.today()
second = t.second + t.microsecond*0.000001
minute = t.minute + second/60.0
hour = t.hour + minute/60.0
tracer(False)
printer.forward(65)
# print(get_week_day())
printer.write(get_week_day(), align="center", font=("Courier", 14, "bold"))
printer.back(130)
printer.write((str(t.year)+"-"+str(t.month)+"-"+str(t.day)), align="center", font=("Courier", 14, "bold"))
printer.home()
tracer(True)
secHand.setheading(6*second)
minHand.setheading(6*minute)
hurHand.setheading(30*hour)
ontimer(Tick, 100) #100ms后继续调用tick
def main():
tracer(False)
Init()
SetupClock(160)
tracer(True)
Tick()
mainloop()
if __name__ == '__main__':
main()
5.穿雨鞋的小鸭子
效果:图五 穿雨鞋的小鸭子
代码:
from turtle import *
#扁嘴
pensize(2)
pu()
goto(-100,100)#上嘴最高顶点
seth(-50)
pd()
color('#6C3100','#FADD77')
begin_fill()
fd(16)
vertex_right = pos()#嘴最右顶点
rt(50)
fd(12)
vertex_down = pos()#下嘴最低顶点
rt(80)
fd(30)
circle(-3,200)
vertex_left = pos()#嘴最左顶点
goto(-100,100)
end_fill()
goto(vertex_left)#回到最左顶点
circle(-3,-200)#扁嘴
goto(vertex_right)
#身体
#头颈背尾曲线
color('#B6A88E')
pu()
goto(-100,100)
pd()
seth(80)
circle(-36,160)
fd(25)
circle(115,20)
circle(60,55)
circle(-200,20)
circle(110,20)
color('#7D6A4C')
circle(40,40)
color('#B6A88E')
seth(-100)
circle(-180,30)
circle(-20,50)
#右鸭腿
circle(20,70)
color('#736856')
circle(-12,120)
leg_pos1 = pos()#定位左腿位置
fd(25)
#前胸肚曲线
pu()
goto(vertex_down)
pd()
seth(-10)
color('#B9AD9D')
circle(-40,50)
circle(-80,48)
color('#736856')
circle(250,5)
circle(50,75)
color('#B9AD9D')
circle(220,28)
#左鸭腿
pu()
seth(175)
fd(40)
pd()
seth(-120)
fd(8)
circle(-10,120)
leg_pos2 = pos()#定位右腿位置
fd(15)
#眼睛
color('black')
#左眼
pu()
goto(vertex_down - (1,-29))
pd()
dot(4,'black')#相比circle(),不需要再额外填充颜色
#右眼
pu()
goto(vertex_down + (23,20))
pd()
dot(4,'black')
#翅膀
color('#BCB2A6')
pu()
goto(vertex_down - (-75,130))
seth(130)
pd()
circle(-25,130)
circle(-100,30)
fd(85)
point = pos()
rt(137)
fd(52)
circle(-100,58)
pu()
goto(point)
lt(30)
pd()
fd(60)
pu()
goto(point)
pd()
lt(10)
fd(70)
#腿部
#左腿
def leg(pos0):#鸭腿绘制函数
pensize(8)
color('#ECC578')
pu()
goto(pos0)
seth(0)
fd(7)
seth(-90)
fd(8.5)
pd()
fd(20)#腿长
leg(leg_pos1)
leg(leg_pos2)
#小红靴——函数
def boot(pos0):
pensize(2)
color('#B4070B','#FBA06B')
pu()
goto(pos0)#靴子右上顶点
pd()
begin_fill()
seth(140)
circle(25,80)
seth(-80)
fd(35)#fd(30)左侧线条
circle(-2,60)#靴低
fd(20)
circle(4,180)
seth(5)
fd(30)
circle(2,60)
goto(pos0)#右侧线条
end_fill()
boot(leg_pos1-(-20,30))
boot(leg_pos2-(-20,30))
#小雨滴
color('#77DDFF','#D8E8E5')
fd_ls = [200,140,250,240,230,220,180,250]
lt_ls = [30,60,60,100,125,170,200,330]
for i in range(8):
pu()
home()
lt(lt_ls[i])
fd(fd_ls[i])
pd()
seth(-78)
fd(15)
begin_fill()
circle(-3,200)
end_fill()
fd(15)
#文字
pu()
goto(vertex_left)
seth(180)
fd(150)
seth(-90)
fd(300)
color('black')
write('code by totoup',font=("Arial",15,"normal"))
hideturtle()
done()
6.骷髅头
效果:图六 骷髅头
代码:
import turtle as t
#黄底帽子
t.pu()
t.goto(0,200)
t.circle(-130,-80)
t.pd()
t.colormode(255)
t.pensize(5)
t.color(242,232,184) #帽子黄底RGB
t.begin_fill()
t.pencolor(0,0,0)
t.circle(-130,160)
t.seth(180)
t.fd(255)
t.end_fill()
#红色线条
t.begin_fill()
t.color(221,65,43) #帽子红色带
t.pencolor(0,0,0)
t.seth(80)
t.circle(-130,19)
t.seth(0)
t.fd(225)
t.seth(-59)
t.circle(-130,19)
t.seth(180)
t.fd(255)
t.end_fill()
#帽檐
t.begin_fill()
t.color(242,232,184)
t.pencolor(0,0,0)
t.fd(60)
t.circle(12,180)
t.fd(375)
t.circle(12,180)
t.fd(255 + 60)
t.end_fill()
#脸部下半轮廓
t.pu()
t.setpos(0,-30)
t.seth(-180)
t.circle(-130,-75)
t.pd()
t.circle(-130,150)
#眼睛鼻子
t.pu()
t.color(33,24,24) #眼睛、鼻子RGB
t.setpos(-45,64)
t.seth(-180)
t.pd()
t.begin_fill()
t.circle(33)
t.pu()
t.setpos(45,64)
t.pd()
t.circle(33)
t.end_fill()
t.pu()
t.setpos(0,5)
t.pd()
t.begin_fill()
t.circle(8)
t.end_fill()
#下巴
t.pencolor(0,0,0)
t.pu()
t.setpos(0,0)
t.seth(0)
t.circle(-75,45)
t.pd()
t.circle(-75,270)
#牙齿
t.pu()
t.setpos(0,120)
t.seth(0)
t.circle(-105,136)
t.pd()
t.circle(-105,86)
t.pu()
t.seth(0)
t.goto(0,200)
t.circle(-130,150)
t.pd()
t.circle(-130,60)
t.pu() #牙齿三根竖线
t.setpos(-30,-27)
t.seth(260)
t.pd()
t.fd(52)
t.pu()
t.setpos(30,-27)
t.pd()
t.seth(-260)
t.fd(-52)
t.pu()
t.setpos(0,-30)
t.seth(-90)
t.pd()
t.fd(56)
#上排右侧小爪爪
#释放注释为:上排右侧小爪爪实心金方案
t.pu()
#t.color(255,215,0) #金色的RGB
t.pencolor(0,0,0)
t.setpos(110,145)
t.seth(45)
t.pd()
#t.begin_fill()
t.fd(40)
t.seth(135)
t.circle(-30,235)
t.seth(-20)
t.circle(-30,220)
t.seth(-135)
t.fd(40)
#t.end_fill()
#上排左侧小爪爪
t.pu()
t.pencolor(0,0,0)
t.setpos(-110,145)
t.seth(135)
t.pd()
t.fd(40)
t.seth(45)
t.circle(30,235)
t.seth(-160)
t.circle(30,220)
t.seth(-45)
t.fd(40)
#下排右侧小爪爪
t.pu()
t.setpos(70,-10)
t.seth(-45)
t.pd()
t.fd(70)
t.seth(45)
t.circle(-30,235)
t.seth(-70)
t.circle(-30,255)
t.seth(135)
t.fd(22)
#下排左侧小爪爪
t.pu()
t.setpos(-70,-10)
t.seth(-135)
t.pd()
t.fd(70)
t.seth(135)
t.circle(30,235)
t.seth(-110)
t.circle(30,255)
t.seth(45)
t.fd(22)
7.奥运五环
效果:图七 五环
代码:
import turtle
p = turtle
p.pensize(3)
p.color("blue")
p.circle(30,360)
p.pu()
p.goto(60,0)
p.pd()
p.color("black")
p.circle(30,360)
p.pu()
p.goto(120,0)
p.pd()
p.color("red")
p.circle(30,360)
p.pu()
p.goto(90,-30)
p.pd()
p.color("green")
p.circle(30,360)
p.pu()
p.goto(30,-30)
p.pd()
p.color("yellow")
p.circle(30.360)
p.done()