python繪製圖形(Turtle模塊)

用python的Turtle模塊能夠繪製不少精美的圖形,下面簡單介紹一下使用方法。python

須要用到的工具備python,python 的安裝這裏就再也不細說。自行搜索。ide

 1 from turtle import *  #引入turtle模塊
 2 color('red', 'yellow')  #設置繪製的顏色和填充顏色
 3 
 4 # 海龜設置
 5 hideturtle()    # 隱藏箭頭
 6 speed(10)       # 設置速度
 7 # 前進後退,左轉右轉
 8 fd(100)     # 前進100像素(forward(100)也能夠)
 9 right(90)   # 右轉90°
10 back(100)   # 後退100像素
11 left(90)    # 左轉90°
12 # 填充顏色
13 begin_fill() #開始填充位置
14 fillcolor('yellow') #填充顏色
15 DoSomethinghere()   #繪製你想繪製的圖形
16 end_fill()   #結束填充位置
17 # 擡起筆和放下筆,這樣進行的操做不會留下痕跡(填充顏色後會顯示)
18 penup()
19 goto(start_pos)
20 fd(radius)
21 pendown()

下面給出幾個簡單的實例工具

1》繪製單個五角星spa

 1 from turtle import *
 2 color('red', 'yellow')
 3 begin_fill()
 4 hideturtle()
 5 speed(10)
 6 while True:
 7     forward(200)
 8     right(144)
 9     if abs(pos()) < 1:
10         break
11 end_fill()
12 done()

效果以下:code

2》繪製雙子星blog

 1 from turtle import *
 2 color('red', 'yellow')
 3 begin_fill()
 4 hideturtle()
 5 speed(10)
 6 while True:
 7     forward(200)
 8     right(144)
 9     if abs(pos()) < 1:
10         break
11 while True:
12     back(200)
13     left(144)
14     if abs(pos()) < 1:
15         break
16 end_fill()
17 done()

效果圖以下:class

 

3》繪製雙花import

 1 from turtle import *
 2 
 3 
 4 speed(10)
 5 color('red', 'yellow')
 6 begin_fill()
 7 while True:
 8     forward(200)
 9     right(164)
10     if abs(pos()) < 1:
11         break
12 while True:
13     back(200)
14     left(164)
15     if abs(pos()) < 1:
16         break
17 end_fill()
18 done()

效果圖以下:搜索

剩下的方法還請自行嘗試,你會繪出更多難以想象的圖形圖案。方法

相關文章
相關標籤/搜索