天天看点

python turtle_使用Python的turtle画炫酷图形

例子一:

import 
           

效果图如下:

python turtle_使用Python的turtle画炫酷图形
例子二:

可以做成交互的,利用eval函数获得用户想绘制的边数

代码:

#Python学习交流群:973783996
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
sides=evali(input("输入要绘制的边的数目,请输入2-6的数字!"))
colors=["red","yellow","green","blue","orange","purple"]
for x in xrange(100):
    t.pencolor(colors[x%sides])
    t.forward(x*3/sides+x)
    t.left(360/sides+1)
    t.width(x*sides/200)

print("####结束####")
           

效果:两条边

python turtle_使用Python的turtle画炫酷图形

例子三:

绘制橡皮筋球体

import turtle
#Python学习交流群:973783996
t = turtle.Pen()
turtle.bgcolor("black")
#sides=evali(input("输入要绘制的边的数目,请输入2-6的数字!"))
sides=6
colors=["red","yellow","green","blue","orange","purple"]
for x in range(360):
    t.pencolor(colors[x%sides])
    t.forward(x*3/sides+x)
    t.left(360/sides+1)
    t.width(x*sides/180)
    t.left(91)
print("####结束####")
           

效果图如下:

python turtle_使用Python的turtle画炫酷图形