利用turtle模塊畫圖

利用turtle模塊畫圖

代碼:

import turtle 
#導入畫圖模塊,查看已安裝的模塊dir('modules')或者help('modules')命令,查看模塊詳情help('turtle')
t=turtle.Turtle() 
#調用模塊裏面的Turtle工具,調用模塊:模塊名.函數名    turtle.Turtle()
t.speed(0) 
#畫筆速度1-9依次變快,0爲最快速度
def setpen(x,y):
      t.penup()
      t.goto(x,y)
      t.pendown()
      t.setheading(0)
#定義函數,定義畫筆的起點及朝向(x,y)爲起點 t.setheading(0)設置朝向爲0
#penup提筆,pundown落筆,goto移動
#也可直接跟參數定義函數。
# def setpen():
#     t.penup()
#     t.goto(10,10)
#     t.pendown()
#     t.sedheading(0)
# setpen()   調用
def circle(x,y,r,color):
      n=36
      angle=360/n
      p=3.1415926
      c=2*p*r
      l=c/n
      point_x=x-l/2
      point_y=y+r
      setpen(point_x,point_y)
      t.pencolor(color)
      t.fillcolor(color)
      t.begin_fill()
      for i in range(n):
           t.forward(l)
           t.right(angle)
      t.end_fill() 
#定義圓,pencolor畫筆顏色,fillcolor填充顏色,range(n)區間,邊數大於36以上時,可認爲是圓形。
#for循環一般形式:
#for <variable> in <sequence>:
#    <statements>
#else:
#    <statements>

#while循環語句一般形式:
#while 判斷條件:
#      語句
#else:
#      語句
circle(0,0,200,'red')
circle(0,0,155,'white')
circle(0,0,110,'red')
circle(0,0,65,'blue')
#調用定義的函數。
def five_star():
      setpen(0,0)
      t.setheading(161)
      t.forward(65)
      t.setheading(0)
      t.fillcolor('white')
      t.begin_fill()
      t.hideturtle()
      t.penup()
      for i in range(5):
          t.forward(124)
          t.right(144)
      t.end_fill() 
#定義五角星,hideturtle隱藏畫筆
five_star()
#調用定義的函數
turtle.done()
#完成
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章