對於Python turtle的學習筆記

進一步地,我嘗試學習了Python 的其中一個很是重要的函數庫——turtle庫python

這是一個用於python繪圖的函數庫,方便又好用!框架

對於它的安裝,如今咱們所用的python 3的系統運用到的指令是:ide

pip3 install turtle函數

安裝完以後就可使用它啦~這樣就能夠開始畫畫啦~(激動)學習

 

1、turtle庫基本介紹:spa

python的turtle庫是一個直觀有趣的圖形繪製的函數庫;blog

turtle庫有一個繪製框架:「」小烏龜」有轉向,行進的動做,方向有「前進方向」,「後退方向」,「左側方向」,「右側方向"等,行進動做有「前進」,「後退」,「旋轉」等;ip

2、開始繪圖:ci

1.創建畫布it

咱們能夠用過代碼setup()或screensize()創建一個窗口畫布,咱們就能夠在上面做畫了!

我如今主要用setup(),其中setup(width,height,startx,starty)

前兩個參數是設置屏幕的寬高比例;

後兩個是設置窗口在屏幕的的位置;

2.設置畫筆

對於畫筆,我瞭解到有幾個設置的函數(在引用了from turtle import*的狀況下):

pensize()是畫筆的大小,pensize(2)即說明畫筆大小爲2個像素點;

penup()是擡起畫筆動做;

pendown()是放下畫筆動做;

pencolor()是設置畫筆的顏色……

恩,瞭解了這些,我已經能夠開始好好利用個人畫筆繪畫了!

固然還少不了其餘一些指令函數:

forward()/fd()    #向前走

backward()       #向後走

right(degree)    #向右轉

left(degree)      #向左轉

speed(speed)  #繪畫速度

begin_fill()        #開始填充

end_fill()           #結束填充……

瞭解到這些之後,就能夠開始實戰了!

3、個人做品:

1.畫疊加的等邊三角形

代碼:

from turtle import*
pensize(3)
color('black','white')
penup()
begin_fill()
seth(90)
fd(50)
pendown()
seth(-120)
fd(200)
seth(0)
fd(200)
seth(120)
fd(100)
seth(180)
fd(100)
seth(-60)
fd(100)
seth(60)
fd(100)
seth(120)
fd(100)
end_fill()
done()

效果:

2.太極!!

代碼:

from turtle import*
pensize(2)
color('black','black')
begin_fill()
circle(80)
end_fill()
begin_fill()
seth(180)
color('black','white')
seth(180)
circle(-80,180)
circle(-40,180)
end_fill()
begin_fill()
color('black','black')
circle(40,180)
end_fill()
penup()
seth(90)
fd(30)
seth(0)
pendown()
begin_fill()
color('black','white')
circle(10)
end_fill()
penup()
seth(90)
fd(80)
seth(0)
pendown()
begin_fill()
color('black','black')
circle(10)
end_fill()
done()

效果:

3.國際象棋盤

代碼:

from turtle import*
import math
pensize(2)
speed(50)
temp=0
num=0
penup()
seth(120)
fd(150)
pendown()
seth(0)
for i in range(10):
  for i in range(10):
    if(temp%2==0):
      begin_fill()
      color('black','white')
      for i in range(4):
        fd(40)
        right(90)
        fd(40)
        temp+=1
      end_fill()
    else:
      begin_fill()
      color('black','black')
      for i in range(4):
        fd(40)
        right(90)
      fd(40)
      temp+=1
      end_fill()
  if(num%2==0):
    right(90)
    fd(80)
    right(90)
    num+=1
    if(num==10):
       break
  else:
    left(90)
    left(90)
    num+=1

penup()
seth(120)
fd(300)
pendown()

seth(-30)           #寫字開始
fd(45)

penup()
seth(194)
fd(70)
pendown()

seth(10)
fd(60)
right(100)
fd(90)
seth(30)
fd(20)                 #偏旁部首寫完

penup()
seth(85)
fd(122)
pendown()

seth(0)               #一橫
fd(50)

penup()
seth(195)
fd(50)
pendown()

seth(0)               #二橫
fd(45)

penup()
seth(195)
fd(70)
pendown()

seth(0)               #三橫
fd(80)

penup()
seth(118)
fd(62)
pendown()

seth(-90)
fd(59)                #右上結束

penup()
seth(-148)
fd(20)
pendown()

seth(-90)
fd(90)
seth(90)
fd(90)
seth(0)
fd(39)
right(90)
fd(92)
seth(120)
fd(15)                 #月外框結束

penup()
fd(55)
pendown()

seth(0)
fd(35)

penup()
seth(220)
fd(45)
pendown()

seth(0)
fd(35)                   #寫字結束

hideturtle()
end_fill()
done()

(我還特意寫了個字~~夠酷吧!)

學到這,我深入認識到原來會用turtle庫是多麼有用!我甚至以爲能夠任意畫圖了!

Python太棒了!

相關文章
相關標籤/搜索