學習網址:https://docs.python.org/zh-cn/3/library/turtle.html
Turtle庫
Turtle庫是Python語言中一個很流行的繪制圖像的函數庫,一個小烏龜,在一個橫軸為x、縱軸為y的坐標系原點(畫布中心),(0,0)位置開始移動,從而在它爬行的路徑上繪制了圖形
Turtle-窗口
turtle.setup(width=0.5, height=0.75, startx=None, starty=None)用于設置窗口的大小和位置
參數:width, height: 輸入寬和高為整數時, 表示像素; 為小數時, 表示占據電腦屏幕的比例;
(startx, starty): 這一坐標表示矩形窗口左上角頂點的位置, 如果為空,則窗口位于屏幕中心。
如:
turtle.setup(width=0.6,height=0.6)
turtle.setup(width=800,height=800, startx=100, starty=100)
說明當畫布尺寸比窗口小時,系統會自動把畫布放大填充滿整個窗口;比比窗口大時,會出現滾動條。
Turtle庫-畫布
畫布就是turtle展開用于繪圖的區域,我們可以設置它的大小和初始位置。
設置畫布大小
turtle.screensize(canvwidth=None, canvheight=None, bg=None),參數分別為畫布的寬(單位像素), 高, 背景顏色。
如:
turtle.screensize(800,600, "green")
Turtle庫-畫筆
畫筆(畫筆的屬性,顏色、畫線的寬度等)
- turtle.pensize():設置畫筆的寬度;
- turtle.pencolor():沒有參數傳入,返回當前畫筆顏色,傳入參數設置畫筆顏色,可以是字符串如"green", “red”,也可以是RGB 3元組。
- turtle.speed(speed):設置畫筆移動速度,畫筆繪制的速度范圍[0,10]整數,數字越大越快。
Turtle-繪圖命令
操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:一種為運動命令,一種為畫筆控制命令,還有一種是全局控制命令
蟒蛇繪制程序
代碼:
# author:dq
# project:PythonProject
# date:2021年11月18日
# function:
import turtledef drawSnake(rad, angle, len, neckrad):for i in range(len):turtle.circle(rad, angle)turtle.circle(-rad, angle)turtle.circle(rad,angle / 2)turtle.fd(rad)turtle.circle(neckrad + 1, 180)turtle.fd(rad * 2 / 3)def main():turtle.setup(1300, 800, 0, 0)pythonsize = 30turtle.pensize(pythonsize)turtle.pencolor("blue")turtle.seth(-40)drawSnake(40, 80, 5, pythonsize / 2)turtle.done()
main()
蟒蛇繪制程序說明
太陽花程序
五角星程序
小豬佩奇程序
代碼:
import turtle as tt.begin_fill()
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.08t.lt(3) # 向左轉3度t.fd(a) # 向前走a的步長else:a = a - 0.08t.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.08t.lt(3) # 向左轉3度t.fd(a) # 向前走a的步長else:a = a - 0.08t.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.end_fill()t.done()
櫻花程序
# coding=utf-8
# 畫一棵櫻花import turtle
import randomfrom time import sleep# 畫櫻花的軀干(60,t)
def tree(branchLen, t):sleep(0.0005)if branchLen > 3:if 8 <= branchLen <= 12:if random.randint(0, 2) == 0:t.color('snow') # 白else:t.color('lightcoral') # 淡珊瑚色t.pensize(branchLen / 3)elif branchLen < 8:if random.randint(0, 1) == 0:t.color('snow')else:t.color('lightcoral') # 淡珊瑚色t.pensize(branchLen / 2)else:t.color('sienna') # 赭(zhě)色t.pensize(branchLen / 10) # 6t.forward(branchLen)a = 1.5 * random.random()t.right(20 * a)b = 1.5 * random.random()tree(branchLen - 10 * b, t)t.left(40 * a)tree(branchLen - 10 * b, t)t.right(20 * a)t.up()t.backward(branchLen)t.down()# 掉落的花瓣
def petal(m, t):for i in range(m):a = 200 - 400 * random.random()b = 10 - 20 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color('lightcoral') # 淡珊瑚色t.circle(1)t.up()t.backward(a)t.right(90)t.backward(b)def main():# 繪圖區域t = turtle.Turtle()# 畫布大小w = turtle.Screen()t.hideturtle() # 隱藏畫筆turtle.getscreen().tracer(5, 0)w.screensize(bg='wheat') # wheat小麥t.left(90)t.up()t.backward(150)t.down()t.color('sienna')# 畫櫻花的軀干tree(60, t)# 掉落的花瓣petal(200, t)w.exitonclick()main()
三角形程序
#author:dq
#project:PythonProject
#date:2021年11月18日
#function:三角形import turtle
def drawBig():turtle.pensize(5)turtle.pencolor('yellow')turtle.forward(200)turtle.left(120)turtle.forward(200)turtle.left(120)turtle.forward(200)
def drawLitte():turtle.home()turtle.forward(100)turtle.left(60)turtle.forward(100)turtle.left(120)turtle.forward(100)turtle.left(120)turtle.forward(100)turtle.done()
drawBig()
drawLitte()