以前没发现啊,python的IDLE里面自带了turtlee的一大堆实例,就在help-turtle demo里面,截下来大家看看,里面还有一些动图也很不错的:
原回答:
没有编过程,初尝python,循着大神们的轨迹,用turtle画了两张图来练练手,就目前接触到的,程序画图主要有两种:
一是类似于小猪佩奇、哆啦A梦这种的,用大量的图形一笔一笔勾勒出来,其实这种图用python还没ppt来的快,很多曲线不知道其方程的话甚至不好规划,只能用椭圆、圆这些常见的来代替,这是我画的一个QQ企鹅,虽然每一步很简单,但真的需要很多步。。。
以下源码:
importturtle ast
frommath import*
#定义一个椭圆
defellipse(m,n,a):
t.goto(m,n)
t.setheading(0)
t.pd()
t.begin_fill()
b=a/5
fori inrange(120):
if0<=i<30 or60<=i<90:
a=a+b
t.lt(3)
t.fd(a)
else:
a=a-b
t.lt(3)
t.fd(a)
t.end_fill()
t.pu()
return
#初始设置
t.screensize(400,400,"white")
t.pensize(4)
t.pencolor("black")
t.speed(0)
t.ht()
t.pu()
#画头
t.goto(150,100)
t.setheading(90)
t.pd()
t.fillcolor("black")
t.begin_fill()
t.circle(200,180)
t.lt(60)
t.circle(400,60)
t.end_fill()
t.fillcolor("red")
t.begin_fill()
t.rt(60)
t.circle(-60,60)
t.rt(60)
t.circle(-460,60)
position1=t.pos()#存下坐标待会画肚子起点
t.rt(60)
t.circle(-60,60)
t.rt(60)
t.circle(400,60)
t.end_fill()
t.pu()
#眼睛,4个椭圆
t.pencolor("white")
t.fillcolor("white")
ellipse(0,130,0.5)
ellipse(-100,130,0.5)
t.pencolor("black")
t.fillcolor("black")
ellipse(-10,160,0.18)
ellipse(-90,160,0.18)
#嘴巴
t.goto(-180,98)
t.pd()
t.pencolor("orange")
t.fillcolor("orange")
t.pd()
t.setheading(30)
t.begin_fill()
t.circle(-260,60)
t.rt(120)
t.circle(-260,60)
t.end_fill()
t.pu()
t.goto(-150,99)
t.setheading(-30)
t.fillcolor("black")
t.begin_fill()
t.circle(200,60)
t.rt(150)
t.circle(-115.47,120)
t.end_fill()
t.pu()
#肚子
t.goto(position1)
t.pd()
t.pencolor("black")
t.fillcolor("black")
t.begin_fill()
t.setheading(-150)
t.circle(200,60)
t.lt(90)
t.circle(50,90)
t.rt(180)
t.circle(280,180)
t.rt(180)
t.circle(50,90)
t.lt(90)
t.circle(200,60)
t.setheading(-150)
t.circle(-460,60)
t.end_fill()
t.pu()
t.fillcolor("white")
t.pencolor("white")
t.begin_fill()
t.goto(-270,30)
t.pd()
t.setheading(-120)
t.circle(250,240)
t.setheading(-150)
t.circle(-433,60)
t.end_fill()
t.pu()
#画脚
t.goto(position1)
t.setheading(0)
t.fd(90)
t.rt(90)
t.fd(365)
position2=t.pos()
t.pd()
t.pencolor("black")
t.fillcolor("orange")
t.begin_fill()
t.setheading(-180)
t.circle(30,90)
t.lt(60)
t.circle(110,60)
t.goto(position2)
t.end_fill()
t.pu()
t.goto(position2)
t.setheading(0)
t.fd(280)
position3=t.pos()
t.pd()
t.begin_fill()
t.setheading(0)
t.circle(-30,90)
t.rt(60)
t.circle(-110,60)
t.goto(position3)
t.end_fill()
t.pu()
t.done()
二是需要用一些迭代算法实现的图,主要就是大家常画的python树,这个就是python画图的优点了,虽然比之专业作图软件肯定不如,但turtle嘛,本来就是给大家玩一玩的,这种图和上面的思路就完全不一样了,很好的体现除了程序画图的特点,模块清晰,代码简洁,功能强大。
以下源码:
importturtle ast
importrandom
defflower(r):
heading=t.heading()
t.pd()
t.pencolor("red")
t.fillcolor("red")
t.begin_fill()
fori inrange(5):
t.circle(r,144)
t.rt(72)
t.end_fill()
t.setheading(heading)
t.pencolor("black")
t.fillcolor("black")
t.pu()
return
deftree(n,l):
ifn>0:
t.pensize(2*n)
t.pd()
t.fd(l)
ifrandom.randint(0,2) == 0:
t.pu()
t.bk(l/2)
rf = 1+4*random.random()
flower(rf)
t.fd(l/2)
a=20*random.random()+10#右转
b=20*random.random()+10#左转
d = l * (random.random() * 0.25 + 0.7) #枝干长度
t.rt(a)
tree(n-1,d)
t.lt(a+b)
tree(n-1,d)
t.rt(b)
t.pu()
t.bk(l)
defsnow(m):
t.pencolor("white")
t.fillcolor("white")
fori inrange(m):
a = 400 - 800 * random.random()
b = 600 - 800 * random.random()
t.pu()
t.goto(a,b)
rsnow = 5 * random.random()
t.pd()
t.begin_fill()
t.circle(rsnow)
t.end_fill()
return
t.screensize(400,400,bg ="lightpink")
t.setheading(90)
t.ht()
t.pu()
t.goto(-150,-400)
t.pd()
t.pensize(14)
t.fd(100)
t.speed(0)
tree(7,100)
snow(200)
t.done()
这就是常规的两种思路画图的方法,期待能有更多的不同的思路画出的图。