Python——turtle(海龜繪圖)

基本功能介紹

在海龜作圖中,我們可以編寫指令讓一個虛擬的(想象中的)海龜在屏幕上來回移動。這個海龜帶着一隻鋼筆,我們可以讓海龜無論移動到哪都使用這隻鋼筆來繪製線條。通過編寫代碼,以各種很酷的模式移動海龜,我們可以繪製出令人驚奇的圖片。使用海龜作圖,我們不僅能夠只用幾行代碼就創建出令人印象深刻的視覺效果,而且還可以跟隨海龜看看每行代碼如何影響到它的移動。這能夠幫助我們理解代碼的邏輯。所以海龜作圖也常被用作新手學習 Python 的一種方式。

python2.6版本中後引入的一個簡單的繪圖工具,叫做海龜繪圖(Turtle Graphics),turtle庫是python的內部庫,使用導入即可 import turtle

1. 畫布(canvas)

畫布就是turtle爲我們展開用於繪圖區域, 我們可以設置它的大小和初始位置

1.1 設置畫布大小

turtle.screensize(canvwidth=None, canvheight=None, bg=None)
參數分別爲畫布的寬(單位像素), 高, 背景顏色
如:

turtle.screensize(800, 600, "green")
turtle.screensize() #返回默認大小(400, 300)

turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
參數:
width, height: 輸入寬和高爲整數時, 表示像素; 爲小數時, 表示佔據電腦屏幕的比例
(startx, starty): 這一座標表示 矩形窗口左上角頂點的位置, 如果爲空,則窗口位於屏幕中心
如:

turtle.setup(width=0.8, height=0.6)
turtle.setup(width=800, height=600, startx=200, starty=200)

2. 畫筆

2.1 畫筆的狀態

在畫布上,默認有一個座標原點爲畫布中心的座標軸, 座標原點上有一隻面朝x軸正方向小烏龜. 這裏我們描述小烏龜時使用了兩個詞語:座標原點(位置),面朝x軸正方向(方向), turtle繪圖中, 就是使用位置方向描述小烏龜(畫筆)的狀態

2.2 畫筆的屬性

畫筆(畫筆的屬性,顏色、畫線的寬度)

  1. turtle.pensize():設置畫筆的寬度;

  2. turtle.pencolor(); 沒有參數傳入,返回當前畫筆顏色,傳入參數設置畫筆顏色,可以是字符串如"green", “red”,也可以是RGB 3元組,

    >>> pencolor('brown')
    >>> tup = (0.2, 0.8, 0.55)
    >>> pencolor(tup)
    >>> pencolor()
    '#33cc8c'
    
  3. turtle.speed(speed): 設置畫筆移動速度,畫筆繪製的速度範圍[0,10]整數, 數字越大越快

2.3 繪圖命令

操縱海龜繪圖有着許多的命令,這些命令可以劃分爲3種:一種爲運動命令,一種爲畫筆控制命令,還有一種是全局控制命令

(1)畫筆運動命令:

命令 說明
turtle.forward(distance) 向當前畫筆方向移動distance像素長
turtle.backward(distance) 向當前畫筆相反方向移動distance像素長度
turtle.right(degree) 順時針移動degree°
turtle.left(degree) 逆時針移動degree°
turtle.pendown() 移動時繪製圖形,缺省時也爲繪製
turtle.goto(x,y) 將畫筆移動到座標爲x,y的位置
turtle.penup() 移動時不繪製圖形,提起筆,用於另起一個地方繪製時用
turtle.speed(speed) 畫筆繪製的速度範圍[0,10]整數
turtle.circle() 畫圓,半徑爲正(負),表示圓心在畫筆的左邊(右邊)畫圓

(2)畫筆控制命令:

命令 說明
turtle.pensize(width) 繪製圖形時的寬度
turtle.pencolor() 畫筆顏色
turtle.fillcolor(colorstring) 繪製圖形的填充顏色
turtle.color(color1, color2) 同時設置pencolor=color1, fillcolor=color2
turtle.filling() 返回當前是否在填充狀態
turtle.begin_fill() 準備開始填充圖形
turtle.begin_fill() 準備開始填充圖形
turtle.end_fill() 填充完成;
turtle.hideturtle() 隱藏箭頭顯示;
turtle.showturtle() 與hideturtle()函數對應

(3) 全局控制命令

命令 說明
turtle.clear() 清空turtle窗口,但是turtle的位置和狀態不會改變
turtle.reset() 清空窗口,重置turtle狀態爲起始狀態
turtle.undo() 撤銷上一個turtle動作
turtle.isvisible() 返回當前turtle是否可見
stamp() 複製當前圖形
turtle.write(s[,font=(“font-name”,font_size,“font_type”)]) 寫文本,s爲文本內容,font是字體的參數,裏面分別爲字體名稱,大小和類型;font爲可選項, font的參數也是可選項

3. 命令詳解

3.1 turtle.circle(radius, extent=None, steps=None)
描述: 以給定半徑畫圓
參數:
radius(半徑); 半徑爲正(負),表示圓心在畫筆的左邊(右邊)畫圓
extent(弧度) (optional);
steps (optional) (做半徑爲radius的圓的內切正多邊形,多邊形邊數爲steps)
舉例:
circle(50) # 整圓;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圓

4. 繪製例子

#PythonDraw.py
import turtle #海龜庫是turtle繪圖的pyhon實現
turtle.setup(650,350,200,200)#turtle.setup(with,height,startx,starty) 設置窗體的大小及位置
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()

'''
turtle.goto(x,y) 海龜從摸個位置去到達(x,y)的座標位置
turtle.fd(d) 向海龜的正前方向運行
turtle.bk(d) 向海龜的反方向運行
turtle.circle(r,angle) 以海龜的當前位置,左側的某一個點爲圓心,進行曲線運行
turtle.seth(angle) 改變海龜的行徑角度,只是改變行徑的方向(絕對角度)
turtle.left(angle) 當前的海龜向左或者向右改變運行的方向
turtle.right(angle)

turtle.colormode(mode) 顏色模塊
-1.0: RGB小數值模塊
-255:RGB整數值模塊

import <庫名>
<庫名>.<函數>(<函數參數>)

from <庫名> import <函數名>
from <庫名> import*
<函數名>(<函數參數>)

import <庫名> as <庫別名>
<庫別名>.<函數> (<函數參數>)

turtle.penup() 擡起畫筆,海龜在飛行         別名  turtle.pu()
turtle.pendown() 畫筆落下,海龜爬行               turtle.pd()
turtle.pensize(width) 畫筆寬度,海龜的腰圍        turtle.width(width)
turtle.pencolot(colot) color爲顏色字符串或r,g,b值

for <變量> in range (<函數名>)
    <被循環執行的語句>
-<變量>表示每次循環的計數,0到<次數>1

range()函數
-range(N)
 產生0到N-1的整數序列,共N個
-range(M,N)
 產生M到N-1的整數序列,共N-M個
'''

在這裏插入圖片描述

import turtle as t
# 繪製小豬佩奇
# =======================================

t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), "pink")
t.setup(840, 500)
t.speed(10)

# 鼻子
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3)  # 向左轉3度
        t.fd(a)  # 向前走a的步長
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
        t.end_fill()

t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()

t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()

# 頭
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
    if 0 <= i < 30 or 60 <= i <90:
        a = a+0.08
        t.lt(3)  # 向左轉3度
        t.fd(a)  # 向前走a的步長
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
        t.end_fill()

# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()

t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()

#眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()

t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()

t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()

# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)

# 身體
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()

# 手
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)

t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)

# 腳
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done()

在這裏插入圖片描述

#繪製哆啦A夢
import turtle as t

t.speed(10)
t.pensize(8)
t.hideturtle()
t.screensize(500, 500, bg='white')
 
# 貓臉
t.fillcolor('#00A1E8')
t.begin_fill()
t.circle(120)
t.end_fill()
 
t.pensize(3)
t.fillcolor('white')
t.begin_fill()
t.circle(100)
t.end_fill()

t.pu()
t.home()
t.goto(0, 134)
t.pd()
t.pensize(4)
t.fillcolor("#EA0014")
t.begin_fill()
t.circle(18)
t.end_fill()
 
t.pu()
t.goto(7, 155)
t.pensize(2)
t.color('white', 'white')
t.pd()
t.begin_fill()
t.circle(4)
t.end_fill()

t.pu()
t.goto(-30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3) #向左轉3度
        t.fd(a) #向前走a的步長
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
 
t.pu()
t.goto(30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3)  # 向左轉3度
        t.fd(a)  # 向前走a的步長
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()
 
t.pu()
t.goto(-38,190)
t.pensize(8)
t.pd()
t.right(-30)
t.forward(15)
t.right(70)
t.forward(15)
 
t.pu()
t.goto(15, 185)
t.pensize(4)
t.pd()
t.color('black', 'black')
t.begin_fill()
t.circle(13)
t.end_fill()
 
t.pu()
t.goto(13, 190)
t.pensize(2)
t.pd()
t.color('white', 'white')
t.begin_fill()
t.circle(5)
t.end_fill()
 
t.pu()
t.home()
t.goto(0, 134)
t.pensize(4)
t.pencolor('black')
t.pd()
t.right(90)
t.forward(40)

t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(10)
t.forward(80)
 
t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(6)
t.forward(80)

t.pu()
t.home()
t.goto(0,104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(0)
t.forward(80)

 

# 左邊的鬍子
t.pu()
t.home()
t.goto(0,124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(170)
t.forward(80)

t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(174)
t.forward(80)

t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(180)
t.forward(80)

t.pu()
t.goto(-70, 70)
t.pd()
t.color('black', 'red')
t.pensize(6)
t.seth(-60)
t.begin_fill()
t.circle(80,40)
t.circle(80,80)
t.end_fill()

t.pu()
t.home()
t.goto(-80,70)
t.pd()
t.forward(160)
 
t.pu()
t.home()
t.goto(-50,50)
t.pd()
t.pensize(1)
t.fillcolor("#eb6e1a")
t.seth(40)
t.begin_fill()
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(40)
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(220)
t.circle(-80, 40)
t.circle(-80, 40)
t.end_fill()

 

# 領帶
t.pu()
t.goto(-70, 12)
t.pensize(14)
t.pencolor('red')
t.pd()
t.seth(-20)
t.circle(200, 30)
t.circle(200, 10)
 
# 鈴鐺
t.pu()
t.goto(0, -46)
t.pd()
t.pensize(3)
t.color("black", '#f8d102')
t.begin_fill()
t.circle(25)
t.end_fill()

t.pu()
t.goto(-5, -40)
t.pd()
t.pensize(2)
t.color("black", '#79675d')
t.begin_fill()
t.circle(5)
t.end_fill()

t.pensize(3)
t.right(115)
t.forward(7)


#大
t.pu()
t.goto(-60,-100)
t.seth(0)
t.pd()
t.pensize(8)
t.pencolor("purple")
t.fd(60)
t.fd(-30)
t.pu()
t.goto(-30,-80)
t.seth(-90)
t.pd()
t.fd(20)

t.pu()
t.goto(-30,-100)
t.pd()
t.seth(-135)
t.fd(40)

t.pu()
t.goto(-30,-100)
t.pd()
t.seth(-45)
t.fd(40)


#大
t.pu()
t.goto(10,-100)
t.seth(0)
t.pd()
t.pensize(8)
t.pencolor("purple")
t.fd(60)
t.fd(-30)
t.pu()
t.goto(40,-80)
t.seth(-90)
t.pd()
t.fd(20)

t.pu()
t.goto(40,-100)
t.pd()
t.seth(-135)
t.fd(40)

t.pu()
t.goto(40,-100)
t.pd()
t.seth(-45)
t.fd(40)

t.mainloop()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章