標籤:

使用Python的turtle畫炫酷圖形

例子一:

import turtle
t = turtle.Pen()
turtle.bgcolor("black")
sides=6
colors=["red","yellow","green","blue","orange","purple"]
for x in xrang(360):
t.pencolor(colors[x%sides])
t.forward(x*3/sides+x)
t.left(360/sides+1)
t.width(x*sides/200)

print("####結束####")

效果圖如下:

例子二:

可以做成交互的,利用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("####結束####")

效果:兩條邊

例子三:

繪製橡皮筋球體

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("####結束####")

效果圖如下:

推薦閱讀:

TAG:Python |