對於Python turtle的學習筆記


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

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

對於它的安裝,現在我們所用的python 3的系統運用到的指令是:

pip3 install turtle

安裝完之后就可以使用它啦~這樣就可以開始畫畫啦~(激動)

 

一、turtle庫基本介紹:

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

turtle庫有一個繪制框架:“”小烏龜”有轉向,行進的動作,方向有“前進方向”,“后退方向”,“左側方向”,“右側方向"等,行進動作有“前進”,“后退”,“旋轉”等;

二、開始繪圖:

1.建立畫布

我們可以用過代碼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()           #結束填充……

了解到這些以后,就可以開始實戰了!

三、我的作品:

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太棒了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM