Turtle庫是Python語言中一個很流行的繪制圖像的函數庫
使用之前需要導入庫:import turtle
turtle繪圖的基礎知識:
1.設置畫布窗口
turtle.setup(width,height,startx,starty)
-setup() 設置窗體的位置和大小
相對於桌面的起始點的坐標以及窗口的寬度高度,若不寫窗口的起始點,則默認在桌面的正中心
窗體的坐標原點默認在窗口的中心
2.絕對坐標
○ turtle.goto(100,100):指從當前的點指向括號內所給坐標
3.海龜坐標,把當前點當做坐標,有前方向,后方向,左方向,右方向
turtle.fd(d):指沿着海龜的前方向運行
turtle.bk(d):指沿着海龜的反方向運行
turtle.circle(r,angle):指沿着海龜左側的某一點做圓運動
4.絕對角度
turtle.seth(angle):只改變海龜的行進方向(角度按逆時針),但不行進,angle為絕對度數。
5.海龜角度
turtle.left(angle)
turtle.right(angle)
6.切換RGB色彩模式
turtle.colormode(mode)
1.0:RGB小數模式
255:RGB整數模式
畫筆控制函數
turtle.penup() 別名turtle.pu()
畫筆抬起,不留下痕跡
turtle.pendown() 別名turtle.pd()
畫筆落下,留下痕跡
turtle.pensize(width) 別名turtle.width(width)
畫筆寬度
turtle.pencolor(color)
color為顏色字符串或者rgb值
eg:turtle.pencolor("purple")顏色字符串
turtle.pencolor(0.63,0.13,0.94)RGB的小數值
turtle.pencolor((0.63,0.13,0.94))RGB的元組值
turtle.goto(x, y) #將畫筆移動至(x,y)處
運動控制函數
turtle.forword(d) 別名turtle.fd(d)
向前行進
d:行進距離,可以為負數
turtle.circle(r,extent=None)
根據半徑r,繪制一個extent角度的弧度
r:默認圓心在海龜左側r距離的位置
方向控制函數
turtle.setheading(angle) 別名turtle.seth(angle)
改變行進方向
angle:改變方向的角度(絕對坐標下,絕對角度)
turtle.left(angle)
turtle.right(angle)
angle:當前方向上轉過得角度(海龜角度)
實例:
畫祝福字
# coding=utf-8
# HappyBirthday
import turtle
def move(angle, length):
# 畫筆控制函數
turtle.penup() # 畫筆抬起,不留下痕跡
# 方向控制函數
turtle.seth(angle) # 改變行進方向
# 運動控制函數
turtle.fd(length) # 向前行進
# prepare
# 寬高開始X開始Y
turtle.setup(1300, 400, 100, 100) # 設置窗體的位置和大小
# 畫筆控制函數
turtle.penup() # 畫筆抬起,不留下痕跡
turtle.fd(-350) # 向前行進
turtle.seth(90) # 改變行進方向
turtle.fd(50) # 向前行進
turtle.pendown() # 畫筆落下,留下痕跡
turtle.pensize(10) # 畫筆寬度
turtle.pencolor("green") # 畫筆顏色,為顏色字符串或者rgb值
turtle.seth(0) # 改變行進方向
# turtle.hideturtle() # 隱藏畫筆
turtle.speed(5) # 筆的移動速度參數范圍0.5——10,范圍之外為0,最快,不設置速度為最慢
# 呀
turtle.fd(100)
# 生
turtle.pencolor("green")
# turtle.circle(r,extent=None)
# 根據半徑r,繪制一個extent角度的弧度
# r:默認圓心在海龜左側r距離的位置
turtle.circle(50, 90)
turtle.circle(50, -30)
turtle.seth(0)
turtle.fd(100)
turtle.fd(-50)
turtle.left(90)
turtle.fd(30)
turtle.fd(-60)
turtle.left(90)
turtle.fd(50)
turtle.fd(-100)
turtle.fd(50)
turtle.left(90)
turtle.fd(50)
turtle.right(90)
turtle.fd(60)
turtle.fd(-120)
# 日
turtle.penup()
turtle.fd(-30)
turtle.pendown()
turtle.seth(90)
turtle.fd(100)
turtle.seth(0)
turtle.fd(70)
turtle.seth(-90)
turtle.fd(50)
turtle.seth(180)
turtle.fd(70)
turtle.seth(-90)
turtle.fd(50)
turtle.seth(0)
turtle.fd(70)
turtle.seth(90)
turtle.fd(50)
# 移動
move(0, 30)
# 快
turtle.pensize(8)
turtle.circle(30, 15)
turtle.pendown()
turtle.circle(30, 60)
turtle.penup()
turtle.seth(0)
turtle.fd(13)
turtle.seth(90)
turtle.pendown()
turtle.fd(40)
turtle.fd(-50)
turtle.penup()
turtle.seth(0)
turtle.fd(13)
turtle.pendown()
turtle.seth(-180)
turtle.circle(20, -90)
turtle.circle(20, 90)
turtle.penup()
turtle.fd(13)
turtle.pendown()
turtle.seth(-90)
turtle.fd(60)
move(0, 40)
move(90, 80)
turtle.pendown()
turtle.seth(0)
turtle.fd(30)
turtle.seth(90)
turtle.fd(30)
turtle.fd(-30)
turtle.seth(0)
turtle.fd(20)
turtle.seth(-90)
turtle.fd(35)
turtle.seth(0)
turtle.fd(10)
turtle.fd(-30)
turtle.seth(90)
turtle.fd(35)
turtle.fd(-35)
turtle.seth(0)
turtle.fd(-25)
move(-90, 50)
move(180, 25)
turtle.pendown()
turtle.seth(0)
turtle.penup()
turtle.circle(50, 20)
turtle.pendown()
turtle.circle(50, 70)
turtle.seth(-90)
turtle.circle(50, 60)
# 移動
move(0, 50)
move(90, 45)
# 樂
turtle.pensize(10)
turtle.pendown()
turtle.fd(40)
turtle.seth(0)
turtle.circle(50, 60)
turtle.circle(50, -25)
move(-90, 15)
turtle.pendown()
turtle.fd(30)
turtle.seth(0)
turtle.fd(-25)
turtle.fd(65)
turtle.fd(-40)
turtle.seth(-90)
turtle.fd(60)
turtle.seth(135)
turtle.fd(20)
move(135, 10)
turtle.pendown()
turtle.seth(-135)
turtle.fd(20)
move(0, 70)
turtle.pendown()
turtle.seth(135)
turtle.fd(20)
畫愛心
import turtle
def curve():
for i in range(200):
turtle.right(1)
turtle.forward(1)
turtle.color('pink')
turtle.write('aaaa', align='center')
turtle.begin_fill()
turtle.left(140)
turtle.forward(111.65)
curve()
# turtle.letf(120)
turtle.left(120)
curve()
turtle.forward(111.65)
turtle.end_fill()
turtle.hideturtle()
turtle.done()